Commit Graph

314 Commits

Author SHA1 Message Date
Søren Bjerregaard Vrist 08fc8302cf Add lookahead for ip check in AnyUrl (#2512) 2021-05-09 10:49:19 +01:00
Eric Jolibois 9cc19e9a8e fix: support arbitrary types with custom __eq__ (#2502) 2021-05-09 10:44:53 +01:00
Koudai Aono 07908b3846 Add "strict" argument to "conbytes" (#2490)
* Add "strict" argument to "conbytes"

* Add unittest

* Update changes/2489-koxudaxi.md

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

Co-authored-by: Eric Jolibois <em.jolibois@gmail.com>
2021-05-09 10:27:29 +01:00
Alexandre Gerona 9f654a1fb2 fix(encoder): serialize NameEmail to str (#2479) 2021-05-09 10:26:02 +01:00
krisaoe 42395056e1 Fix(docs): remove colon to separate unrelated clauses (#2733)
* Fix(docs): remove colon to separate unrelated clauses 

The colon at the end of the line "The fields which were supplied when user was initialised:" suggests that the code following it is related.
Changed it to a period.

* Include change summary readme.
2021-05-03 17:46:37 +01:00
Davis Kirkendall db697cc799 Add exclude as field parameter (#2231)
* Add exclude/include as field parameters

- Add "exclude" / "include" as a field parameter so that it can be
  configured using model config (or fields) instead of purely at
  `.dict` / `.json` export time.
- Unify merging logic of advanced include/exclude fields
- Add tests for merging logic and field/config exclude/include params
- Closes #660

* Precompute include/exclude fields for class

* Increase test coverage
* Remove (now) redundant type checks in Model._iter: New
  exclusion/inclusion algorithms guarantee that no sets are passed further down.

* Add docs for advanced field level exclude/include settings

* Minimal optimization for simple exclude/include export

Running benchmarks this vs. master is at:

this: pydantic best=33.225μs/iter avg=33.940μs/iter stdev=1.120μs/iter version=1.7.3
master: pydantic best=32.901μs/iter avg=33.276μs/iter stdev=0.242μs/iter version=1.7.3

* Apply review comments on exclude/enclude field arguments

* Fix/simplify type annotations
* Allow both ``True`` and ``Ellipsis`` to be used to indicate full field
  exclusion
* Reenable hypothesis plugin (removed by mistake)
* Update advanced include/include docs to use ``True`` instead of ``...``

* Move field info exclude/include updates into FieldInfo class

This way, the model field object does not need to concern itself with
dealing with field into specific fields.
(Same was done for alias in a previous commit).

* remove double back tick in markdown.

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
2021-05-01 16:45:37 +01:00
Samuel Colvin 908257914c docs preview and coverage using smokeshow (#2580)
* docs preview using smokeshow

* switch to using smokeshow for coverage

* fix macos and windows builds

* correct coverage prep.

* tweak coverage build logic

* print more coverage info

* mess with coverage setup, set config [PATHS]

* updating badges

* add context to coverage reports

* adding smokeshow status context
2021-03-27 18:36:52 +00:00
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