Commit Graph

390 Commits

Author SHA1 Message Date
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
Sebastián Ramírez 45db4ad3aa Add autocomplete support for VS Code, via dataclass_transform (#2721)
*  Add autocomplete support for VS Code, via dataclass_transform

* 📝 Update changes

* 📝 Add docs for VS Code

* 📝 Clarify strict type error checks alternatives, include example with cast()

* ♻️ Update BaseSettings annotation to improve editor support

keeping editor support for BaseSetting's custom Config, but preventing __config__ from showing in constructor on editors supporting dataclass_transform

* 🎨 Remove unused type: ignore comment

* 🎨 Update type annotations for BaseSettings and BaseModel to use ClassVar where suitable

to improve editor support with type annotations and dataclass_transform

* 🎨 Apply ClassVars again

* 📝 Simplify VS Code docs terms

refer mainly to Pylance and clarify the relationship with Pyright

* 📝 Add link to Pylance FAQ

Co-authored-by: PrettyWood <em.jolibois@gmail.com>
2021-09-06 12:13:02 +02:00
Eric Jolibois 5bc1fcfb92 feat: support custom extra in validate_arguments (#3177) 2021-09-06 00:44:16 +02:00
Eric Jolibois c4e793b767 fix: make validate_arguments work with Config.validate_all (#3176)
* fix: make `validate_arguments` work with `Config.validate_all`

* coverage
2021-09-06 00:33:36 +02:00
Yurii Karabas 63e42db921 Fix bug when mypy plugin fail on construct method call (#2767)
* Fix bug when mypy plugin fail on construct method call

* Update type annotation for __config__ field

* Remove type ignore

* Update changes/2753-uriyyo.md

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

Co-authored-by: Eric Jolibois <em.jolibois@gmail.com>
2021-09-05 11:29:13 +02:00
John Walk 0c2f69c500 Toml support for pydantic-mypy plugin config (#2908)
* add toml reader

* fix path

* skip configparser step

* fix quotes

* full pyproject.toml check

* add doc note

* cleaner formatting, raise ValueError for non-bool

* fix tests

* add bad config test case

* add changelog file.

* bump mypy to 0.902

* tweak change MD, fix formatting in requirements

* import check around toml

* switch to tomli for parsing to match mypy dependency

* import check around toml/tomli

* add note on tomli usage

* more succinct changelog entry

* fix quotes in changelog

* linting fixes, remove unnecessary stub install

* mypy checks on mypy plugin file

* wrongly placed pragma no cover

Co-authored-by: PrettyWood <em.jolibois@gmail.com>
2021-09-05 11:25:47 +02:00
Jamey Sharp 72d4f30f0a Resolve postponed NamedTuple annotations (#2760) (#2761)
Thanks to @PrettyWood for pointing me to the right place to fix this!

Since I was told that both NamedTuple and TypedDict use the same
__pydantic_model__ machinery that dataclasses do, I checked and found
that TypedDict had the same bug, and fixed that too.

Tests for both issues are included, which fail without the associated
fixes being applied.
2021-09-04 23:45:23 +02:00
Yurii Karabas 1aa22e199f Add ability to use Counter type (#2806)
Co-authored-by: Eric Jolibois <em.jolibois@gmail.com>
2021-09-04 23:41:09 +02:00
sergejkozin 257908628f fix(schema): fix schema generation for Deque field (#2811)
* fix(schema): fix schema generation for `Deque` field

* add change description
2021-09-04 22:45:40 +02:00
Xavier Defrang 161b000c7e Accept empty query/fragment URL parts (#2888)
* Accept empty query/fragment URL parts

* forbid two # in fragment

Co-authored-by: PrettyWood <em.jolibois@gmail.com>
2021-09-04 21:06:08 +02:00
Piotr Gnus abbad5e88c Added dark color scheme for docs (#2913)
* Added dark color scheme for docs

Added alternative, dark color scheme for docs, which should be automatically activated if user's browser presents the `prefers-color-scheme: dark` media query.

* switch icons

* add change file

Co-authored-by: PrettyWood <em.jolibois@gmail.com>
2021-09-04 20:37:42 +02:00
Pax e431604654 clarify docs on validation and strict type checking (#2855)
* clarify docs on validation and strict type checking

Related: Issue#578

Co-authored-by: cknoll <unknown@exampleemail.com>
Co-authored-by: PyAntony <unknown@exampleemail.com>

* add change.md for PR#2855

* docs: explicitly specify _partial_ support

* chore: update formatting of change description

Co-authored-by: cknoll <unknown@exampleemail.com>
2021-09-04 19:58:47 +02:00
Pax 4ac2501477 mention 3.8 requirement in contrib docs (#2856)
* mention 3.8 requirement in contrib docs

* add change.md for PR#2856

* docs: fix grammar and wording
2021-09-04 19:55:40 +02:00
Christopher Bartz 428f1e4d34 Parse JSON for union type with complex subfields (#2936) (#2970)
* Parse JSON for union type with complex subfields (#2936)

* style

* simplify

Co-authored-by: Christopher Bartz <c.bartz@zbw.eu>
Co-authored-by: PrettyWood <em.jolibois@gmail.com>
2021-09-04 19:53:32 +02:00
Hmvp 22849ee049 Fix error when using slots with multiple inheritance (#2989)
* Fix error when using slots with multiple inheritance

* typo

* typing

Co-authored-by: PrettyWood <em.jolibois@gmail.com>
2021-09-04 18:32:29 +02:00
Jacob Hayes acb4ff3744 Fix support for using a subclass of an annotation as a default (#3018)
* Add test for type hint subclass as default

* Fix support for using a subclass of an annotation as a default

* Add changes file

* add check in test

Co-authored-by: PrettyWood <em.jolibois@gmail.com>
2021-09-04 18:11:17 +02:00
Matthew Hughes e9327b85fc Add a TypedDict class for error objects (#3038)
* Add a TypedDict class for error objects

This adds more specific typing for the dictionaries in
`ValidationError.errors()`. This shouldn't introduce any functional
change.

* quotes

* split required and not required keys

* type but be a class

* workaround cython (can't wait for v3)

Co-authored-by: PrettyWood <em.jolibois@gmail.com>
2021-09-04 17:33:48 +02:00
Eric Jolibois 020a0219eb fix: make create_model_from_typeddict mypy compliant (#3047)
* fix: make `create_model_from_typeddict` mypy compliant

closes #3008

* add comment
2021-09-04 16:59:23 +02:00
Eric Jolibois f5a9f4fbe3 feat: handle object type (#3062) 2021-09-04 16:40:14 +02:00
Roger Collins 268c3cc41f Fix: _pydantic_post_init() reassigns __dict__ and removes properties (#3093)
* Fix: _pydantic_post_init() reassigns __dict__ and removes properties added to the object, for example in __new__(), breaking integration with SQLAlchemy and other libraries #3043

* Added changes file
2021-09-04 16:32:22 +02:00
Grégoire Charvet 黑瓜 97a4176110 Correctly parse generic models with Json[T] (#2987)
* Fix 2860

When dealing with JsonWrapper, replace the inner type when constructing
a a generic model.

* tweak and schema test

Co-authored-by: PrettyWood <em.jolibois@gmail.com>
2021-09-04 11:36:29 +02:00
Yurii Karabas b26d6f925b mypy: Add create_model overload (#2748)
* Add create_model overload

* Add changes

* fix: import

Co-authored-by: PrettyWood <em.jolibois@gmail.com>
2021-09-04 04:53:49 +02:00
Eric Jolibois 6b6116054a fix: nested literal with constraints (#2794)
* fix: nested literal with constraints

* docs: add change file
2021-09-04 04:36:42 +02:00
Shahriyar Rzayev 7108652d0b Renamed schema variable name to schema_ as there is a schema function defined (#2724)
* Renamed schema variable name to schema_ as there is a schema function already defined

* Same approach in enum_process_schema()

* added changes file

* Update changes/2724-shahriyarr.md

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

* last shadow rename

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
Co-authored-by: PrettyWood <em.jolibois@gmail.com>
2021-09-04 04:35:54 +02:00
Yurii Karabas 0d1ed534ac Fix mypy plugin issue with self field (#2743)
* Fix mypy issue with self field

* Add docs
2021-09-04 04:15:49 +02:00