Commit Graph

928 Commits

Author SHA1 Message Date
Samuel Colvin 2f439eff0d v1.7 patch (currently v1.7.3) (#2161)
* fix: ignore `__doc__` as valid private attribute (#2091)

closes #2090

* 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>

* 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>

* fix: support `underscore_attrs_are_private` with generic models (#2139)

closes #2138

* fix: set right default value for required (optional) fields (#2143)

closes #2142

* 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>

* uprev and add history

* tempoarily pin pip

Co-authored-by: Eric Jolibois <em.jolibois@gmail.com>
Co-authored-by: Ben Martineau <bm424@cam.ac.uk>
Co-authored-by: Ben Martineau <b.martineau@iwoca.co.uk>
Co-authored-by: Arthur Pastel <arthur.pastel@gmail.com>
Co-authored-by: aimestereo <aimestereo@gmail.com>
2021-01-01 14:19:24 +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
Eric Jolibois 3496a473c7 build(deps): bump to pytest 6.2.1, pytest-mock 3.4.0, mkdocs-material 6.1.7 and codecov-action v1.0.15 (#2201)
* build(deps): bump to pytest 6.2.1, pytest-mock 3.4.0, mkdocs-material 6.1.7 and codecov-action v1.0.15

replaces #2199, #2180 and #2164

* fix: yield_fixture is deprecated

* chore: add dotenv in optional deps
2020-12-17 23:28:07 +01:00
Eric Jolibois 09a5e28fc7 ci: fix it by pinning importlib-metadata (#2202)
`importlib-metadata` now requires python3.8+ or `typing-extensions` since [3.2.0](https://github.com/python/importlib_metadata/commit/530f5da8dd3a255f1198f29ea0126b8b25e644d8#diff-cc51f8e0c17159a1860664f70a89eba0d48c641f4deafe74912a7274dff91a34R15)
And `pytest` doesn't pin the version of `importlib-metadata`.
So on the CI when we uninstall `typing-extensions` **after** installing `pytest`, it crashes!
Let's pin `importlib-metadata` to 3.1.0 for now
2020-12-17 23:09:19 +01:00
Samuel Colvin de0657e4a5 tempoarily pin pip 2020-12-01 10:26:49 +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 2a82bd7ee3 chore: fix contributing steps (#2144)
Following #2030, the requirements to build the documentation are not installed
with the rest making `make docs` crash when following the contributing steps.
As `make install` is only used for this purpose (easy onboarding), let's just add
again the installation of the doc requirements with it
2020-11-30 18:02:36 +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
oridistor 223744da9f Updated validators.md with the type of field (#2110)
As someone started using pydantic, it can be very convenient to know what type we can rely the returned field is, so we know what we can check on it and what fields it has.
After searching the code I found out it has to be of type ModelField and that's why I'm offering it here as something that I think should be clarified in the documentation.
2020-11-30 00:01:02 +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
Samuel Colvin 7c3919ce4d combined version bumps (#2153) 2020-11-29 17:57:29 +00:00
dependabot[bot] 7cbab2bfad Bump email-validator from 1.1.1 to 1.1.2 (#2102)
Bumps [email-validator](https://github.com/JoshData/python-email-validator) from 1.1.1 to 1.1.2.
- [Release notes](https://github.com/JoshData/python-email-validator/releases)
- [Commits](https://github.com/JoshData/python-email-validator/compare/v1.1.1...v1.1.2)

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

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2020-11-29 17:40:01 +00:00
dependabot[bot] 3461ad96ba Bump python-dotenv from 0.14.0 to 0.15.0 (#2083)
Bumps [python-dotenv](https://github.com/theskumar/python-dotenv) from 0.14.0 to 0.15.0.
- [Release notes](https://github.com/theskumar/python-dotenv/releases)
- [Changelog](https://github.com/theskumar/python-dotenv/blob/master/CHANGELOG.md)
- [Commits](https://github.com/theskumar/python-dotenv/compare/v0.14.0...v0.15.0)

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

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2020-11-29 17:37:11 +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
dependabot[bot] 6c50a86df4 Bump codecov/codecov-action from v1.0.13 to v1.0.14 (#2080)
Bumps [codecov/codecov-action](https://github.com/codecov/codecov-action) from v1.0.13 to v1.0.14.
- [Release notes](https://github.com/codecov/codecov-action/releases)
- [Commits](https://github.com/codecov/codecov-action/compare/v1.0.13...7d5dfa54903bd909319c580a00535b483d1efcf3)

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

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2020-11-01 15:25:40 +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
milo-minderbinder 9af8f11086 fix typo in example secret filename (#2074) 2020-10-31 18:41:18 +00:00
Samuel Colvin 8dcc87cf42 correct history 2020-10-28 19:26:46 +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
Joscha Götzer fb5ea74bba Update datamodel_code_generator.md (#2058) 2020-10-28 10:18:38 +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
Samuel Colvin 8b4e726be2 Fix typing coverage (#2032)
* fix coverage

* linting

* correct forward ref import

* fix conlist __args__

* chore: remove useless except

* skip test_get_args prior to 3.8

Co-authored-by: PrettyWood <em.jolibois@gmail.com>
2020-10-26 10:25:48 +00:00
Arseny Boykov 44b639951b add pytest assert rewrite to create_module, rewrite existing tests (#2035) 2020-10-26 10:12:49 +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
dependabot[bot] 52889203ac Bump devtools from 0.6.0 to 0.6.1 (#2037)
* Bump devtools from 0.6.0 to 0.6.1

Bumps [devtools](https://github.com/samuelcolvin/python-devtools) from 0.6.0 to 0.6.1.
- [Release notes](https://github.com/samuelcolvin/python-devtools/releases)
- [Changelog](https://github.com/samuelcolvin/python-devtools/blob/master/HISTORY.md)
- [Commits](https://github.com/samuelcolvin/python-devtools/compare/v0.6...v0.6.1)

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

* remove unnecessary dependencies

* tweaks while testing 3.9

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2020-10-26 09:53:28 +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
Arseny Boykov 2f7e40476f include assert in validator test in pytest (#2033)
* include assert in valudator test in pytest

* remove try_assert execution in Makefile

* use pytest.raises

* add newline

* tweak

* undo my mistake

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2020-10-25 19:50:38 +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
me-ransh f2af9f7806 Fix #1999: call validator with correct "values" type in setattr (#2000)
* call validator with correct "values" type in BaseModel.__setattr__

* Fix code review comments
- use `self.__dict__` as the values parameter
- refine changed markdown

Co-authored-by: Ran Shaham <ransh@mobileye.com>
2020-10-25 19:18:56 +00:00
Yurii Karabas 5bfee873c8 Add ability to use min_length and max_length constraints with secret types (#1974)
* Add ability to use min_length/max_length constraints with secret types

* Add change file

* Remove StrLike and BytestLike classes

* Add ability to define constraints for a custom class

* Fix issue with incorrect secrets schema generation

* Add schema test for SecretStr and SecretBytes

* Add test to cover valid secret length
2020-10-25 18:32:11 +00:00