Commit Graph

269 Commits

Author SHA1 Message Date
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
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
Eric Jolibois 13a5c7d676 fix(schema): fix schema generation with multiple Enums having the same name (#2226)
closes #1857
2021-01-02 09:33:36 +00:00
Eric Jolibois 43308d1b24 fix: force need of update_forward_refs in recursive models (#2221)
* fix: force need of `update_forward_refs` in recursive models

closes #1201

* fix: lint issue (false positive)

https://github.com/PyCQA/pyflakes/issues/567

* chore: avoid if statement for coverage

* fix linting

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2021-01-02 08:38:13 +00:00
Eric Jolibois 8d7e0b86f3 fix: do not ignore annotated fields when type is Union[Type[...], ...] (#2214)
closes #2213
2021-01-01 19:20:45 +00:00
Eric Jolibois 80175f3628 fix: ensure to always return one of the values in Literal field type (#2181)
* fix: ensure to always return one of the values in `Literal` field type

closes #2166

* perf: improve `literal_validator` speed

Thanks to @yobiscus

* fix: when more options in Literal

switch from `set` to `dict` to still have a O(1) complexity
Thanks @layday :)
2021-01-01 15:15:38 +00:00
Andrew M. White 7af90a8d6c Fix url fragment without query (#2169)
* fix: parse URL fragment without query string (#2168)

* Add changes file
2021-01-01 14:22:15 +00:00
Eric Jolibois 1c3ef841e6 feat: add validator for None, NoneType or Literal[None] (#2149)
* feat: add validator for NoneType

closes #2095

* feat: support NoneType directly as type

* chore: add change file

* chore: fix typo

* fix: handle Literal[None] for python3.6/3.7 without typing_extensions

* chore: move NotNoneError and export it

* chore: use constant NONE_TYPES to simplify a bit

* chore: use check on Literal instead of typing_extensions

These tests would not run on python 3.8+ without typing_extensions
even though Literal is defined

* test: use fresh Literal[None] in test case

* feat: handle `None` type in schema

* docs: add NoneType documentation

* refactor: switch NONE_TYPES from tuple to set
2021-01-01 14:01:25 +00:00
Eric Jolibois 8bad7bc911 feat: add validate method on function to validate input without calling it (#2128)
* feat: add `validate` method on function to validate input without calling it

* docs: add documentation

* chore: add change file
2020-12-31 17:45:51 +00:00
Edward Betts 58a60707b4 Use % for percentage in string format (#1960)
* Use % for percentage in string format

* add change

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2020-12-30 18:33:28 +00:00
Davis Kirkendall 6b8d2babaa Fix #947, #1483, #1247: allow inner type vars to be present in parent generic classes (#1989)
* Fix #947, fix #1483, fix #1247 allow inner type vars to be present in parent generic classes

- Rename generics._is_type to _is_generic in response to comment:
  https://github.com/samuelcolvin/pydantic/pull/1989#discussion_r503400391
- Add more explicit type assertion in generics test
- Add generics tests and unify naming
- Move deep generic tests all into same place in code
- Unify naming convention in deep generic tests using naming of existing tests
- Add tests for multiple inheritance and multiple type vars
- Add generic tests for type hint resolution cases
- Fix edge cases for handling parameters in generic models
- Resolve parameters correctly
- Add tests for special cases like callables
- Add returning generic type directly if parameters and arguments are
  identical.

* Apply review comments

 - Add docstrings
 - Small refactor `generics.__concrete_name__`
 - Small refactor of `generics.resolve_type_hints`
 - Change `is_identity_typevars_map` to more generic and clearer
  `all_identical` and move into utils.
 - rename "resolve_type_hint" to "replace_types" so as to not get
   confused with "resolve_annotations" from `pydantic.typing`

* Fix generics test coverage

* Update pydantic/generics.py, remove unneeded annotation

mypy understands list comprehensions as they are now

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

Co-authored-by: Eric Jolibois <em.jolibois@gmail.com>
2020-12-30 18:23:55 +00:00
Mykola Solodukha aacf592040 Add rediss (Redis over SSL) protocol to RedisDsn (#1911)
* Add `rediss` (Redis over SSL) protocol to `RedisDsn`

* Update docs

* Update changes history

* Fix typo in `changes/1911-TrDex.md`

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

* Bump black from 19.10b0 to 20.8b1 (#1909)

* Bump black from 19.10b0 to 20.8b1

Bumps [black](https://github.com/psf/black) from 19.10b0 to 20.8b1.
- [Release notes](https://github.com/psf/black/releases)
- [Changelog](https://github.com/psf/black/blob/master/CHANGES.md)
- [Commits](https://github.com/psf/black/commits)

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

* fix: run `make format`

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: PrettyWood <em.jolibois@gmail.com>

* Allow URLs without `user` part

Refer to #1877

* Update docs

* Allow Redis DSN with schema only

Refer to https://github.com/samuelcolvin/pydantic/pull/1911#issuecomment-722335692
Refer to https://www.iana.org/assignments/uri-schemes/prov/redis

* Fix lint error

* Fix lint error

* Set `parts: Dict[str, Optional[str]]` instead of `Dict[str, str]`

* Fix linting

* More verbose default values set in `RedisDsn.validate_parts()`

* Fix linting 2

* Fix typo in docs

* Add a note in the changelog

* Add test case for URL without `user` part

* change port in test

Co-authored-by: PrettyWood <em.jolibois@gmail.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2020-12-30 18:20:51 +00:00
Rick Izzo 465f267c7d Added StrictBytes type (#2136)
* Added StrictBytes type

* updated tests for StrictBytes, updated relevant sections in documentation for StrictBytes and ConstrainedBytes

* added changelog

* chore: typo in change

Co-authored-by: Eric Jolibois <em.jolibois@gmail.com>
2020-12-01 01:18:35 +01:00
aimestereo 35fde4e216 Fix #2111: support pickle for built-in dataclasses (#2114)
* 2111: support pickle for built-in dataclasses

* 2111: add changes

* 2111: simplify test

* return original name + handle similar names

* add additional check

* fix a misspell

* remove useless f-string

* cleanup test

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2020-11-30 19:28:14 +00:00
Eric Jolibois 4169b1e2fa fix: support plain typing.Tuple (#2133)
closes #2132
2020-11-30 17:59:46 +00:00
Eric Jolibois c0ac53b757 fix: set right default value for required (optional) fields (#2143)
closes #2142
2020-11-30 17:55:50 +00:00
Eric Jolibois a82a411d60 fix: support underscore_attrs_are_private with generic models (#2139)
closes #2138
2020-11-30 17:54:07 +00:00
Eric Jolibois 1a2791d422 fix: update all modified field values in root_validator when validate_assignment is on (#2119)
* fix: update all modified field values in `root_validator` when `validate_assignment` is on

closes #2116

* chore: update root_validator name

Co-authored-by: Arthur Pastel <arthur.pastel@gmail.com>

Co-authored-by: Arthur Pastel <arthur.pastel@gmail.com>
2020-11-30 00:32:43 +00:00
Ben Martineau 0b7a570afd Fixes a regression where Enum fields would not propagate keyword arguments to the schema (#2109)
fix #2108

* Fix schema extra not being included when field type is Enum

* Code format

* More code format

* Add changes file

Co-authored-by: Ben Martineau <b.martineau@iwoca.co.uk>
2020-11-29 23:57:42 +00:00
Yurii Karabas 5506d8ec19 Update type annotation for create_model function (#2071)
* Update type annotation for create_model function

* Remove redundant __base__ cast
2020-11-29 17:34:12 +00:00
Eric Jolibois 4020ebc412 feat: add default encoder for Pattern type (#2056)
closes #2045
2020-11-29 17:20:43 +00:00
Matthew Davis 9cb7ca744b add NonPositive/NonNegative Int/Float (#1987)
fix #1975

* add NonPositive/NonNegative Int/Float

* delete data.json file
2020-11-29 16:07:51 +00:00
Eric Jolibois 31bc2435d7 fix: ignore __doc__ as valid private attribute (#2091)
closes #2090
2020-11-04 23:11:13 +00:00
Samuel Colvin 6853032372 prepare for v1.7.2 release 2020-11-01 15:30:36 +00:00
Arseny Boykov 4a094477c6 Fix generics creation time and allow model name reusing (#2078)
* preserve progress

* make get_caller_module_name much faster
combine get_caller_module_name and is_call_from_module in get_caller_frame_info

* fix coverage

* add changes file
2020-10-31 23:37:03 +00:00
Eric Jolibois 4680940146 fix: keep the order of the fields when validate_assignment is set (#2075)
Following #2000 and #2046 we can't `pop` the current value when assigning
a new one (which was probably the most efficient) as we want to keep the
order in the `__dict__`.
So let's do a shallow copy of the `__dict__` without the current value

fix #2073
2020-10-31 18:47:22 +00:00
Eric Jolibois b3f7b28f13 fix: forward all the params of the stdlib dataclass when converted into _pydantic_ dataclass (#2066)
* fix: forward all the params of the stdlib `dataclass` when converted into _pydantic_ `dataclass`

closes #2065

* add some documentation
2020-10-31 18:45:20 +00:00
Samuel Colvin 4fb800337b uprev and create history 2020-10-28 19:24:46 +00:00
John Sabath 6b1a0bff54 Prevent fields from being deleted from BaseModel.__dict__ when a field validator raises an unexpected exception (#2046)
* don't pop the value off __dict__

* adding change description

* still pop the field, but catch any exc, set the field to og value, and reraise exc

* adding tests

* fixing test

* improve tests/test_validators.py tests

Co-authored-by: John Sabath <john.sabath@equipmentshare.com>
Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
2020-10-28 19:21:26 +00:00
Eric Jolibois 0b9cd4e537 fix: pydantic dataclass can inherit from stdlib dataclass and arbitrary_types_allowed is supported (#2051)
* fix: pydantic dataclasses can inherit from stdlib dataclasses

closes #2042

* docs: add some documentation

* fix: support arbitrary_types_allowed with stdlib dataclass

closes #2054

* docs: add documentation for custom types
2020-10-28 19:10:55 +00:00
layday e899692654 Add two overload variants to validate_arguments (#2055)
* Add two overload variants to `validate_arguments`

Fixes #2052.

* Add change file

* Move decorator tests to success.py

* Reword changelog entry
2020-10-28 19:09:24 +00:00
Samuel Colvin 73ed6ed5cb underscore_attrs_are_private causing TypeError (#2053)
* underscore_attrs_are_private causing TypeError, fix #2047

* remove repeat and order alphabetically
2020-10-28 10:19:21 +00:00
Alex Hedges 8aad3a2f58 Refactor PrivateAttr to type-check like Field (#2057)
* Refactor PrivateAttr to type-check like Field

* Change TypeError to ValueError for consistency

* Add PrivateAttr example to mypy tests
2020-10-28 10:17:45 +00:00
Samuel Colvin 95435de452 build history and uprev version (#2040)
* build history and uprev version

* tweak history

* add #744 to changelog

* bump
2020-10-26 11:31:10 +00:00
PrettyWood c83156d0e0 feat: make pydantic dataclass decorator support built-in dataclass (#1817)
* feat: pydantic dataclasses support built-in ones

closes #744

* feat: improve dataclass typing

* feat: add support for nested dataclasses

closes #1743

* feat: support dataclass schema with nested dataclasses

* refactor: remove `_dataclass_with_validation` function

* docs: add docstring for `make_dataclass_validator`

* refactor: rename DataclassType into Dataclass

The name `DataclassType` was missleading as it's not a `Type` per say.

* refactor: change global `dataclass` import to local

pydantic import time was improved in
https://github.com/samuelcolvin/pydantic/pull/1132
by keeping `dataclass` import local. So let's keep it that way!

* test: add extra nested case with BaseModel

* chore: s/pydantic/_pydantic_/g

* docs: add some documentation
2020-10-26 11:09:28 +00:00
mdgilene 30cd121cfe Add ability to read secrets from files (#1820)
* Add ability to read secrets from files

* Added docs and updated tests to handle new _build_values argument

* Updated docs to note which types are loaded

* Updated deep_update to take a list of update mappings

* Added warning when fields are not defined as a secret type

* Address additional PR feedback

* improve documentation

* remove redundant UserWarning lines

* allow any field type, not just SecretStr

* regex escape paths in windows 😴

* skip tests on windows 😧 🔫

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2020-10-26 10:06:25 +00:00
Arseny Boykov 664cbcfc01 Add private attributes support (#1679)
* Add private attributes support

* Add more blank lines in example

* Add changes file

* Update docs/usage/models.md

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

* fix after bad merge

* Add PrivateAttr, Config.underscore_attrs_are_private

* remove unrelated change in utils.py

* add   # noqa: C901 (ignore complexity) to __setattr__
(see comment in PR)

* add annotation to Config.underscore_attrs_are_private

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

* use sunder names

* mention underscore_attrs_are_private in model_config.md

* add comment about default factory

* fix comment

* fix comment

* clarify that both dunder and sunder names might be used

* tweak docs and name

* _set_default_private_attributes -> _init_private_attributes

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

* use new name _init_private_attributes

* move tests

* copy private attributes in BaseModel.copy()

* add test for default and default_factory used together

* fix linting

* more tests, default_factory kw only

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2020-10-26 09:47:40 +00:00
Chris Hoogeboom 0af5e5f559 Allow generics to extend concrete classes. fixes #2005 (#2006)
* Allow generics to extend concrete classes. fixes #2005

* Update the docs

* Updote the changes

* Convert double quotes to single quotes

* Fix formatting

* Add a check for data

* Update example

* Add a skip_36 decorator that got accidentally deleted when resolving conflicts.
2020-10-25 19:22:25 +00:00