Commit Graph

243 Commits

Author SHA1 Message Date
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
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
PrettyWood 1736ea1e70 fix(validation): also check root_validators when validate_assignment is on (#1972)
closes #1971
2020-10-25 18:02:14 +00:00
Tyler Wozniak 3e4c1b5714 Adding deque to valid field types (#1935)
* Adding deque to valid field types

* Added missing type example

* Fix bad copy-paste

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

* correct enum validator name

* correct enum validator name, take2

Co-authored-by: PrettyWood <em.jolibois@gmail.com>
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2020-10-25 17:52:34 +00:00
Noel Evans 8b2f299fc5 Fix representation of literal enums (#1747) (#1838)
* Fix representation of literal enums (#1747)

Use existing is_literal_type

Skip when typing_extensions is not present

Improvements proposed by PrettyWood reviewing PR

* Trim unnecessary logic

* Clarify behaviour

* extend test slightly

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2020-10-25 16:48:03 +00:00
Gabriel d14731f16c schema ref_template (#1480)
* ignore Pipfile, .lock files

* add ref_template option

use a string.Template instead of a ref_prefix to allow for more varied`$ref`s to be created.
Template string is expected to have $model_name `identifier `

* formatting / linting

* add changes

* typo

* use string.format instead of string.Template

* remove ref_prefix default

if no `ref_prefix` provided, use the `template_default`

* use ref_template in field_singleton_schema

* fix test_schema_with_ref_template

* add parameters for `test_schema_with_refs`

test name change 
test for key error

* provide ref_template default argument

* fix linting

* fix mypy and coverage

* docs and correct model_schema usage

* fastAPI tests actualy caught an error

* linting

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2020-10-25 16:24:37 +00:00
Noel Evans 5da8b9cded Parse time with tz (#1814)
* Parse timezone information in time str

* Add description of change fixing issue 1744

* Guard against TZ over 24 hours

* Parse timezone information in time str

* Datetime and time TZ parsed in separate fn

* Sharpen annotation, many return statements

* Fix mypy errors raised on datetime_parse

* fix type hints

* update docs and correct types

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2020-10-25 16:08:38 +00:00
PrettyWood 4bc4230df6 feat: support default_factory with BaseModel.construct (#1755)
* feat: support `default_factory` with `BaseModel.construct`

closes #1732

* refactor: remove __field_defaults__

* docs: update change with `__field_defaults__` deletion
2020-10-25 15:41:57 +00:00
Sergey Teplyakov 52aa5c456f Add init for forward ref stub (#1739)
* Add __init__ for ForwardRef stub

* Add changes file

* Update changes/1738-sirtelemak.md

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

* Add mypy check for ForwardRef

* Add Black line after if statement

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
Co-authored-by: Sergey Teplyakov <s.teplyakov@fitingo.com>
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2020-10-25 15:36:38 +00:00
Samuel Colvin a25aba26b5 linting as a separate CI step (#2030)
* linting as a separate ci step

* CI and docs improvements

* fix docs preview

* fix docs, benchmarks fastapi tests

* fix formatting and docs build

* tweak publishing docs

* fix for new download-artifact action

* skip check tag to check docs build

* prepare for merge
2020-10-25 15:03:33 +00:00
Arseny Boykov 6b53cabe03 Add pickle support to dynamically created models and generics (#1686)
* Add pickle support to dynamically created models

* Add created model module check

* Use globals() to retrieve model

* Use single quotes

* use mocker instead of mock

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

* remove unused import

* add test for dynamic forward ref

* move generic test to test_generics.py

* fix test_generic_model_pickle test, add additional check for <locals> in create_model

* fix code style

* resolve issues with global/local models

* make ensure_picklable return given model

* remove ensure_picklable, use test_is_call_from_module only in generics

* reformat code after updating black==20.8b1

* move get_caller_module_name and is_call_from_module to generics

* apply suggestions from @samuelcolvin

* add tests for get_caller_module and is_call_from_module called from module

* fix linting

* fix path to modules in test_module fixture, capture stderr and stdout

* fix broken test module

* fix subprocess call for windows

* enhance create_module fixture, add run_as_module fixture

this will allow run modules in subprocess and define module code in functions-containers

* add test for redefined concrete model without cache

* add changes file

* remove unused variable

* rewrite tests with pytest.raises

* fix linting

* rewrite test_create_model_pickle and test_forward_ref_with_create_model with module definition in function

* Update create_model docstring

Co-authored-by: PrettyWood <em.jolibois@gmail.com>
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2020-10-18 20:55:44 +01:00
PrettyWood ab671a3670 feat(field): add Enum and IntEnum as valid types for fields (#1737)
* feat(field): add Enum type support

closes #1735

* test: add extra cases and rename to be more explicit

* refactor: rename enum_validator

* feat: add Enum and IntEnum types support

* docs: add new types

* chore: update change message
2020-10-18 20:53:29 +01:00
PrettyWood 794d0bccf9 feat: add basic support for python 3.9 (#1844)
* feat: use partially get_origin and get_args from typing

Rework of d86a184072655d93652d6ea941421005f34de962 by @hmvp
We cannot directly use `typing.get_origin` and `typing.get_args` for
our custom types for which args and origin are wrong by default.
But at least we now have a unified behaviour, which can be improved
in the near future

* feat: add fallback in `get_args` for generics in python 3.9

* feat: add new `_evaluate` for python 3.9

In python 3.9, `typing._evaluate` has a new required
positional argument `recursive_guard`

* chore: add pragma no cover for python 3.9 fallbacks

* chore: add change file

* chore: add comments

* feat(ci): add python 3.9 support

* fix: mypy assumption is wrong
2020-10-18 20:36:18 +01:00
PrettyWood 3aacec4e17 feat(schema): support custom title, description and default for enums (#1749)
* refactor(schema): put schema data from fieldinfo in dedicated function

* feat(schema): support custom title, description and default for enums

closes #1748

* refactor: replace $ref by allOf + $ref to be supported by doc generation tools

* fix: do not set title by default for enums

* refactor: make code more explicit

* fix: run linter
2020-10-18 20:23:06 +01:00
Louis Sautier e8326f899e Include tests in source distributions (#1976) 2020-10-09 14:32:27 +01:00
Hmvp c6a24f28c5 Fix const validator not running when class validators are present (#1957) 2020-10-09 13:26:11 +01:00
Davis Kirkendall 30ee8e2203 Force fields.Undefined to be a singleton objectIn various places of the code, we compare directly to fields.Undefinedsince we assume it to be constant.When new models get created however, the object is deepcopied andis no longer identical with the original object.We therefore add __copy__ and __deepcopy__ methods to ensurethat the copied objects are actually the same original object. (#1981) 2020-10-09 13:25:24 +01:00
Brian Maissy a89c97a3e3 add a __call__ stub to PyObject for mypy (#1849)
Co-authored-by: Brian Maissy <brian@medigate.io>
2020-10-09 12:11:23 +01:00
Geon Kim - KimMachineGun be8ef63a33 docs: Fix incorrect description of copy method (#1821)
* docs: Fix typo

* docs: Fix incorrect description of copy method

* changes: Add changes description file

* docs: Add space before parentheses

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

Co-authored-by: PrettyWood <em.jolibois@gmail.com>
2020-10-09 11:28:04 +01:00
PrettyWood 75859a9d58 feat(tools): add parse_raw_as util (#1813)
closes #1812
2020-10-09 10:35:07 +01:00
PrettyWood 76fdbe92be feat(dotenv): support home directory relative paths (e.g. ~/.env) (#1804)
closes #1803

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2020-10-09 10:23:28 +01:00
Matthew Davis 094da94da9 clarify argument type to parse_file (#1795)
Co-authored-by: Matthew Davis <Matthew.Davis.2@team.telstra.com>
2020-10-08 21:10:09 +01:00
Selim Belhaouane b99c37e3d0 Fix #1770 (#1771) 2020-10-08 21:07:22 +01:00
PrettyWood 55e78cc106 fix(schema): add basic support of Pattern type in schema generation (#1768)
closes #1767
2020-10-08 20:42:14 +01:00
PrettyWood a2fc01a59b fix: forward ref with nested models and optional fields (#1752)
* fix: forward ref with nested models and optional fields

PR #1712 introduced a regression for forward refs in `ModelField.prepare`
as it would not return early for forward refs anymore.
Optional fields would hence have `required` set to `True`.

closes #1736

* test: skip python 3.6 as __future__.annotations is not defined
2020-10-08 20:26:30 +01:00
Samuel Colvin 9c4860ce96 Valdiate arguments config (#1663)
* add `configs` to validate_arguments

* simplify `validate_arguments` and add annotation for parameter `configs`

* change double quotes to single quotes

* reformat code

* fix mypy error

* fix mypy 'maximum semantic analysis' error

* rename 'configs' > 'config_params'

* change name and usage, start tests

* prevent setting fields on custom config

* add docs and fix mypy

* tweak docs

* add change

Co-authored-by: quantpy <quantpy@qq.com>
2020-09-06 23:17:52 +01:00