Skip to content

Commit

Permalink
Update to latest typing-extensions, add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Viicos committed Dec 2, 2023
1 parent 5158d86 commit d2af80f
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 70 deletions.
99 changes: 38 additions & 61 deletions pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions pydantic/fields.py
Expand Up @@ -293,10 +293,10 @@ class MyModel(pydantic.BaseModel):
new_field_info.frozen = final or field_info.frozen
metadata: list[Any] = []
for a in extra_args:
if not isinstance(a, FieldInfo):
metadata.append(a)
elif _typing_extra.is_deprecated_instance(a):
if _typing_extra.is_deprecated_instance(a):
new_field_info.deprecated = True
elif not isinstance(a, FieldInfo):
metadata.append(a)
else:
metadata.extend(a.metadata)
new_field_info.metadata = metadata
Expand Down Expand Up @@ -369,10 +369,10 @@ class MyModel(pydantic.BaseModel):
field_info = FieldInfo.merge_field_infos(*field_infos, annotation=first_arg, default=default)
metadata: list[Any] = []
for a in extra_args:
if not isinstance(a, FieldInfo):
metadata.append(a)
elif _typing_extra.is_deprecated_instance(a):
if _typing_extra.is_deprecated_instance(a):
field_info.deprecated = True
elif not isinstance(a, FieldInfo):
metadata.append(a)
else:
metadata.extend(a.metadata)
field_info.metadata = metadata
Expand Down
2 changes: 1 addition & 1 deletion pydantic/types.py
Expand Up @@ -2825,7 +2825,7 @@ def __get_pydantic_core_schema__(cls, source_type: Any, handler: GetCoreSchemaHa


# Can't use `Field(deprecated=True)` because of a circular import
Deprecated = Annotated[T, deprecated("")]
Deprecated = Annotated[T, deprecated('')]
"""
Mark a field as being deprecated.
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Expand Up @@ -63,7 +63,7 @@ classifiers = [
]
requires-python = '>=3.8'
dependencies = [
'typing-extensions>=4.6.1',
'typing-extensions>=4.9.0rc1',
'annotated-types>=0.4.0',
"pydantic-core==2.14.5",
]
Expand Down
25 changes: 24 additions & 1 deletion tests/test_json_schema.py
Expand Up @@ -33,7 +33,7 @@
import pytest
from dirty_equals import HasRepr
from pydantic_core import CoreSchema, SchemaValidator, core_schema, to_json
from typing_extensions import Annotated, Literal, Self, TypedDict
from typing_extensions import Annotated, Literal, Self, TypedDict, deprecated

import pydantic
from pydantic import (
Expand Down Expand Up @@ -75,6 +75,7 @@
UUID3,
UUID4,
UUID5,
Deprecated,
DirectoryPath,
FilePath,
Json,
Expand Down Expand Up @@ -5812,3 +5813,25 @@ class OuterModel(pydantic.BaseModel):

with pytest.raises(ValidationError):
OuterModel(x=2, y=-1, z=-1)


def test_deprecated_fields():
class Model(BaseModel):
a: Annotated[int, Field(deprecated=True)]
b: Annotated[int, Field(deprecated=False)]
c: Annotated[int, deprecated('')]
d: Annotated[int, deprecated('')] = 1
e: Deprecated[int]

assert Model.model_json_schema() == {
'properties': {
'a': {'deprecated': True, 'title': 'A', 'type': 'integer'},
'b': {'deprecated': False, 'title': 'B', 'type': 'integer'},
'c': {'deprecated': True, 'title': 'C', 'type': 'integer'},
'd': {'default': 1, 'deprecated': True, 'title': 'D', 'type': 'integer'},
'e': {'deprecated': True, 'title': 'E', 'type': 'integer'},
},
'required': ['a', 'b', 'c', 'e'],
'title': 'Model',
'type': 'object',
}

0 comments on commit d2af80f

Please sign in to comment.