Commit Graph

307 Commits

Author SHA1 Message Date
Samuel Colvin 619ff261c9 prepare for v1.8.1 2021-03-03 17:03:59 +00:00
Samuel Colvin 62bb2ad492 allow Config.field to update a Field (#2461)
* allow Config.field to update a Field, fix #2426

* move logic to update_from_config, work with Annotated

* fix flake8 erroneous warnings

* test for allow_mutation

* better support for allow_mutation
2021-03-03 16:52:37 +00:00
Eric Jolibois 3f84d1405e fix: validation with a BaseModel field and a custom root type (#2451) 2021-03-03 10:47:58 +00:00
Eric Jolibois ab691142b5 fix: avoid RecursionError when using some types like Enum or Literal with generic models (#2438)
* fix: support properly `Enum` when combined with generic models

* whitelist iterables

* update change description

* add test for Literal

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2021-03-03 09:25:23 +00:00
Tobi Lipede 429b439830 Allow for generation of a constrained float with multiple_of argument for hypothesis plugin (#2442)
* added method to generate a constrained float with multiple_of argument

* removed HealthCheck.filter_too_much

* Added change file

* fixes for case when min/max aren't provided; adding an extra test for float gt/lt with multiple of
2021-03-02 18:56:45 +00:00
Eric Jolibois a74232e101 fix: variable length tuples of compound types (#2421) 2021-03-02 12:09:31 +00:00
Eric Jolibois 37c37fd55e fix(mypy): fix custom Path and UUID related types (#2420)
* add tests that should pass

But we have those errors

226: error: Item "str" of "Union[UUID, str]" has no attribute "hex"  [union-attr]
227: error: Item "str" of "Union[UUID, str]" has no attribute "hex"  [union-attr]
228: error: Item "str" of "Union[Path, str]" has no attribute "absolute"  [union-attr]
229: error: Item "str" of "Union[Path, str]" has no attribute "absolute"  [union-attr]
230: error: Item "str" of "Union[Path, str]" has no attribute "absolute"  [union-attr]
231: error: Item "str" of "Union[Path, str]" has no attribute "absolute"  [union-attr]

* fix: right types should be valid

* remove new useless `type: ignore`

* docs: add change file
2021-03-02 12:07:07 +00:00
Eric Jolibois b2d3f333f0 refactor: set Pattern encoder in ENCODERS_BY_TYPE (#2444)
* refactor: set `Pattern` encoder in `ENCODERS_BY_TYPE`

* docs: add change file
2021-03-02 12:05:57 +00:00
Eric Jolibois f9fe4aa086 fix: do not overwrite declared hash in subclasses of a model (#2423) 2021-02-27 15:42:53 +00:00
Samuel Colvin aff4a41f9d preparing for v1.8 (#2414)
* preparing for v1.8 🎉 🚀

* change description for #2415

* tweak change descriptions

* fix nested lists in docs

* remove items in 1.7.3 from 1.8
2021-02-26 16:30:08 +00:00
xppt 8f0980e982 fix: prevent RecursionError while using recursive GenericModels (#2338)
Co-authored-by: Samuel Colvin <samcolvin@gmail.com>

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
2021-02-26 10:30:12 +00:00
Eric Jolibois 3ec35590f1 Support Field in dataclass + 'metadata' kwarg of dataclasses.field (#2384)
* Support `Field` in `dataclass` + `'metadata'` kwarg of `dataclasses.field`

Please enter the commit message for your changes. Lines starting

* add `__has_field_info_default__` for minimal effect on perf

* lower complexity of `_process_class`
2021-02-25 20:04:16 +00:00
Eric Jolibois ededd3ebf8 refactor(schema): support properly Literal in generated JSON schema (#2348)
* test: improve example

* refactor: use enum for Literal in JSON schema

* test: update test with new schema generation

* chore: add change file
2021-02-25 19:42:17 +00:00
Davis Kirkendall 2c2e238c19 Update mkdocs extensions to fix local syntax highlighting (#2401)
- Fixes #2400
2021-02-25 19:24:03 +00:00
Ofek Lev 4ddf4f14cd Properly retain types of Mapping subclasses (#2325)
* Properly retain types of Mapping subclasses

* Create 2325-ofek.md

* update with feedback

Co-Authored-By: Eric Jolibois <eric.jolibois@toucantoco.com>

* satisfy mypy?

* Update fields.py

Co-Authored-By: Eric Jolibois <eric.jolibois@toucantoco.com>

* show uncovered line numbers

* fix coverage

* update

* address feedback

* try

* update

Co-Authored-By: Eric Jolibois <eric.jolibois@toucantoco.com>

* rename test

* address feedback

Co-authored-by: Eric Jolibois <eric.jolibois@toucantoco.com>
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2021-02-25 17:27:50 +00:00
Davis Kirkendall aa92db5e4f Add test for generic aliases and lenient_issubclass (#2392) 2021-02-25 17:08:21 +00:00
Alexander Hultnér eab9d05473 fix #2293: Properly encode Decimals without any decimal places. (#2294)
* fix #2293: Properly encode Decimals without any decimal places.

* doc: Added changelog entry.

* refactor: Move ConstrainedDecimal test from separate file into test_json

* docs: Remove prefix from changelog.

* test: Changed test_con_decimal_encode to @samuelcolvins recommendations
2021-02-24 11:50:23 +00:00
Alexander c8883e34db Added support for 13/19 digits VISA credit cards in PaymentCardNumber type (#2286)
* Added support for 13/19 digits VISA credit cards

* Added changes md-file

* Fixed tests VALID_VISA_19 length, changed constants order

* tiny tweaks

Co-authored-by: Alexander Plaxin <a.plaxin@qiwi.com>
Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
2021-02-23 12:13:25 +00:00
Tom Cobb 3f849a368f Support Field(default_factory) in validate_arguments (#2176)
* Support Field(default_factory) in validate_arguments

* Added docs for validate_arguments with Field
2021-02-23 12:07:11 +00:00
Eric Jolibois 7da04d95fa fix(mypy): remove complaints about most custom _pydantic_ types (#2099)
* fix(mypy): handle FilePath and DirectoryPath custom types

closes #2098

* fix(mypy): handle almost all other custom types

* remove mypy overwrite when extra logic is set

* fix unused mypy ignore
2021-02-23 12:05:42 +00:00
Raphael Huille d8e8e6a780 Generate a hash function when frozen is True (#1881)
* feature: add a `frozen` parameter to config

For now, `frozen` is a strict duplication of `allow_mutation` parameter
i.e. setting `frozen=True` does everything that `allow_mutation=False` does.

NB: this does not change the behavior of `allow_mutation`.

In next commit, setting `frozen=True` will also make the BaseModel hashable
while the existing behavior of `allow_mutation` will not be updated.

* refactor: factorise immutability tests

* feature: generate a hash function when frozen is True

Now, setting `frozen=True` also generate a hash function for the model
i.e. `__hash__` is not `None`. This makes instances of the model potentially
hashable if all the attributes are hashable. (default: `False`)

* reviewer feedback: use hash of the class instead of the super

* reviewer feedback: fix spelling checks

* reviewer feedback: update changes description

* test: remwork mypy tests in order to catch only frozen related errors

Before: there were errors about other stuff than frozen behavior
After: The modification catch only errot related to the frozen behavior

* test: split test_immutablity in 2 functions

One function tests the behavior: 'the model is mutable'
The other tests the behavior:OC 'the model is immutable'

* test mutability: remove the unnecessary parametrization

* test immutability: remove assertion that do not test frozen behavior
2021-02-23 11:45:40 +00:00
Arseny Boykov ce67660d2f Allow to configure models through class kwargs (#2356)
* add support for class kwargs config

* reformat tests

* add changes file and docs

* fix linting in 'inherit_config'

* tweak docs

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2021-02-22 17:10:04 +00:00
Samuel Colvin b7a8ef25c6 fix coverage and make typing-extensions a required dependency (#2368)
* fixing coverage by simplifying Annotated import logic, fix #2367

* avoid checking against annotated if it's None

* make typing-extensions required WIP

* more making typing-extensions required

* fix docs and get_origin for python 3.6

* fix mypy test

* fix docs

* update docs, cleanup and add change

* clean docs/examples/schema_annotated.py

* move AnnotatedTypeNames
2021-02-17 19:17:30 +00:00
Samuel Colvin fc18f8ef34 make "resolve_annotations" more lenient, allowing for missing modules (#2366)
* make "resolve_annotations" more lenient, allowing for missing modules

* linting
2021-02-15 12:17:34 +00:00
Scott Black 13928e5b98 introduce allow_mutation Field constraint (#2196)
* introduce read_only Field constraint

* add changes markdown for read_only constraint

* add readOnly property to json schema generation

* Revert "add readOnly property to json schema generation"

This reverts commit dad3d3e23a7336aa978dcdedc157559ea102fa54.

* change read_only field constraint to allow_mutation

* Update change notes for allow_mutation

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

* allow field constraints defaults to be not None

* remove unnecessary test after constraint refactor

* push used constraints check back to schema functions

* use tuple item name descriptions instead of indexes

* move get_constraints function to method on FieldInfo

* address code review comments for minor changes

* Apply suggestions from code review

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

* fix merge conflict

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
2021-02-13 17:22:32 +00:00
Eric Jolibois add3a672a6 fix: allow None for type Optional[conset / conlist] (#2321) 2021-02-13 17:21:18 +00:00
Taneli Hukkinen 40a925f30a Fix python_requires metadata to require >=3.6.1 (#2306)
* Fix `python_requires` metadata to require >=3.6.1

* Add changelog entry
2021-02-13 17:14:28 +00:00
maximberg c3870b611e Added schema generation for Generic fields (#2262)
* Added schema generation for Generic fields with tests.
Fixed test_assert_raises_validation_error.

* Added schema generation for Generic fields with tests.
Fixed test_assert_raises_validation_error.

* tested on python 3.6, 3.7, 3.8, 3.9

* tested on python 3.6, 3.7, 3.8, 3.9

* restore formatting

* fix mistakes

* formatting

* formatting

* formatting

* fixed coverage

* changes file

* changes file

* remove redundant len

* Update pydantic/schema.py

Co-authored-by: Maz Jaleel <mazjaleel@gmail.com>

Co-authored-by: Maxim Berg <berg@petrostyle.com>
Co-authored-by: Maz Jaleel <mazjaleel@gmail.com>
2021-02-13 17:04:49 +00:00
Jacob Hayes b742c6f527 Support Annotated type hints and extracting Field from Annotated (#2147)
* Infer root type from Annotated

* Extract Field from Annotated

* Add changelog

* Extend existing get_args/get_origin

* Fix Annotated on py3.6 without typing-extensions

* Handle Ellipsis default

* Fix field reuse after FieldInfo.default mutation

* Fix ci
2021-02-13 16:13:21 +00:00
Arthur Pastel 33c5a4dc34 Add merged json_encoders inheritance (#2064)
* Implement merged json_encoders inheritance

* json_encoders inheritance documentation
2021-02-13 15:56:34 +00:00
Samuel Colvin b87e2492cc ensure cythonized functions are left untouched2 (#2228)
* ensure cythonized functions are left untouched2

* add change
2021-02-13 15:48:55 +00:00
layday 78934db631 PropagateClassVars to sub-models (#2179)
* Propagate`ClassVar`s to sub-models

Currently, if a `ClassVar` is defined on a model and re-defined
on a sub-model omitting the `ClassVar` annotation, Pydantic produces an
unrelated error:

    NameError: Field name "..." shadows a BaseModel attribute ...

This check was introduced to prevent shadowing Pydantic's own methods
and attributes defined on the `BaseModel` class.  Following this change,
class variables (that is, variables annotated with `ClassVar`)
defined on parent models will be inherited by sub-models and
will be overwritable without having to reapply the annotation.

Closes #2061.

* docs: explain how attributes are excluded and when to use `PrivateAttr`
2021-02-13 14:52:39 +00:00
Eric Jolibois 6f0ab55159 fix: update __fields_set__ in BaseModel.copy(update=…) (#2291)
* fix: update `__fields_set__` in `BaseModel.copy(update=…)`

closes #2290

* chore: rewrite to be more explicit

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

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
2021-02-13 11:57:17 +00:00
Eric Jolibois ad7862f032 fix: keep order of fields with BaseModel.construct() (#2282)
* fix: keep order of fields with `BaseModel.construct()`

closes #2281

* chore: keep extra fields in construct

thanks @layday!
2021-02-13 11:26:43 +00:00
Eric Jolibois 9ca8fe85eb fix: support empty tuple type (#2319) 2021-02-13 11:10:08 +00:00
David Jack Wange Olrik 12ebf0cfab fix: Change non-existent secrets dir into a warning (#2265)
* fix: Change non-existant secrets dir into a warning

* Simplify if condition

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

* Reword changelog entry

Co-authored-by: Eric Jolibois <em.jolibois@gmail.com>
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2021-02-13 11:06:39 +00:00
Anthony King b4d1ada004 fix(decorator): detect when a duplicate argument is passed in (#2250)
* fix(decorator): detect when a duplicate argument is passed in

Currently, it is possible to override a positional argument with a keyword
argument using the decorator.

In native python, this raises a TypeError.

Detect this usecase, to prevent keywords overriding positional parameters.

Closes: https://github.com/samuelcolvin/pydantic/issues/2249

* add changelog entry

* fix docs example to not override positional argument

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2021-02-13 11:06:12 +00:00
Eyitayo Ogunbiyi cb3c682eb4 Docs for con types (#2243)
* added documentation for con* functions

* added change markdown file

* updates change markdown file

* Update docs/usage/types.md

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

Co-authored-by: Eric Jolibois <em.jolibois@gmail.com>
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2021-02-13 10:40:05 +00:00
Anthony King 2a87f7954a fix(decorator): align var args + keywords behaviour with native python (#2252)
* fix(decorator): align kwargs behaviour with native python

Previously, validate_arguments would strip the value if the keyword
was the same as the target kwargs argument.

Align with Python's behaviour.

* fix: also prevent overwriting variable args

Introduces tests for both, which compare against the native python implementation

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

* mention about 'args' in the changelog too

* simplify var_kwargs pop

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

Co-authored-by: Eric Jolibois <em.jolibois@gmail.com>
2021-02-13 10:35:21 +00:00
Eric Jolibois b21da6f381 fix: support custom root type with nested models in parse_obj (#2238) 2021-02-13 10:18:54 +00:00
Eric Jolibois b076567591 fix: support properly custom root type with from_orm() (#2237)
* fix: support custom root type with `from_orm()`

* add other example

* chore: add change file

* refactor(main): use ROOT_KEY instead of __root__
2021-02-13 10:14:18 +00:00
Eric Jolibois 7bef40bb11 fix: resolve forward refs for inherited dataclasses (#2220)
* fix: resolve forward refs for inherited dataclasses

closes #1668

* chore: add change file

* fix: make test work everywhere

* chore: rename file just in case

As it doesn't solve the target issue, let's change the PR number

* docs: update change description
2021-02-13 10:10:14 +00:00
Eric Jolibois c314f5a909 feat: add support for NamedTuple and TypedDict types (#2216)
* feat: add support for `NamedTuple` and `TypedDict` types

* support `total=False`

* tests: fix ci with python < 3.8 without typing-extensions

* chore: improve mypy

* chore: @samuelcolvin remarks

* refactor: move tests in dedicated file

* docs: add annotated types section with examples

* feat: support properly required and optional fields

* chore(deps-dev): bump typing_extensions

* docs: add a note for `typing_extensions`

* chore: update message to be more accurate

* feat: pass down config to created models

* feat: add util methods to create model from TypedDict or NamedTuple

* refactor: rename into typeddict and namedtuple

* test: add utils tests

* chore: fix lint

* chore: improve test

* refactor: rename utils to match the rest

* chore: update change

* docs: add section for create_model_from_{namedtuple,typeddict}

* refactor: rename typed_dict/named_tuple

* feat: support schema with TypedDict

* feat: support schema for NamedTuple

* feat: add json support for NamedTuple

* chore: rewording

* refactor: use parse_obj

* fix: add check for max items in tuple

* docs: separate typing.NamedTuple and collections.namedtuple
2021-02-13 10:05:57 +00:00
Michael (Misha) Salim dcc00beaf0 Helpful TypeError when user forgets to return values in root valida… (#2209)
* Helpful TypeError when user forgets to return `values` in root validator.

* add changelog

* Handle more generic TypeError

* change exception message

* Update 2209-masalim2.md

* remove unused comment, improve change description

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2021-02-12 13:10:10 +00:00
Eric Jolibois f11b3ae313 feat: avoid reconstructing models used as fields of another model on validation (#2193)
* feat: avoid reconstructing models used as fields of another model on validation

closes #265
closes #1837

* docs: add section to change config

* change default value to avoid breaking change

* refactor: only suggest custom BaseModel approach
2021-02-12 13:04:52 +00:00
Eyitayo Ogunbiyi 61b45340e7 Feature/add anystr lower to config (#2183)
* added anystr_lower field to config

* updated docs with any anystr_lower config property

* updated changes folder with markdown describing new change

* added anystr to _validators list under bytes type

* added unit tests for anystr_lower config

* Update pydantic/validators.py

Fix wrong method used in cosntr_lower

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

* added lower_str to conbytes and constr functions

* Update changes/2134-tayoogunbiyi.md

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

* renamed conbytes & constr paramater name

* updated change markdown

* added docs for args to constr and conbytes

* added examples to showing to_lower functionality in conbytes & constr

* added better description for curtail_length

* removed unessary whitespace, added default and types in docs

* Update changes/2134-tayoogunbiyi.md

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

* added single whitespace to arguments list under constr & conbytes

Co-authored-by: Eric Jolibois <em.jolibois@gmail.com>
Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2021-02-12 12:28:08 +00:00
Moritz E. Beber a1ac464371 Add a frozenset type annotation to allowed_schemes on stricturl (#2198)
* docs: add changelog entry

* chore: add a frozenset type annotation
2021-02-11 17:56:36 +00:00
Justin Sexton 688107ec8e Fixed KeyError when creating schema from models with same names declared within different classes (#2170)
* Fixed issue causing KeyError to be raised when building schema from multiple `BaseModel` with the same names declared in separate classes (#1912)

* Update test

* Removed redundant f-string

* Fixed formatting

* Fixed lint
2021-02-11 17:39:59 +00:00
Thomas Berdy 1155de82b9 feat: Add the ability to add extra settings sources (#2107)
* feat: Add the ability to add extra settings sources

* doc: Document "customise settings sources" feature

* tests: Add missing test and add change file

* Update changes/2107-kozlek.md

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

* improve docs for settings customise_sources

* fix docs building

* fix test :-(

Co-authored-by: Thomas Berdy <thomas.berdy@outlook.com>
Co-authored-by: Eric Jolibois <em.jolibois@gmail.com>
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2021-02-11 16:55:40 +00:00
Zac Hatfield-Dodds 771b0d3d92 Add a Hypothesis plugin (#2097)
* Configure Hypothesis

* Hypothesis plugin docs

* Add Hypothesis plugin

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2021-02-11 12:33:11 +00:00