Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ADD] StrictExtendableBaseModel: An ExtendableBaseModel with strict v… #17

Merged
merged 2 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ jobs:
machine: ubuntu-22.04
- python-version: "3.11"
machine: ubuntu-22.04
- python-version: "3.12"
machine: ubuntu-22.04
steps:
- uses: "actions/checkout@v3"
- uses: "actions/setup-python@v4"
Expand Down
6 changes: 6 additions & 0 deletions news/17.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Add a new base class `StrictExtendableBaseModel`. This class is a subclass of
`ExtendableBaseModel` and enforces strict validation by forcing the revalidation
of instances when the method `model_validate` is called and by ensuring that
the values assignment is validated. This class is useful when you need to
instantiate you model from a partial dictionary and you want to ensure that
any value assignment taking place after the instantiation is validated.
24 changes: 24 additions & 0 deletions src/extendable_pydantic/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,27 @@ class ExtendableBaseModel(BaseModel, metaclass=ExtendableModelMeta):
"""Base class for extendable pydantic models."""

pass


class StrictExtendableBaseModel(
ExtendableBaseModel,
revalidate_instances="always",
validate_assignment=True,
extra="forbid",
):
"""An ExtendableBaseModel with strict validation.

By default, Pydantic does not revalidate instances during validation, nor
when the data are changed. Validation only occurs when the model is created.
This is sometime not suitable when the data are changed after the
model is created or the model is created from a partial set of data and
then updated with more data. This class enforces strict validation by
forcing the revalidation of instances when the method `model_validate` is
called and by ensuring that the values assignment is validated.

The following parameters are added:
* revalidate_instances="always": model instances are revalidated during validation
(default is "never")
* validate_assignment=True: revalidate the model when the data is changed (default is False)
* extra="forbid": Forbid any extra attributes (default is "ignore")
"""
2 changes: 2 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ python =
3.9: py39, typing, pypi-description
3.10: py310, typing
3.11: py311, typing
3.12: py312, typing
pypi-description: pypi-description

[tox]
Expand All @@ -15,6 +16,7 @@ envlist =
py39
py310
py311
py312
lint
typing
pypi-description
Expand Down