Skip to content

Commit

Permalink
Use pydantic core 0.17.1 (#5291)
Browse files Browse the repository at this point in the history
* Use pydantic core 0.17.0

* Use 0.17.1
  • Loading branch information
dmontagu committed Mar 27, 2023
1 parent 8583d9d commit 98427b3
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 18 deletions.
20 changes: 16 additions & 4 deletions pydantic/_internal/_core_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,25 @@
core_schema.WrapValidatorFunctionSchema,
]

_CORE_SCHEMA_FIELD_TYPES = {'typed-dict-field', 'dataclass-field'}

def is_typed_dict_field(schema: CoreSchema | core_schema.TypedDictField) -> TypeGuard[core_schema.TypedDictField]:
return 'type' not in schema

def is_typed_dict_field(
schema: CoreSchema | core_schema.TypedDictField | core_schema.DataclassField,
) -> TypeGuard[core_schema.TypedDictField]:
return schema['type'] == 'typed-dict-field'

def is_core_schema(schema: CoreSchema | core_schema.TypedDictField) -> TypeGuard[CoreSchema]:
return 'type' in schema

def is_dataclass_field(
schema: CoreSchema | core_schema.TypedDictField | core_schema.DataclassField,
) -> TypeGuard[core_schema.DataclassField]:
return schema['type'] == 'dataclass-field'


def is_core_schema(
schema: CoreSchema | core_schema.TypedDictField | core_schema.DataclassField,
) -> TypeGuard[CoreSchema]:
return schema['type'] not in _CORE_SCHEMA_FIELD_TYPES


