8000 Switching to `pydantic_core` by samuelcolvin · Pull Request #4516 · pydantic/pydantic · GitHub
[go: up one dir, main page]

Skip to content

Switching to pydantic_core #4516

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

Merged
merged 70 commits into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
70 commits
Select commit Hold shift + click to select a range
e2e4602
working on core schema generation
samuelcolvin Sep 12, 2022
964bd48
adapting main.py
samuelcolvin Sep 14, 2022
71fd9a6
getting tests to run
samuelcolvin Sep 14, 2022
9c0b13d
fix tests
samuelcolvin Sep 14, 2022
3c50562
disable pyright, fix mypy
samuelcolvin Sep 14, 2022
4fd2027
moving to class-based model generation
samuelcolvin Sep 14, 2022
169db6e
working on validators
samuelcolvin Sep 15, 2022
9b4a41d
change how models are created
samuelcolvin Sep 16, 2022
59bdd49
start fixing test_main.py
samuelcolvin Sep 16, 2022
246fff2
fixing mypy
samuelcolvin Sep 20, 2022
5b19c5e
SelfType
samuelcolvin Sep 20, 2022
99a8a15
recursive models working, more tests fixed
samuelcolvin Sep 24, 2022
8000
8426d5c
fix tests on <3.10
samuelcolvin Sep 24, 2022
034132e
get docs build to pass
samuelcolvin Sep 24, 2022
a4cf9d7
starting to cleanup types.py
samuelcolvin Sep 24, 2022
b948a30
starting works on custom types
samuelcolvin Sep 24, 2022
98c3391
working on using annotated-types
samuelcolvin Sep 25, 2022
8638a00
using annoated types for constraints
samuelcolvin Sep 26, 2022
e6f424d
lots of cleanup, fixing network tests
samuelcolvin Sep 26, 2022
e40986d
network tests passing :tada:
samuelcolvin Sep 27, 2022
cd96301
working on types
samuelcolvin Sep 27, 2022
0ec15d8
working on types and cleanup
samuelcolvin Sep 29, 2022
b73d44a
fixing UUID type, restructing again
samuelcolvin Sep 30, 2022
8ddc5ba
more types and newer pydantic-core
samuelcolvin Sep 30, 2022
e1060fa
working on Iterable
samuelcolvin Sep 30, 2022
20eefe2
more test_types tests
samuelcolvin Oct 11, 2022
9e81a50
support newer pydantic-core, fixing more test_types.py
samuelcolvin Oct 13, 2022
eaf4aa6
working through more test_types.py
samuelcolvin Oct 14, 2022
3bf5ef7
test_types.py at last passing locally :tada:
samuelcolvin Oct 16, 2022
38ee60e
fixing more tests in test_types.py
samuelcolvin Oct 16, 2022
50b745c
fix datetime_parse tests and linting
samuelcolvin Oct 16, 2022
0c6ebd6
get tests running again, rename to test_datetime.py
samuelcolvin Oct 16, 2022
e0f0a69
renaming internal modules
samuelcolvin Oct 17, 2022
7b1f337
working through mypy errors
samuelcolvin Oct 17, 2022
167313a
fixing mypy
samuelcolvin Oct 18, 2022
018e2c9
refactoring _generate_schema.py
samuelcolvin Oct 18, 2022
43b160d
test_main.py passing
samuelcolvin Oct 18, 2022
508db66
uprev deps
samuelcolvin Oct 18, 2022
7dd6a1b
fix conftest and linting?
samuelcolvin Oct 18, 2022
d1ac9c9
importing Annotated
samuelcolvin Oct 18, 2022
6b7d15b
ltining
samuelcolvin Oct 18, 2022
6c5bd7a
import Annotated from typing_extensions
samuelcolvin Oct 18, 2022
f6b8281
fixing 3.7 compatibility
samuelcolvin Oct 18, 2022
996a922
fixing tests on 3.9
samuelcolvin Oct 18, 2022
41c0530
fix linting
samuelcolvin Oct 19, 2022
66ac24c
fixing SecretField and 3.9 tests
samuelcolvin Oct 19, 2022
e7d8be8
customising get_type_hints
samuelcolvin Oct 19, 2022
7768b6d
ignore warnings on 3.11
samuelcolvin Oct 19, 2022
f3a3101
spliting repr out of utils
samuelcolvin Oct 19, 2022
aa65758
removing unused bits of _repr, fix tests for 3.7
samuelcolvin Oct 19, 2022
8c9639c
more cleanup, removing many type aliases
samuelcolvin Oct 19, 2022
730ebbd
clean up repr
samuelcolvin Oct 19, 2022
c6bf124
support namedtuples and typeddicts
samuelcolvin Oct 20, 2022
fa0462c
test is_union
samuelcolvin Oct 20, 2022
50c193b
removing errors, uprev pydantic-core
samuelcolvin Oct 20, 2022
60f1e1b
fix tests on 3.8
samuelcolvin Oct 25, 2022
3d2603b
fixing private attributes and model_post_init
samuelcolvin Oct 25, 2022
10cf0b4
renaming and cleanup
samuelcolvin Oct 26, 2022
fe1c3b2
remove unnecessary PydanticMetadata inheritance
samuelcolvin Oct 26, 2022
eb610b4
fixing forward refs and mypy tests
samuelcolvin Oct 26, 2022
6c1ba91
fix signatures, change how xfail works
samuelcolvin Oct 26, 2022
10ac55f
revert mypy tests to 3.7 syntax
samuelcolvin Oct 26, 2022
bb3db68
correct model title
samuelcolvin Oct 26, 2022
5656056
try to fix tests
samuelcolvin Oct 26, 2022
40358de
fixing ClassVar forward refs
samuelcolvin Oct 27, 2022
fa83ac9
uprev pydantic-core, new error format
samuelcolvin Oct 27, 2022
eaa2ad1
add "force" argument to model_rebuild
samuelcolvin Oct 27, 2022
cc7aaab
Apply suggestions from code review
samuelcolvin Nov 2, 2022
a356144
more suggestions from @tiangolo
samuelcolvin Nov 2, 2022
708c77b
extra -> json_schema_extra on Field
samuelcolvin Nov 2, 2022
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
Prev Previous commit
extra -> json_schema_extra on Field
  • Loading branch information
