Commit Graph

414 Commits

Author SHA1 Message Date
Samuel Colvin 8846ec4685 limit the length of generics._limit_assigned_parameters (#4083)
* limit the length of generics._limit_assigned_parameters

* switch to using _limit_cache_size for both

* add change description

* correct `_limit_cache_size` cache

* implemented LimitedDict

* try using UserDict

* try upgrading cython

* stop LimitedDict from inheriting from dict

* separate LimitedDict for typing checking :-(

* fix for __class_getitem__
2022-05-18 15:22:58 +01:00
Giuliano Oliveira 5a61292252 Adds dataclass_transform to dataclass (#4007)
*  Adds dataclass_transform to dataclasses

*  Adds dataclass_transform to dataclasses

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2022-05-18 12:19:25 +01:00
Samuel Colvin abea8232ee speedup __instancecheck__ check on BaseModel when they fail (#4081)
* speedup __instancecheck__ check on BaseModel when they fail

* add change description

* linting
2022-05-17 14:13:36 +01:00
David Brochart a7e896c5a3 Update Jupyter's use of pydantic (#4082)
* Update Jupyter's use of pydantic

* Fix markdown link
2022-05-16 11:26:31 +01:00
Maxim Martynov cc54acb612 Fix missing port in HttpUrl.build() result (#3652)
* Port number is no longer being ignored by HttpUrl.build()

* Update tests/test_networks.py

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>

* Update networks.py

* Update tests/test_networks.py

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>

* Update test_networks.py

* Update test_networks.py

* update change description

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2022-05-14 17:35:39 +01:00
Samuel Colvin 122dd2f24e fix ClassVars, better fix for #3679 (#4077) 2022-05-14 17:27:52 +01:00
Alex f69012a5aa fix: error checking inheritance when using PEP585 and PEP604 type hints (#3681)
* Add tests

* Fix the issue

* Add changes file

* Improved convert_generics

* Add default fallback to convert_generics
Improved Annotated and Literal handling

* Fix Cython doesn't support generic types (PEP560)
Watch cython issue cython/cython#2753
Previous implementation can be used after cython 3.0 release

* Add custom type test

* Cosmetic fixes

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>

* Fix typos

* Add SelfReferencing test validation
Add parametrization to

* Fix: parametrization caused test discovery problem

* Better explanation for a test case

* Better assertions for model creation tests

* Rerun CI

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
2022-05-14 17:26:12 +01:00
Samuel Colvin 8718db581d Fix JSON Schema generation for Discriminated Unions within lists. (#4071)
* Fix JSON Schema generation for Discriminated Unions within lists.

* linting

* fix mypy
2022-05-14 07:26:34 +01:00
Adrian Garcia Badaracco e5540296cc Fix issue with in-place modification of FieldInfo (#4067)
* Fix info with in-place modification of field info

* add changes

* add test for 3714

* Update changes/4067-adriangb.md

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
2022-05-13 15:16:32 +01:00
Samuel Colvin a45276d6b1 guard against ClassVar in fields (#4064)
* guard against ClassVar in fields, fix #3679

* fix linting

* skipif for test_class_var_forward_ref
2022-05-13 15:06:53 +01:00
Yurii Karabas 42acd8f8d2 Fix issue with self-referencing dataclass (#3713)
* Fix issue with self-referencing dataclass

* Fix mypy issue
2022-05-11 19:09:13 +01:00
Luis R faee3301eb Fix regression in handling of nested dataclasses in get_flat_models_from_field (#3819)
* add test for nested python dataclass schema generation

* fix handling of dataclasses in `get_flat_models_from_field`

* add change note
2022-05-11 19:02:37 +01:00
Samuel Colvin 74403c2f15 test pyright with pydantic (#3972)
* test pyright with pydantic

* rename file to avoid pytest running it

* try another name 😴

* add docs about BaseSettings and Field

* add change
2022-05-11 19:00:37 +01:00
Eric Jolibois 5490ad5173 fix: Config.copy_on_model_validation does a deep copy and not a shallow one (#3642)
* fix: `Config.copy_on_model_validation` does a deep copy and not a shallow one

closes #3641

* fix: typo

* use python 3.10 to run fastapi tests

* fix fastapi test call

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2022-05-11 17:03:48 +01:00
Samuel Colvin 8997cc5961 Remove benchmarks completely (#3973)
* removing benchmarks completely

* [no ci] add change
2022-04-04 13:29:17 +01:00
Samuel Colvin b25e22f7d4 [no ci] correct name of change file 2022-04-04 13:22:07 +01:00
Gary Donovan 7f90b2f342 Remove incorrect comment about lazy evaluation of setting sources (#3806)
* Remove incorrect comment about lazy evaluation of setting sources

It looks like the current implementation always evaluates every source (https://github.com/samuelcolvin/pydantic/blob/9d631a3429a66f30742c1a52c94ac18ec6ba848d/pydantic/env_settings.py#L73) before coalescing them into a single dictionary to pass to `BaseModel`. So the comment about lazy evaluation is incorrect and should be removed.

* Add changelog
2022-04-02 15:25:43 +01:00
Samuel Colvin f0acf6efe7 Prevent subclasses of bytes being converted to bytes (#3707)
* adding a test

* fix and add change description
2022-04-02 14:45:31 +01:00
Tom Milligan 02eb182db0 fix: clarify that discriminated unions do not support singletons (#3639) 2022-04-02 14:06:11 +01:00
Wong Hoi Sing Edison 6e0f81c73f CentOS 7: read_text(encoding='utf-8') (#3625)
With CentOS 7 Python 3.6, running install from source with pip failed:

    sudo podman run -ti --rm centos:7
    yum -y update
    yum -y install epel-release
    yum -y install git python3 python3-devel python3-pip python3-setuptools python3-wheel
    git clone https://github.com/samuelcolvin/pydantic.git
    cd pydantic
    pip3 install .

With following error message:

    [root@c99d0585636c pydantic]# pip3 install .
    Processing /pydantic
        Complete output from command python setup.py egg_info:
        Traceback (most recent call last):
          File "<string>", line 1, in <module>
          File "/tmp/pip-91v_ixvz-build/setup.py", line 62, in <module>
            history = (THIS_DIR / 'HISTORY.md').read_text()
          File "/usr/lib64/python3.6/pathlib.py", line 1197, in read_text
            return f.read()
          File "/usr/lib64/python3.6/encodings/ascii.py", line 26, in decode
            return codecs.ascii_decode(input, self.errors)[0]
        UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 14648: ordinal not in range(128)

        ----------------------------------------
    Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-91v_ixvz-build/

This PR add the required `read_text(encoding='utf-8')` for `setup.py`.

Signed-off-by: Wong Hoi Sing Edison <hswong3i@pantarei-design.com>
2022-04-02 14:05:12 +01:00
Samuel Colvin 6f46a5a146 drop python3.6 support (#3605)
* drop python3.6 support

* revert small change

* fix 3.7 failures

* more cases and cleanup

* add change description
2022-01-02 13:53:45 +00:00
Samuel Colvin 5406423268 prepare for v1.9.0 release 2021-12-31 14:53:14 +00:00
Samuel Colvin 87da9ac23f apply update_forward_refs to json_encoders (#3595)
* apply update_forward_refs to json_encoders, fix #3583

* linting

* mypy

* avoid use of ForwardRef with python3.6

* fix ForwardRef usage, take 2

* coverage
2021-12-31 14:44:09 +00:00
Samuel Colvin 5d6f48cb07 prepare for v1.9.0a2 2021-12-24 13:54:35 +00:00
Hyun Sol c532e8324e #3234 exclude extra field when represent model (#3241)
* exclude extra field when represent model

* add test code

* fix W293

* add change md

* Update changes/3234-cocolman.md

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>

* Update pydantic/main.py

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>

* Update tests/test_main.py

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
2021-12-19 16:08:26 +00:00
layday da916f369e Remove __root__ from BaseModel typing-only attrs (#3540)
* Remove `__root__` from BaseModel typing-only attrs

`__root__` is not a class variable nor is it always present.

The switch to `ClassVar` caused type errors to be reported in Pylance/Pyright.

* Add changelog entry
2021-12-19 10:24:52 +00:00
Samuel Colvin 5de27ca465 update history and uprev version 2021-12-18 21:18:37 +00:00
Mark Trifonov be246701c5 Nested env (#3159)
* Environment names for complex types #2304

* nested env disabled by default

* cleanup

* nested env settings: simplified and mypy fixes

* nested env settings: config, test, doc

* nested env settings: changes file

* nested env settings: cleanup

* Apply suggestions from code review

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>

* Apply suggested changes from code review

* lint fix

* changes from code review

* simplify explosing env vars

* linting

Co-authored-by: Mark Trifonov <>
Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2021-12-18 20:56:14 +00:00
Jaakko Moisio 63337fbadc Add optional field argument to __modify_schema__() (#3434)
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2021-12-18 20:55:22 +00:00
Christian Bundy 49f946ddc3 Add support for removing TypeVarDef in mypy 0.920 (#3175)
* Add support for removing TypeVarDef in mypy 0.920

* Add changes/3175-christianbundy.md

* type ignore on import

* 🤦

* coverage, again

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2021-12-18 20:53:51 +00:00
Marcelo Trylesinski 7eaa582980 Add AmqpDsn class (#3254)
* Add RabbitmqDsn

Will update once finished - haven't looked at the CONTRIB yet. Want to test locally to see if it works.

* added tests; added to docs

* added changes

* fixed import in networks.py

* fixed linting issues; fixed __init__.py import issue

* sorted imports

* added trailing comma on imports

* Merge master

* Change class name from RabbitmqDsn to RabbitMqDsn

* Format code

* Rename change file and prettify content

* Fix RabbitMQ name on documentation

* Add a trivial test

* Address Samuel and Nuno's comments

* Refactor AMQP tests according to Redis tests style

* Update docs/examples/settings_main.py

* cleanup

Co-authored-by: Thomas <thomas@9bitbyte.com>
Co-authored-by: Thomas Crha <tom.crha@dragonflytechnologies.com>
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2021-12-18 20:40:16 +00:00
Eric Jolibois c834f3419d Support discriminated union (#2336)
* feat: add discriminated union

* feat: add OpenAPI spec schema

* test: add basic example for generated schema

* test: add validation tests

* docs: add basic documentation

* fix: support ForwardRef

* test: add ForwardRef case

* fix: false positive lint error

https://github.com/PyCQA/pyflakes/pull/600

* improve error

* add schema/schema_json utils

* fix tests after merge

* refactor: add `discriminator` attribute to `FieldInfo`

* refactor: @cybojenix remarks

* fix schema with forward ref

* start nested

* feat: add allowed values in error message

* fix wrong check

Same example with

```
class FooDomainA(BaseModel):
    __root__: Union[FooDomainAA, FooDomainAB]
```

(without a discriminator)
should obviously be valid

* test: add nested examples

* remove uncovered code as we don't need it

* docs: add nested example

* fix: support properly Annotated Field syntax

* support naked annotated

* fix: handle TypeError

* make error loc more explicit

* fix behaviour with basemodel instance as value

* support schema for dataclasses

* tweak examples

* refactor: context manager just around code that fails

* refactor: add docstring + tweak on `get_sub_types`

* refactor: move `get_discriminator_values` in `utils.py`

* refactor: create `MissingDiscriminator` and `InvalidDiscriminator`

* refactor: move logic in `_validate_discriminated_union`

* refactor: remove `DiscriminatedUnionConfig`

* docs: schema/schema_json

* tests: add tests with other `Literal` types

* update 3.10

* add schema docstring

* weird bug on 3.8 with `Literal[None]`

* bump to view docs & coverage

* bump to prompt tests

* move tests in dedicated file

* chore: rewording

* refactor: replace property by direct slot

* refactor: faster check

* refactor: missing discriminator

* refactor: change error to ConfigError

* refactor: use display_as_type

* fix: mypy

* fix: duplicate

* feat: handle alias

* feat: handle nested unions

* tweak first example

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2021-12-18 16:31:35 +00:00
Samuel Colvin d36bb74e83 generate x86_64 and arm64 binaries to support MacOS M1 CPU (#3514)
* generate x86_64 and arm64 binaries for macos

* Uncomment check
2021-12-12 00:13:48 +00:00
Bas van Beek e012089f12 Mention Python >= 3.9.2 as an alternative to typing_extensions.TypedDict (#3374)
* Mention python >= 3.9.2 as an alternative to `typing_extensions.TypedDict`

* Narrow the upper version for `LegacyTypedDict`: 3.9 -> 3.9.2

* Add an entry to `changes`

* Update pydantic/annotated_types.py

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>

* Update `TypedDict` exception message in the test suite

* linting

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2021-12-11 22:44:09 +01:00
Sebastián Ramírez 61d30aed7a Add support for decimal-specific configs in Field() (#3507)
*  Add support for Decimal-specific configs in Field()

*  Add/update tests for condecimal and variant with Field()

* 📝 Update schema - Field() docs including Decimal-specific configs

* 📝 Add PR changes file
2021-12-11 11:21:06 +00:00
Toby Harradine 6ad80cd402 Allow None when using a Union containing Any or object (#3452)
* Add unit test for Union[int, Any]

* Allow None when Any or object is in Union

Resolves #3444

* Add changelog entry for #3444

* Prefer `is_none_type()` over `type_ is NoneType`

* fix(lint): remove useless import

Co-authored-by: PrettyWood <em.jolibois@gmail.com>
2021-12-10 18:41:37 +00:00
Nuno André 91ecfd651e feat: Add unique items validation to constrained lists (#2618)
* Add unique items validation to constrained list

* add unique_items to field and schema
add failover for unhashable types
check keyword value to call the validator
add some tests

* update unique_items validation

Co-authored-by: Nuno André Novo <nuno.novo@forensic-security.com>
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2021-12-10 10:04:06 +00:00
Michael Rios 043d16bcf8 Update validator function name in examples to show that value must b… (#3327)
* Update validator function name in  examples to show that value must be bar vs must contain a space

* Add md file in
2021-12-08 23:17:08 +00:00
Hiroshi Ogawa d1a014542a fix: collect only valid fields in mypy plugin (#3247)
* fix: collect only valid fields in mypy plugin

* Fix flake8 C901 in `PydanticModelTransformer.collect_fields`
2021-12-08 22:33:43 +00:00
João M. Martins dc4710b56c Replacing encode_default instance check with strict type check (#3197)
* Replacing encode_default instance check with strict type check

* Adding change notes

* Changing to dictionary indexing in tests

* Adding explicit Enum check and returning its value
2021-12-08 21:52:25 +00:00
David Reed a3f180eb2b Make SecretsSettingsSource deserialize complex types (#3194)
* Make SecretsSettingsSource deserialize complex types

* Add change notes
2021-12-08 21:50:47 +00:00
Eric Jolibois 67bfbe3f44 feat: avoid dict coercion for subclasses (#3138)
* feat: avoid dict coercion for subclasses

* docs: add change file

* add coercion example
2021-12-08 21:16:13 +00:00
Eric Jolibois 8c2c602c1b feat: add confrozenset() (#2672)
* feat: add `confrozenset()`

* fix: add tests for allowed_schemas
2021-12-07 22:11:42 +00:00
Eric Jolibois 458f257dcb feat: make JSON serialization of referenced pydantic models possible (#2650)
* feat: make JSON serialization of referenced pydantic models possible

* add docs

* chore: @samuelcolvin remarks
2021-12-07 22:01:11 +00:00
Eric Jolibois c38c463137 feat: add Config.smart_union option (#2092)
* feat: add `Config.smart_union` to prevent coercion in `Union` if possible

* docs: write some documentation

* Update docs/usage/model_config.md

Thanks @djpugh

Co-authored-by: David J Pugh <6003255+djpugh@users.noreply.github.com>

* improve doc

* support 3.10

* improve smart_union

* Update docs/usage/types.md

Co-authored-by: David J Pugh <6003255+djpugh@users.noreply.github.com>

* put new sentence inside warning block

* docs: reorder

* rename is_union_origin into is_union

* inverse and condition for perf

* fix doc

Co-authored-by: David J Pugh <6003255+djpugh@users.noreply.github.com>
2021-12-07 21:58:52 +00:00
Sam McHardy eef4ac505e Fix missing enum extra (#2818)
* Allow passing schema_overrides to get_field_info_schema

This function was resetting schema_overrides which prevents **extras on Enum type Fields
from being saved to their json schema

* Add changes file

* Update changes/2697-sammchardy.md

Co-authored-by: Eric Jolibois <em.jolibois@gmail.com>

Co-authored-by: Sam McHardy <sam.mchardy@psma.com.au>
Co-authored-by: Eric Jolibois <em.jolibois@gmail.com>
2021-12-07 15:10:53 +01:00
Yurii Karabas 415eb54f96 Try to evaluate forward refs after model created (#2588)
* Try to evaluate forward refs after model created

* Upadate docs and remove code duplication

* Update changes/2588-uriyyo.md

Co-authored-by: Eric Jolibois <em.jolibois@gmail.com>

* Update docs/usage/postponed_annotations.md

Co-authored-by: Eric Jolibois <em.jolibois@gmail.com>

* Remove unused import

Co-authored-by: Eric Jolibois <em.jolibois@gmail.com>
2021-12-05 14:20:48 +00:00
diabolo-dan e71f53d2b5 Improve generic subclass support (#2549)
* Derive concrete subclasses for parameterised generics

* Resolve type issues

* Add negative assertions to generic subclass tests

* Remove incorrect subclassing of partial.

The type was incorrectly being picked up for this style of subclassing,
and it can be regardless inferred through cls.

* Apply feedback:

* Improve parameterisation explanation
* fix typos
* Alias Parameterisation type

* Apply suggestions from code review

* start docstring with newline.
* Use None as default over empty tuple.

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>

* Combine _assigned_parameters cases in __paramaterized_bases__ of generics

* Add description for the `_assigned_parameters` variable.

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2021-12-05 13:40:23 +00:00
Eric Jolibois a35cde90af feat(schema): set minItems and maxItems in JSON schema for tuples (#2497)
* feat(schema): enforce length in generated JSON schema for tuple type

* docs: add change file

* docs: update documentation

* simplify a bit

* always set array
2021-12-05 13:20:07 +00:00
Yurii Karabas ee0b2e2153 Fix issue with ClassVar parsing (#3403)
* Fix issue with ClassVar parsing

* Fix lint error

* Simplify test

* Fix condition and test
2021-11-18 23:40:15 +01:00