* fix#2293: Properly encode Decimals without any decimal places.
* doc: Added changelog entry.
* refactor: Move ConstrainedDecimal test from separate file into test_json
* docs: Remove prefix from changelog.
* test: Changed test_con_decimal_encode to @samuelcolvins recommendations
* fix(mypy): handle FilePath and DirectoryPath custom types
closes#2098
* fix(mypy): handle almost all other custom types
* remove mypy overwrite when extra logic is set
* fix unused mypy ignore
* 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>
* Infer root type from Annotated
* Extract Field from Annotated
* Add changelog
* Extend existing get_args/get_origin
* Fix Annotated on py3.6 without typing-extensions
* Handle Ellipsis default
* Fix field reuse after FieldInfo.default mutation
* Fix ci
* 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`
* fix: update `__fields_set__` in `BaseModel.copy(update=…)`
closes#2290
* chore: rewrite to be more explicit
Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
* fix: Change non-existant secrets dir into a warning
* Simplify if condition
Co-authored-by: Eric Jolibois <em.jolibois@gmail.com>
* Reword changelog entry
Co-authored-by: Eric Jolibois <em.jolibois@gmail.com>
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
* fix(decorator): detect when a duplicate argument is passed in
Currently, it is possible to override a positional argument with a keyword
argument using the decorator.
In native python, this raises a TypeError.
Detect this usecase, to prevent keywords overriding positional parameters.
Closes: https://github.com/samuelcolvin/pydantic/issues/2249
* add changelog entry
* fix docs example to not override positional argument
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
* 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>
* 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
* 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
* 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
* 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>
* 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
* 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>
* 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
* 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
* 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 :)