* doc(schema): use boldface to highlight a note
* doc(schema): fix small formatting typo
* doc(schema): attempt to fix the callout
* docs(schema): indent the callout
* 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: 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>
* 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>
* 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>
* 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
* 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>
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.
* 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
* 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
* 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>
* 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>
* 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.
* 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>
* 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>
* 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
* add description of subclass each_item scenario
* add example of subclass validator with each_item
* each_item causes validator to not run when parent class has List field #1566
* 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