Commit Graph

992 Commits

Author SHA1 Message Date
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
Eric Jolibois 7cc8d254e9 disable hypothesis health check to avoid false negative (#2389) 2021-02-23 00:44:28 +01: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
Zac Hatfield-Dodds 5b147065a3 build(deps): bump hypothesis from 5.44.0 to 6.3.0 (#2388)
* build(deps): bump hypothesis from 5.44.0 to 6.3.0

Bumps [hypothesis](https://github.com/HypothesisWorks/hypothesis) from 5.44.0 to 6.3.0.
- [Release notes](https://github.com/HypothesisWorks/hypothesis/releases)
- [Commits](https://github.com/HypothesisWorks/hypothesis/compare/hypothesis-python-5.44.0...hypothesis-python-6.3.0)

Signed-off-by: dependabot[bot] <support@github.com>

* Tweak Hypothesis / Json

Fixing a tricky interaction around priority with subclassing and metaclasses.

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2021-02-22 15:39:45 +00:00
dependabot[bot] 53cf3cb645 build(deps): bump cython from 0.29.21 to 0.29.22 (#2385)
Bumps [cython](https://github.com/cython/cython) from 0.29.21 to 0.29.22.
- [Release notes](https://github.com/cython/cython/releases)
- [Changelog](https://github.com/cython/cython/blob/master/CHANGES.rst)
- [Commits](https://github.com/cython/cython/compare/0.29.21...0.29.22)

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2021-02-22 15:38:29 +00:00
dependabot[bot] 1e98d1671b build(deps): bump mypy from 0.800 to 0.812 (#2386)
Bumps [mypy](https://github.com/python/mypy) from 0.800 to 0.812.
- [Release notes](https://github.com/python/mypy/releases)
- [Commits](https://github.com/python/mypy/compare/v0.800...v0.812)

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2021-02-22 15:38:20 +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 c33ced6441 set categories on links in issue chooser 2021-02-17 10:38:03 +00:00
Huon Wilson 88aa92de70 Include new GitHub Discussions in contributing docs (#2371) 2021-02-17 10:35:24 +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
Eric Jolibois cc3010c80d chore(deps-dev): fix typing-extensions version (#2361) 2021-02-13 22:54:15 +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 61bdba37b9 fix ci by ensuring order of created list (#2352) 2021-02-13 12:29:00 +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
Samuel Colvin 502570706a use github discussions for questions and feature requests 2021-02-12 19:13:06 +00:00
Samuel Colvin 20fd949f40 allow test_inherited_model_field_untouched to fail 2021-02-12 17:59:39 +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
Samuel Colvin 1ee9737cf5 make hypothesis optional for testing (#2343)
* make hypothesis optional for testing

* fix coveragin in "_hypothesis_plugin.py"

* uncomment ci checks
2021-02-12 10:46:34 +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
Eric Jolibois d0baf0f156 fix(fields): handle properly default value for type Callable (#2094)
* fix(fields): handle properly default value for type `Callable`

closes #1596

* chore: move test in test_dataclasses

* chore: add comments

* test: rewrite test

* chore: remove useless variable

* fix(schema): add support when callable field has default value

closes #2086
2021-02-11 11:39:09 +00:00
Eric Jolibois f05bdb732f fix: check only first sublevel for validators with each_item (#1991)
* fix: check only first sublevel for validators with `each_item`

When using a validator with `each_item`, the items are all validated
one by one. But if the items are also iterable the subitems would then
be validated because the validator would be kept as it is.
Now the validator passed to the items is changed and won't be propagated

closes #1933

* chore: add breaking change
2021-02-11 11:23:31 +00:00