* 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()
* 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
* 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>
* 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>
* 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`
* feat: avoid reconstructing models used as fields of another model on validation
closes#265closes#1837
* docs: add section to change config
* change default value to avoid breaking change
* refactor: only suggest custom BaseModel approach
* 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 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>
* 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>
* 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
* 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
* 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>
* 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>
* 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>
* 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
* 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
* 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()
* 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
* 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>
* 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
* Fix alias_generator and field config conflict (see #645)
* HISTORY.rst update: fix alias_generator and field config conflict
* Merge HISTORY.rst with master
* 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