Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit944674a

Browse files
authored
Removetyping_extensions version check in tests (#11830)
1 parentf165eac commit944674a

File tree

5 files changed

+42
-48
lines changed

5 files changed

+42
-48
lines changed

‎docs/concepts/fields.md

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -804,16 +804,15 @@ See the [Serialization] section for more details.
804804
The`deprecated` parameter can be used to mark a field as being deprecated. Doing so will result in:
805805

806806
* a runtime deprecation warning emitted when accessing the field.
807-
*`"deprecated": true` being set in the generated JSON schema.
807+
* The[deprecated](https://json-schema.org/draft/2020-12/json-schema-validation#section-9.3) keyword
808+
being set in the generated JSON schema.
808809

809-
You can set the`deprecated` parameter as one of:
810-
811-
* A string, which will be used as the deprecation message.
812-
* An instance of the`warnings.deprecated` decorator (or the`typing_extensions` backport).
813-
* A boolean, which will be used to mark the field as deprecated with a default`'deprecated'` deprecation message.
810+
This parameter accepts different types, described below.
814811

815812
###`deprecated` as a string
816813

814+
The value will be used as the deprecation message.
815+
817816
```python
818817
from typingimport Annotated
819818

@@ -828,30 +827,50 @@ print(Model.model_json_schema()['properties']['deprecated_field'])
828827
#> {'deprecated': True, 'title': 'Deprecated Field', 'type': 'integer'}
829828
```
830829

831-
###`deprecated` via the`warnings.deprecated` decorator
830+
###`deprecated` via the`@warnings.deprecated` decorator
832831

833-
!!! note
834-
You can only use the`deprecated` decorator in this way if you have
835-
`typing_extensions` >= 4.9.0 installed.
832+
The[`@warnings.deprecated`][warnings.deprecated] decorator (or the
833+
[`typing_extensions` backport][typing_extensions.deprecated] on Python
834+
3.12 and lower) can be used as an instance.
836835

837-
```python {test="skip"}
838-
import importlib.metadata
839-
from typingimport Annotated, deprecated
836+
<!-- TODO: tabs should be auto-generated if using Ruff (https://github.com/pydantic/pydantic/issues/10083)-->
840837

841-
from packaging.versionimport Version
838+
=== "Python 3.9 and above"
842839

843-
from pydanticimport BaseModel, Field
840+
```python
841+
from typing import Annotated
842+
843+
from typing_extensions import deprecated
844+
845+
from pydantic import BaseModel, Field
844846

845-
if Version(importlib.metadata.version('typing_extensions'))>= Version('4.9'):
846847

847848
class Model(BaseModel):
848849
deprecated_field: Annotated[int, deprecated('This is deprecated')]
849850

850851
# Or explicitly using `Field`:
851-
alt_form: Annotated[
852-
int, Field(deprecated=deprecated('This is deprecated'))
853-
]
854-
```
852+
alt_form: Annotated[int, Field(deprecated=deprecated('This is deprecated'))]
853+
```
854+
855+
=== "Python 3.13 and above"
856+
857+
```python {requires="3.13"}
858+
from typing import Annotated
859+
from warnings import deprecated
860+
861+
from pydantic import BaseModel, Field
862+
863+
864+
class Model(BaseModel):
865+
deprecated_field: Annotated[int, deprecated('This is deprecated')]
866+
867+
# Or explicitly using `Field`:
868+
alt_form: Annotated[int, Field(deprecated=deprecated('This is deprecated'))]
869+
```
870+
871+
!!! note "Support for`category` and`stacklevel`"
872+
The current implementation of this feature does not take into account the`category` and`stacklevel`
873+
arguments to the`deprecated` decorator. This might land in a future version of Pydantic.
855874

856875
###`deprecated` as a boolean
857876

@@ -869,10 +888,6 @@ print(Model.model_json_schema()['properties']['deprecated_field'])
869888
#> {'deprecated': True, 'title': 'Deprecated Field', 'type': 'integer'}
870889
```
871890

872-
!!! note "Support for`category` and`stacklevel`"
873-
The current implementation of this feature does not take into account the`category` and`stacklevel`
874-
arguments to the`deprecated` decorator. This might land in a future version of Pydantic.
875-
876891
!!! warning "Accessing a deprecated field in validators"
877892
When accessing a deprecated field inside a validator, the deprecation warning will be emitted. You can use
878893
[`catch_warnings`][warnings.catch_warnings] to explicitly ignore it:

‎docs/concepts/models.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,8 @@ for more details).
705705

706706
Here is an example using a generic Pydantic model to create an easily-reused HTTP response payload wrapper:
707707

708+
<!-- TODO: tabs should be auto-generated if using Ruff (https://github.com/pydantic/pydantic/issues/10083)-->
709+
708710
=== "Python 3.9 and above"
709711

710712
```python {upgrade="skip"}

‎mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ plugins:
256256
import:
257257
-url:https://docs.python.org/3/objects.inv
258258
domains:[py, std]
259+
-url:https://typing-extensions.readthedocs.io/en/latest/objects.inv
259260
-redirects:
260261
redirect_maps:
261262
'usage/mypy.md':'integrations/mypy.md'

‎tests/test_deprecated_fields.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
importimportlib.metadata
21
fromtypingimportAnnotated
32

43
importpytest
5-
frompackaging.versionimportVersion
64
fromtyping_extensionsimportSelf,deprecated
75

86
frompydanticimportBaseModel,Field,computed_field,field_validator,model_validator
@@ -35,10 +33,6 @@ class Model(BaseModel):
3533
assertb==1
3634

3735

38-
@pytest.mark.skipif(
39-
Version(importlib.metadata.version('typing_extensions'))<Version('4.9'),
40-
reason='`deprecated` type annotation requires typing_extensions>=4.9',
41-
)
4236
deftest_deprecated_fields_deprecated_class():
4337
classModel(BaseModel):
4438
a:Annotated[int,deprecated('')]
@@ -164,10 +158,6 @@ def p5(self) -> int:
164158
assertp3==1
165159

166160

167-
@pytest.mark.skipif(
168-
Version(importlib.metadata.version('typing_extensions'))<Version('4.9'),
169-
reason='`deprecated` type annotation requires typing_extensions>=4.9',
170-
)
171161
deftest_computed_field_deprecated_deprecated_class():
172162
classModel(BaseModel):
173163
@computed_field(deprecated=deprecated('This is deprecated'))

‎tests/test_json_schema.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
importdataclasses
2-
importimportlib.metadata
32
importjson
43
importmath
54
importre
@@ -30,7 +29,6 @@
3029

3130
importpytest
3231
fromdirty_equalsimportHasRepr
33-
frompackaging.versionimportVersion
3432
frompydantic_coreimportCoreSchema,SchemaValidator,core_schema,to_jsonable_python
3533
frompydantic_core.core_schemaimportValidatorFunctionWrapHandler
3634
fromtyping_extensionsimportTypeAliasType,TypedDict,deprecated
@@ -6213,20 +6211,12 @@ class MyTypedDict(TypedDict):
62136211
]
62146212

62156213

6216-
@pytest.mark.skipif(
6217-
Version(importlib.metadata.version('typing_extensions'))<Version('4.9'),
6218-
reason='`deprecated` type annotation requires typing_extensions>=4.9',
6219-
)
62206214
@pytest.mark.parametrize('cls',_generate_deprecated_classes())
62216215
deftest_deprecated_classes_json_schema(cls):
62226216
asserthasattr(cls,'__deprecated__')
62236217
assertTypeAdapter(cls).json_schema()['deprecated']
62246218

62256219

6226-
@pytest.mark.skipif(
6227-
Version(importlib.metadata.version('typing_extensions'))<Version('4.9'),
6228-
reason='`deprecated` type annotation requires typing_extensions>=4.9',
6229-
)
62306220
@pytest.mark.parametrize('cls',_generate_deprecated_classes())
62316221
deftest_deprecated_subclasses_json_schema(cls):
62326222
classModel(BaseModel):
@@ -6241,10 +6231,6 @@ class Model(BaseModel):
62416231
}
62426232

62436233

6244-
@pytest.mark.skipif(
6245-
Version(importlib.metadata.version('typing_extensions'))<Version('4.9'),
6246-
reason='`deprecated` type annotation requires typing_extensions>=4.9',
6247-
)
62486234
@pytest.mark.parametrize('cls',_generate_deprecated_classes())
62496235
deftest_deprecated_class_usage_warns(cls):
62506236
ifissubclass(cls,dict):

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp