Commit Graph

106 Commits

Author SHA1 Message Date
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 f9fe4aa086 fix: do not overwrite declared hash in subclasses of a model (#2423) 2021-02-27 15:42:53 +00:00
Samuel Colvin 2ee6811655 remove DeprecationWarnings from v1 release & fix coverage (#2415)
* remove DeprecationWarnings from v1 release

* coverage on unpickling Undefined private attributes

* coverage on undefined in copy, allow Undefined to be pickled unchanged

* fix coverage of model._iter()
2021-02-26 14:43:47 +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
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
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
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 b21da6f381 fix: support custom root type with nested models in parse_obj (#2238) 2021-02-13 10:18:54 +00:00
Samuel Colvin 20fd949f40 allow test_inherited_model_field_untouched to fail 2021-02-12 17:59:39 +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
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 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
PrettyWood 1736ea1e70 fix(validation): also check root_validators when validate_assignment is on (#1972)
closes #1971
2020-10-25 18:02:14 +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
Hmvp c6a24f28c5 Fix const validator not running when class validators are present (#1957) 2020-10-09 13:26:11 +01:00
PrettyWood e2fcab52b9 fix: validate and parse nested models properly with default_factory (#1712)
* fix: validate nested models with `default_factory`

PR #1504 introduced a regression by bypassing `populate_validators()`,
which would skip the validation of children in nested models
with `default_factory`

closes #1710

* test: add example of nested models parsing with `default_factory`

closes #1717

* add testcase from #1722

* bodge for benchmarks

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2020-07-15 20:23:18 +01:00
Spiros Dontas 1f4ecd0785 Fix nested all include exclude (#1588)
* Fix nested all include exclude

* Ensure original exclude value is not modified

This commit also fixes some weird cases in the recursive
`update_normalized_all` call and Ellipsis values.

* Add docstring, parametrize tests
2020-07-11 11:28:53 +01:00
Patrick Wang 5a2d78765a squash internal __root__ models in .dict() (#1607)
fix #1414

* flatten internal __root__ models

* add 1414 changes doc
2020-06-27 14:55:17 +01:00
PrettyWood c59db275dc fix(field): remove some side effects of default_factory (#1504)
* Avoid some side effects of default factory

- by calling it only once if possible (fix #1491)
- by not setting the default value in the schema (fix #1520)

* refactor: ensure type is set when using default_factory
2020-06-27 14:02:29 +01:00
PrettyWood 329b1d3e7b fix: type hints on BaseConfig (#1618)
closes #1614
2020-06-09 10:32:07 +01:00
John Carter 827388b4fe Add a test assertion that default_factory can return a singleton (#1523) 2020-05-31 14:51:50 +01:00
Samuel Colvin c8906ce810 uprev all test and docs dependencies (#1528) 2020-05-18 21:31:23 +01:00
Selim Belhaouane 881df8bde7 move const validator to post validators. fixes #1410 (#1446) 2020-04-30 19:05:42 +01:00
PrettyWood 3cd8b1ee2d Fix :: avoid mutating Field default value (#1413)
fix #1412
2020-04-22 11:25:13 +01:00
AlexECX 52af916206 Prevent mutation of excludes/includes (#1404)
* Don't mutate excludes/includes dictionaries inside ValueItems

* Make exludes/includes read-only

* Add changelog

* Update 1404-AlexECX.md

* Add test for includes/excludes immutability
2020-04-18 17:09:06 +01:00
PrettyWood 2c05415bae accept None as value for all optional fields (#1307)
* ignore pyenv generated file

* Allow `None` as input to all optional list fields

* skip test on python 3.6

* fix mypy error, line length

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2020-04-15 22:43:38 +01:00
Michael (Misha) Salim ebb2e6c942 Use __all__ to exclude fields from all elements of a list/tuple of submodels. Raise more informative TypeError when passing bad exclude kwargs. (#1286)
* Raise informative TypeError when passing bad exclude kwargs to export

* Cleanup spurious example

* Add __all__ keyword to enable excluding fields from all members of a list/tuple of child submodels

* Add changelog

* Fix MD format inconsistencies and consolidate export tests

* Excluding from exported List of Dict fields with "__all__"

* tweaks

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2020-03-18 20:22:49 +00:00
Pavel Ilyin d8262131d2 No dict update (#1244)
* update_forward_refs now modifies only a copy of __dict__ of cls.__module__

* changes

* test for update_forward_refs

* fixed brackets

* black changes fixed

* make format

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2020-03-17 19:57:13 +00:00
PrettyWood 78a3f42b0a Dynamic default value (#1210)
* Simplify dynamic default values

closes #866

* Add documentation and change

* Add `ModelField.get_default` method

* Fix default value in `infer`

Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
2020-03-04 16:37:23 +00:00
Samuel Colvin 943a8a06e5 change alias priority logic (#1178)
* fix alias priority so alias_generators don't take priority

* improve test names

* remove debugs

* Apply suggestions from code review

* more tests and allow custom alias_priority on fields

* precedence tests and docs

* tweaks and add change

* suggestions
2020-01-24 10:31:16 +00:00
dmontagu 62bc930f57 Add support for mapping types as custom root (#958)
* Add support for mapping types as custom root

* Incorporate feedback

* Add changes

* Incorporate feedback

* Add docs and tests

* Fix linting issue

* Incorporate more feedback

* Add more specific match
2019-11-25 12:19:41 +00:00
retnikt dccc4014dc Clarify documentation and error message about keep_untouched (#926)
* Clarify keep_untouched documentation (#924)

* Clarify error message for custom types (#924)

* Fix tests for changed error message (#924)

* fix formatting

* remove erroneous error message and add change
2019-10-23 11:14:51 +01:00
Samuel Colvin cb262daddd fix ValidationError.json() (#922) 2019-10-23 10:27:55 +01:00
dmontagu 6b5adcc977 Rename skip-defaults, and add ability to exclude actual defaults (#915)
* Rename skip-defaults, and add ability to exclude actual defaults

* Add __defaults__ and deprecation warnings

* Add note about `skip_defaults` to docs

* Incorporate feedback

* Add tests and changes

* Fix reference to .json()
2019-10-23 10:14:51 +01:00
Samuel Colvin 78921da353 better str and repr for ModelField (#912)
* better str and repr for ModelField, fix #505

* better type display, fix tests

* correct _type_display signature

* fix for python3.6 differences

* fix PyObjectStr

* fix coverage
2019-10-18 14:32:30 +01:00
Samuel Colvin c3098a30cf Consistent __repr__ and __str__ methods for all types (#884)
* Consistent __repr__ and __str__ methods for all types

* add change description

* devtools integration and feedback on repr methods

* fix Color repr

* tests for truncate

* add devtools section to docs

* tests for devtools

* ValidationError inheriting from Representation

* fix imports

* tweaks

* tweak docs

* exec_examples.py integration with __repr__ changes
2019-10-14 17:37:04 +01:00
Samuel Colvin d0c6ec7009 deprecate Model.fields for Model.__fields__ (#883)
* deprecate Model.fields, use Model.__fields__

* correct docs
2019-10-11 11:10:02 +01:00
retnikt 6a0e5313cb Fix #867 by accepting and passing on **kwargs in ModelMetaclass.__new__ (#868)
* Fix #867 by accepting and passing on **kwargs in ModelMetaclass.__new__

pydantic.main.MetaModel.__new__ should include **kwargs at the end of
the method definition and pass them on to the super call at the end in
order to allow the special method __init_subclass__ to be defined with
custom parameters on extended BaseModel classes.

* MetaModel -> ModelMetaclass

* shrink change notes

* rename and reformat change description
2019-10-08 11:10:43 +01:00
Samuel Colvin ae34a6a733 rename allow_population_by_alias -> allow_population_by_field_name (#875)
* rename allow_population_by_alias -> allow_population_by_field_name, fix #585

* add change

* tweak PR template ☣️

* tweak Makefile
2019-10-07 18:40:01 +01:00
Samuel Colvin 12f4e0d082 rename Schema to Field (#824)
* rename Schema to Field

* add Schema function with deprecation warning

* mypy tests and other tweaks

* tweaks and cleanup

* Update pydantic/fields.py

Co-Authored-By: Sebastián Ramírez <tiangolo@gmail.com>
2019-09-30 17:49:59 +01:00
Hmvp dd32a43814 Fix const validations (#794)
This fixes #620 and #793
2019-09-17 18:30:58 +01:00
Timon Ruban f08fd2fee7 Add support for Type[T] typehints when arbitrary_types_allowed==True. (#808)
* Add support for Type[T] typehints when arbitrary_types_allowe==True.

* Add documentation.

* Let black do its magic.

* Ignore mypy warning - see here: https://github.com/python/mypy/issues/3060

* Prettify docs.

* Change Changelog.

* Refactor and simplify check for Type[T].

* Black again. ^^ - Really need pre-commit hooks.

* Update pydantic/validators.py

Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>

* Rename arbitrary_class to class.

* Black.

* Add type hints.

* Make private function public.

* Add support for bare Type.

* Black again.

* Update docs.

* CO_ct not meant for export.

* Fix get_class for Python3.6

* Update error message of ClassError.

* Use relative import.

* Incorporate typing feedback (both versions are fine with mypy).

* Move from issubclass to lenient_issubclass.

* correct docs
2019-09-16 10:42:40 +01:00
dmontagu 321cde0c88 Include all annotated fields in order (#715)
* Include all annotated fields in order

* Update docs and changes

* fix field ordering

* update change info

* fix coverage
2019-08-12 12:05:22 +01:00
Samuel Colvin 82ef45c890 alter the behaviour of dict(model) so that sub-models are nolonger converted to dictionaries (#733)
* fix iteration to not convert to dict by default

* add change

* remove extra newline
2019-08-12 11:31:35 +01:00
Arseny Boykov fae3588f42 Add Config.keep_untouched for custom descriptors support (#679)
* Add Config.keep_untouched for custom descriptors support

* Update HISTORY.rst

* Separate test

* Fix tests

* Update pydantic/validators.py

Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>

* Update HISTORY.rst

Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>

* Rename TYPE_BLACKLIST -> UNTOUCHED_TYPES, fix tests and formatting

* Update docs/index.rst

Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>
2019-07-24 16:33:49 +01:00
Arseny Boykov 1b4c1a884c Fix alias_generator and field config conflict (#658)
* Fix alias_generator and field config conflict (see #645)

* HISTORY.rst update: fix alias_generator and field config conflict

* Merge HISTORY.rst with master
2019-07-24 10:32:40 +01:00
Koudai Aono e4b285a0cd support custom root types (#628)
* support custom root types

* update HISTORY.rst for costom root types

* fix invalid arguments case

* remove the keyword argument of "__root__" in __init__
add a keyword argument of "__root__" in parse_obj
fix documents
create a method for cheking valid field name

* fix parse_obj

* Update docs/index.rst

Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>

* Apply suggestions from code review

Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>

* add type cheking for custom root type

* Update docs/index.rst

Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>

* Update index.rst
2019-07-06 09:52:42 +01:00