def is_function_with_inner_schema(
Expand Down
9 changes: 8 additions & 1 deletion pydantic/json_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ def generate_inner(self, schema: CoreSchema | TypedDictField) -> JsonSchemaValue
# Generate the core-schema-type-specific bits of the schema generation:
if _core_utils.is_typed_dict_field(schema):
json_schema = self.typed_dict_field_schema(schema)
elif _core_utils.is_dataclass_field(schema):
json_schema = self.dataclass_field_schema(schema)
elif _core_utils.is_core_schema(schema): # Ideally we wouldn't need this redundant typeguard..
generate_for_schema_type = self._schema_type_to_method[schema['type']]
json_schema = generate_for_schema_type(schema)
Expand Down Expand Up @@ -641,6 +643,11 @@ def typed_dict_field_schema(self, schema: core_schema.TypedDictField) -> JsonSch

return json_schema

def dataclass_field_schema(self, schema: core_schema.DataclassField) -> JsonSchemaValue:
json_schema = self.generate_inner(schema['schema'])

return json_schema

def model_schema(self, schema: core_schema.ModelSchema) -> JsonSchemaValue:
# Note: While it might be nice to be able to call schema['model'].model_json_schema(),
# I don't think that is a good idea because that method does caching (which is good),
Expand Down Expand Up @@ -831,7 +838,7 @@ def field_title_should_be_set(self, schema: CoreSchema | TypedDictField) -> bool
Intuitively, we want this to return true for schemas that wouldn't otherwise provide their own title
(e.g., int, float, str), and false for those that would (e.g., BaseModel subclasses).
"""
if _core_utils.is_typed_dict_field(schema):
if _core_utils.is_typed_dict_field(schema) or _core_utils.is_dataclass_field(schema):
return self.field_title_should_be_set(schema['schema'])

elif _core_utils.is_core_schema(schema):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ classifiers = [
requires-python = '>=3.7'
dependencies = [
'typing-extensions>=4.2.0',
'pydantic-core>=0.16.1',
'pydantic-core>=0.17.1',
'annotated-types>=0.4.0',
]
optional-dependencies = { email = ['email-validator>=1.3.0'] }
Expand Down
2 changes: 1 addition & 1 deletion requirements/linting.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ platformdirs==3.1.1
# virtualenv
pre-commit==2.21.0
# via -r requirements/linting.in
pydantic-core==0.16.1
pydantic-core==0.17.1
# via -r requirements/linting.in
pyupgrade==3.3.1
# via -r requirements/linting.in
Expand Down
2 changes: 1 addition & 1 deletion requirements/pyproject-all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ email-validator==1.3.1
# via pydantic (pyproject.toml)
idna==3.4
# via email-validator
pydantic-core==0.16.1
pydantic-core==0.17.1
# via pydantic (pyproject.toml)
typing-extensions==4.5.0
# via
Expand Down
2 changes: 1 addition & 1 deletion requirements/pyproject-min.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#
annotated-types==0.4.0
# via pydantic (pyproject.toml)
pydantic-core==0.16.1
pydantic-core==0.17.1
# via pydantic (pyproject.toml)
typing-extensions==4.5.0
# via
Expand Down
42 changes: 33 additions & 9 deletions tests/test_discriminated_union.py
Original file line number Diff line number Diff line change
Expand Up @@ -878,12 +878,17 @@ def test_wrap_function_schema() -> None:
'function': HasRepr(IsStr(regex=r'<function [a-z_]*\.<locals>\.<lambda> at 0x[0-9a-fA-F]+>')),
},
'schema': {
'fields': {'kind': {'schema': {'expected': ['cat'], 'type': 'literal'}}},
'fields': {
'kind': {'schema': {'expected': ['cat'], 'type': 'literal'}, 'type': 'typed-dict-field'}
},
'type': 'typed-dict',
},
'type': 'function-wrap',
},
'dog': {'fields': {'kind': {'schema': {'expected': ['dog'], 'type': 'literal'}}}, 'type': 'typed-dict'},
'dog': {
'fields': {'kind': {'schema': {'expected': ['dog'], 'type': 'literal'}, 'type': 'typed-dict-field'}},
'type': 'typed-dict',
},
},
'discriminator': 'kind',
'from_attributes': True,
Expand Down Expand Up @@ -942,13 +947,17 @@ def test_lax_or_strict_definitions() -> None:
'definitions': [{'ref': 'my-str-definition', 'type': 'str'}],
'schema': {
'lax_schema': {
'fields': {'kind': {'schema': {'expected': ['DOG'], 'type': 'literal'}}},
'fields': {
'kind': {'schema': {'expected': ['DOG'], 'type': 'literal'}, 'type': 'typed-dict-field'}
},
'type': 'typed-dict',
},
'strict_schema': {
'definitions': [{'ref': 'my-int-definition', 'type': 'int'}],
'schema': {
'fields': {'kind': {'schema': {'expected': ['dog'], 'type': 'literal'}}},
'fields': {
'kind': {'schema': {'expected': ['dog'], 'type': 'literal'}, 'type': 'typed-dict-field'}
},
'type': 'typed-dict',
},
'type': 'definitions',
Expand All @@ -957,7 +966,10 @@ def test_lax_or_strict_definitions() -> None:
},
'type': 'definitions',
},
'cat': {'fields': {'kind': {'schema': {'expected': ['cat'], 'type': 'literal'}}}, 'type': 'typed-dict'},
'cat': {
'fields': {'kind': {'schema': {'expected': ['cat'], 'type': 'literal'}, 'type': 'typed-dict-field'}},
'type': 'typed-dict',
},
'dog': 'DOG',
},
'discriminator': 'kind',
Expand Down Expand Up @@ -1003,23 +1015,35 @@ def test_wrapped_nullable_union() -> None:
'schema': {
'choices': {
'ant': {
'fields': {'kind': {'schema': {'expected': ['ant'], 'type': 'literal'}}},
'fields': {
'kind': {'schema': {'expected': ['ant'], 'type': 'literal'}, 'type': 'typed-dict-field'}
},
'type': 'typed-dict',
},
'cat': {
'function': {
'type': 'general',
'function': HasRepr(IsStr(regex=r'<function [a-z_]*\.<locals>\.<lambda> at 0x[0-9a-fA-F]+>')),
'type': 'general',
},
'schema': {
'schema': {
'choices': [
{
'fields': {'kind': {'schema': {'expected': ['cat'], 'type': 'literal'}}},
'fields': {
'kind': {
'schema': {'expected': ['cat'], 'type': 'literal'},
'type': 'typed-dict-field',
}
},
'type': 'typed-dict',
},
{
'fields': {'kind': {'schema': {'expected': ['dog'], 'type': 'literal'}}},
'fields': {
'kind': {
'schema': {'expected': ['dog'], 'type': 'literal'},
'type': 'typed-dict-field',
}
},
'type': 'typed-dict',
},
],
Expand Down

0 comments on commit 98427b3

Please sign in to comment.