-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into f/discriminated-union
- Loading branch information
Showing
90 changed files
with
2,999 additions
and
298 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
blank_issues_enabled: true | ||
contact_links: | ||
- name: Question | ||
url: https://github.com/samuelcolvin/pydantic/discussions/new | ||
about: Ask a question about how to use pydantic using github discussions | ||
|
||
- name: Feature Request | ||
url: https://github.com/samuelcolvin/pydantic/discussions/new | ||
about: > | ||
If you think we should add a new feature to pydantic, please start a discussion, once it attracts | ||
wider support, it can be migrated to an issue |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ dist/ | |
.mypy_cache/ | ||
test.py | ||
.coverage | ||
.hypothesis | ||
/htmlcov/ | ||
/benchmarks/*.json | ||
/docs/.changelog.md | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Fixed issue causing KeyError to be raised when building schema from multiple `BaseModel` with the same names declared in separate classes. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
**Breaking Change:** always validate only first sublevel items with `each_item`. | ||
There were indeed some edge cases with some compound types where the validated items were the last sublevel ones. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
allow overwriting `ClassVar`s in sub-models without having to re-annotate them |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add merged `json_encoders` inheritance |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Handle properly fields of type `Callable` with a default value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add a [Hypothesis](https://hypothesis.readthedocs.io/) plugin for easier [property-based testing](https://increment.com/testing/in-praise-of-property-based-testing/) with Pydantic's custom types - [usage details here](https://pydantic-docs.helpmanual.io/hypothesis_plugin/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add the ability to customize settings sources (add / disable / change priority order). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
added `Config.anystr_lower` and `to_lower` kwarg to `constr` and `conbytes`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Support `typing.Annotated` hints on model fields. A `Field` may now be set in the type hint with `Annotated[..., Field(...)`; all other annotations are ignored but still visible with `get_type_hints(..., include_extras=True)`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Allow non-existent secrets directory by only issuing a warning |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
add `allow_mutation` constraint to `Field` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add a `FrozenSet[str]` type annotation to the `allowed_schemes` argument on the `strict_url` field type. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Raise a user-friendly `TypeError` when a `root_validator` does not return a `dict` (e.g. `None`) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Add support for `NamedTuple` and `TypedDict` types. | ||
Those two types are now handled and validated when used inside `BaseModel` or _pydantic_ `dataclass`. | ||
Two utils are also added `create_model_from_namedtuple` and `create_model_from_typeddict`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Resolve forward refs for stdlib dataclasses converted into _pydantic_ ones |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ensure cythonized functions are left untouched when creating models, based on #1944 by @kollmats |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Support custom root type (aka `__root__`) with `from_orm()` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Support custom root type (aka `__root__`) when using `parse_obj()` with nested models |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
add documentation for con* type functions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Prevent overriding positional arguments with keyword arguments in | ||
`validate_arguments`, as per behaviour with native functions. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Fix `validate_decorator` so `**kwargs` doesn't exclude values when the keyword | ||
has the same name as the `*args` or `**kwargs` names. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Support generating schema for Generic fields. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
fix: keep order of fields with `BaseModel.construct()` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
fix: update `__fields_set__` in `BaseModel.copy(update=…)` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
fix: `python_requires` metadata to require >=3.6.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Support empty tuple type |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
fix: allow `None` for type `Optional[conset / conlist]` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Add `Config.copy_on_model_validation` flag. When set to `False`, _pydantic_ will keep models used as fields | ||
untouched on validation instead of reconstructing (copying) them |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from typing import NamedTuple | ||
|
||
from pydantic import BaseModel, ValidationError | ||
|
||
|
||
class Point(NamedTuple): | ||
x: int | ||
y: int | ||
|
||
|
||
class Model(BaseModel): | ||
p: Point | ||
|
||
|
||
print(Model(p=('1', '2'))) | ||
|
||
try: | ||
Model(p=('1.3', '2')) | ||
except ValidationError as e: | ||
print(e) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from typing import TypedDict | ||
|
||
from pydantic import BaseModel, Extra, ValidationError | ||
|
||
|
||
# `total=False` means keys are non-required | ||
class UserIdentity(TypedDict, total=False): | ||
name: str | ||
surname: str | ||
|
||
|
||
class User(TypedDict): | ||
identity: UserIdentity | ||
age: int | ||
|
||
|
||
class Model(BaseModel): | ||
u: User | ||
|
||
class Config: | ||
extra = Extra.forbid | ||
|
||
|
||
print(Model(u={'identity': {'name': 'Smith', 'surname': 'John'}, 'age': '37'})) | ||
|
||
print(Model(u={'identity': {'name': None, 'surname': 'John'}, 'age': '37'})) | ||
|
||
print(Model(u={'identity': {}, 'age': '37'})) | ||
|
||
|
||
try: | ||
Model(u={'identity': {'name': ['Smith'], 'surname': 'John'}, 'age': '24'}) | ||
except ValidationError as e: | ||
print(e) | ||
|
||
try: | ||
Model( | ||
u={ | ||
'identity': {'name': 'Smith', 'surname': 'John'}, | ||
'age': '37', | ||
'email': 'john.smith@me.com', | ||
} | ||
) | ||
except ValidationError as e: | ||
print(e) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from datetime import datetime, timedelta | ||
from pydantic import BaseModel | ||
from pydantic.json import timedelta_isoformat | ||
|
||
|
||
class BaseClassWithEncoders(BaseModel): | ||
dt: datetime | ||
diff: timedelta | ||
|
||
class Config: | ||
json_encoders = { | ||
datetime: lambda v: v.timestamp() | ||
} | ||
|
||
|
||
class ChildClassWithEncoders(BaseClassWithEncoders): | ||
class Config: | ||
json_encoders = { | ||
timedelta: timedelta_isoformat | ||
} | ||
|
||
|
||
m = ChildClassWithEncoders(dt=datetime(2032, 6, 1), diff=timedelta(hours=100)) | ||
print(m.json()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import typing | ||
from hypothesis import given, strategies as st | ||
from pydantic import BaseModel, EmailStr, PaymentCardNumber, PositiveFloat | ||
|
||
|
||
class Model(BaseModel): | ||
card: PaymentCardNumber | ||
price: PositiveFloat | ||
users: typing.List[EmailStr] | ||
|
||
|
||
@given(st.builds(Model)) | ||
def test_property(instance): | ||
# Hypothesis calls this test function many times with varied Models, | ||
# so you can write a test that should pass given *any* instance. | ||
assert 0 < instance.price | ||
assert all('@' in email for email in instance.users) | ||
|
||
|
||
@given(st.builds(Model, price=st.floats(100, 200))) | ||
def test_with_discount(instance): | ||
# This test shows how you can override specific fields, | ||
# and let Hypothesis fill in any you don't care about. | ||
assert 100 <= instance.price <= 200 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from pydantic import BaseModel as PydanticBaseModel | ||
|
||
|
||
class BaseModel(PydanticBaseModel): | ||
class Config: | ||
arbitrary_types_allowed = True | ||
|
||
|
||
class MyClass: | ||
"""A random class""" | ||
|
||
|
||
class Model(BaseModel): | ||
x: MyClass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from typing import TypedDict | ||
|
||
from pydantic import ValidationError, create_model_from_typeddict | ||
|
||
|
||
class User(TypedDict): | ||
name: str | ||
id: int | ||
|
||
|
||
class Config: | ||
extra = 'forbid' | ||
|
||
|
||
UserM = create_model_from_typeddict(User, __config__=Config) | ||
print(repr(UserM(name=123, id='3'))) | ||
|
||
try: | ||
UserM(name=123, id='3', other='no') | ||
except ValidationError as e: | ||
print(e) |
Oops, something went wrong.