samuelcolvin committed Nov 2, 2022
commit 708c77bd5a94c5ca2d79faf726aa593ba2c8ca16
28 changes: 2 additions & 26 deletions pydantic/_internal/_utils.py
8000
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""
Bucket of reusable internal utilities.

This should be reduced as much as possible with functions only used in one place, moved to that place.
"""
from __future__ import annotations as _annotations

Expand All @@ -23,10 +25,8 @@

__all__ = (
'sequence_like',
'validate_field_name',
'lenient_isinstance',
'lenient_issubclass',
'in_ipython',
'is_valid_identifier',
'deep_update',
'update_not_none',
Expand Down Expand Up @@ -84,18 +84,6 @@ def sequence_like(v: Any) -> bool:
return isinstance(v, (list, tuple, set, frozenset, GeneratorType, deque))


def validate_field_name(bases: list[type[BaseModel]], field_name: str) -> None:
"""
Ensure that the field's name does not shadow an existing attribute of the model.
"""
for base in bases:
if getattr(base, field_name, None):
raise NameError(
f'Field name "{field_name}" shadows a BaseModel attribute; '
f'use a different field name with "alias=\'{field_name}\'".'
)


def lenient_isinstance(o: Any, class_or_tuple: type[Any] | tuple[type[Any], ...] | None) -> bool:
try:
return isinstance(o, class_or_tuple) # type: ignore[arg-type]
Expand All @@ -112,18 +100,6 @@ def lenient_issubclass(cls: Any, class_or_tuple: Any) -> bool:
raise # pragma: no cover


def in_ipython() -> bool:
"""
Check whether we're in an ipython environment, including jupyter notebooks.
"""
try:
eval('__IPYTHON__')
except NameError:
return False
else: # pragma: no cover
return True


def is_valid_identifier(identifier: str) -> bool:
"""
Checks that a string is a valid identifier and not a Python keyword.
Expand Down
35 changes: 18 additions & 17 deletions pydantic/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@

class FieldInfo(_repr.Representation):
"""
Captures extra information about a field.
Hold information about a field, FieldInfo is used however a field is defined, whether or not the `Field()`
function below is explicitly used.
"""

__slots__ = (
Expand All @@ -33,7 +34,7 @@ class FieldInfo(_repr.Representation):
'metadata',
'repr',
'discriminator',
'extra',
'json_schema_extra',
)

# used to convert kwargs to metadata/constraints,
Expand All @@ -57,29 +58,29 @@ class FieldInfo(_repr.Representation):
}

def __init__(self, **kwargs: Any) -> None:
self.annotation, annotation_metadata = self._extract_metadata(kwargs.pop('annotation', None))
self.annotation, annotation_metadata = self._extract_metadata(kwargs.get('annotation'))

default = kwargs.pop('default', Undefined)
if default is Ellipsis:
self.default = Undefined
else:
self.default = default

self.default_factory = kwargs.pop('default_factory', None)
self.default_factory = kwargs.get('default_factory')

if self.default is not Undefined and self.default_factory is not None:
raise ValueError('cannot specify both default and default_factory')

self.alias = kwargs.pop('alias', None)
8000 self.alias_priority = kwargs.pop('alias_priority', 2 if self.alias is not None else None)
self.title = kwargs.pop('title', None)
self.description = kwargs.pop('description', None)
self.exclude = kwargs.pop('exclude', None)
self.include = kwargs.pop('include', None)
self.alias = kwargs.get('alias')
self.alias_priority = kwargs.get('alias_priority', 2 if self.alias is not None else None)
self.title = kwargs.get('title')
self.description = kwargs.get('description')
self.exclude = kwargs.get('exclude')
self.include = kwargs.get('include')
self.metadata = self._collect_metadata(kwargs) + annotation_metadata
self.discriminator = kwargs.pop('discriminator', None)
self.repr = kwargs.pop('repr', True)
self.extra = kwargs
self.discriminator = kwargs.get('discriminator')
self.repr = kwargs.get('repr', True)
self.json_schema_extra = kwargs.get('json_schema_extra')

@classmethod
def from_field(cls, default: Any = Undefined, **kwargs: Any) -> 'FieldInfo':
Expand Down Expand Up @@ -227,7 +228,7 @@ def __repr_args__(self) -> 'ReprArgs':
continue
if s == 'default_factory' and self.default_factory is not None:
yield 'default_factory', _repr.PlainRepr(_repr.display_as_type(self.default_factory))
elif s != 'extra' or self.extra:
else:
value = getattr(self, s)
if value is not None and value is not Undefined:
yield s, value
Expand Down Expand Up @@ -258,7 +259,7 @@ def Field(
pattern: str = None,
discriminator: str = None,
repr: bool = True,
**extra: Any,
json_schema_extra: dict[str, Any] | None = None,
) -> Any:
"""
Used to provide extra information about a field, either for the model schema or complex validation. Some arguments
Expand Down Expand Up @@ -306,7 +307,7 @@ def Field(
:param discriminator: only useful with a (discriminated a.k.a. tagged) `Union` of sub models with a common field.
The `discriminator` is the name of this common field to shorten validation and improve generated schema
:param repr: show this field in the representation
:param **extra: any additional keyword arguments will be added as is to the schema
:param json_schema_extra: extra dict to be merged with the JSON Schema for this field
"""
return FieldInfo.from_field(
default,
Expand All @@ -332,7 +333,7 @@ def Field(
pattern=pattern,
discriminator=discriminator,
repr=repr,
**extra,
json_schema_extra=json_schema_extra,
)


Expand Down
2 changes: 1 addition & 1 deletion tests/mypy/modules/success.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class WrapperModel(GenericModel, Generic[T]):

class WithField(B 77F4 aseModel):
age: int
first_name: str = Field('John', const=True)
first_name: str = Field('John', max_length=42)


# simple decorator
Expand Down
7 changes: 7 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1848,3 +1848,10 @@ def model_post_init(self, **kwargs) -> None:
m = Model(c=1, d='2', sub={'a': 3, 'b': '4'})
assert m.dict() == {'c': 1, 'd': 2, 'sub': {'a': 3, 'b': 4}}
assert calls == ['submodel_post_init', 'model_post_init']


def test_extra_args_to_field_type_error():
with pytest.raises(TypeError, match='unexpected keyword argument'):

class Model(BaseModel):
a: int = Field(thing=1)
0