Skip to content

Commit

Permalink
Fix usage of @deprecated (#8294)
Browse files Browse the repository at this point in the history
  • Loading branch information
Viicos committed Jan 9, 2024
1 parent dc7d01a commit 54d1185
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 111 deletions.
11 changes: 5 additions & 6 deletions pdm.lock

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

8 changes: 4 additions & 4 deletions pydantic/_internal/_model_construction.py
Expand Up @@ -247,11 +247,11 @@ def _collect_bases_data(bases: tuple[type[Any], ...]) -> tuple[set[str], set[str
return field_names, class_vars, private_attributes

@property
@deprecated(
'The `__fields__` attribute is deprecated, use `model_fields` instead.', category=PydanticDeprecatedSince20
)
@deprecated('The `__fields__` attribute is deprecated, use `model_fields` instead.', category=None)
def __fields__(self) -> dict[str, FieldInfo]:
warnings.warn('The `__fields__` attribute is deprecated, use `model_fields` instead.', DeprecationWarning)
warnings.warn(
'The `__fields__` attribute is deprecated, use `model_fields` instead.', PydanticDeprecatedSince20
)
return self.model_fields # type: ignore

def __dir__(self) -> list[str]:
Expand Down
14 changes: 7 additions & 7 deletions pydantic/deprecated/decorator.py
Expand Up @@ -26,25 +26,25 @@


@overload
@deprecated(
'The `validate_arguments` method is deprecated; use `validate_call` instead.', category=PydanticDeprecatedSince20
)
def validate_arguments(func: None = None, *, config: 'ConfigType' = None) -> Callable[['AnyCallableT'], 'AnyCallableT']:
...


@overload
@deprecated(
'The `validate_arguments` method is deprecated; use `validate_call` instead.', category=PydanticDeprecatedSince20
)
def validate_arguments(func: 'AnyCallableT') -> 'AnyCallableT':
...


@deprecated(
'The `validate_arguments` method is deprecated; use `validate_call` instead.',
category=None,
)
def validate_arguments(func: Optional['AnyCallableT'] = None, *, config: 'ConfigType' = None) -> Any:
"""Decorator to validate the arguments passed to a function."""
warnings.warn(
'The `validate_arguments` method is deprecated; use `validate_call` instead.', DeprecationWarning, stacklevel=2
'The `validate_arguments` method is deprecated; use `validate_call` instead.',
PydanticDeprecatedSince20,
stacklevel=2,
)

def validate(_func: 'AnyCallable') -> 'AnyCallable':
Expand Down
24 changes: 17 additions & 7 deletions pydantic/deprecated/json.py
Expand Up @@ -79,14 +79,19 @@ def decimal_encoder(dec_value: Decimal) -> Union[int, float]:


@deprecated(
'pydantic_encoder is deprecated, use pydantic_core.to_jsonable_python instead.', category=PydanticDeprecatedSince20
'`pydantic_encoder` is deprecated, use `pydantic_core.to_jsonable_python` instead.',
category=None,
)
def pydantic_encoder(obj: Any) -> Any:
warnings.warn(
'`pydantic_encoder` is deprecated, use `pydantic_core.to_jsonable_python` instead.',
category=PydanticDeprecatedSince20,
stacklevel=2,
)
from dataclasses import asdict, is_dataclass

from ..main import BaseModel

warnings.warn('pydantic_encoder is deprecated, use BaseModel.model_dump instead.', DeprecationWarning, stacklevel=2)
if isinstance(obj, BaseModel):
return obj.model_dump()
elif is_dataclass(obj):
Expand All @@ -104,12 +109,17 @@ def pydantic_encoder(obj: Any) -> Any:


# TODO: Add a suggested migration path once there is a way to use custom encoders
@deprecated('custom_pydantic_encoder is deprecated.', category=PydanticDeprecatedSince20)
@deprecated(
'`custom_pydantic_encoder` is deprecated, use `BaseModel.model_dump` instead.',
category=None,
)
def custom_pydantic_encoder(type_encoders: Dict[Any, Callable[[Type[Any]], Any]], obj: Any) -> Any:
# Check the class type and its superclasses for a matching encoder
warnings.warn(
'custom_pydantic_encoder is deprecated, use BaseModel.model_dump instead.', DeprecationWarning, stacklevel=2
'`custom_pydantic_encoder` is deprecated, use `BaseModel.model_dump` instead.',
category=PydanticDeprecatedSince20,
stacklevel=2,
)
# Check the class type and its superclasses for a matching encoder
for base in obj.__class__.__mro__[:-1]:
try:
encoder = type_encoders[base]
Expand All @@ -121,10 +131,10 @@ def custom_pydantic_encoder(type_encoders: Dict[Any, Callable[[Type[Any]], Any]]
return pydantic_encoder(obj)


@deprecated('timedelta_isoformat is deprecated.', category=PydanticDeprecatedSince20)
@deprecated('`timedelta_isoformat` is deprecated.', category=None)
def timedelta_isoformat(td: datetime.timedelta) -> str:
"""ISO 8601 encoding for Python timedelta object."""
warnings.warn('timedelta_isoformat is deprecated.', DeprecationWarning, stacklevel=2)
warnings.warn('`timedelta_isoformat` is deprecated.', category=PydanticDeprecatedSince20, stacklevel=2)
minutes, seconds = divmod(td.seconds, 60)
hours, minutes = divmod(minutes, 60)
return f'{"-" if td.days < 0 else ""}P{abs(td.days)}DT{hours:d}H{minutes:d}M{seconds:d}.{td.microseconds:06d}S'
8 changes: 4 additions & 4 deletions pydantic/deprecated/parse.py
Expand Up @@ -22,7 +22,7 @@ class Protocol(str, Enum):
pickle = 'pickle'


@deprecated('load_str_bytes is deprecated.', category=PydanticDeprecatedSince20)
@deprecated('`load_str_bytes` is deprecated.', category=None)
def load_str_bytes(
b: str | bytes,
*,
Expand All @@ -32,7 +32,7 @@ def load_str_bytes(
allow_pickle: bool = False,
json_loads: Callable[[str], Any] = json.loads,
) -> Any:
warnings.warn('load_str_bytes is deprecated.', DeprecationWarning, stacklevel=2)
warnings.warn('`load_str_bytes` is deprecated.', category=PydanticDeprecatedSince20, stacklevel=2)
if proto is None and content_type:
if content_type.endswith(('json', 'javascript')):
pass
Expand All @@ -56,7 +56,7 @@ def load_str_bytes(
raise TypeError(f'Unknown protocol: {proto}')


@deprecated('load_file is deprecated.', category=PydanticDeprecatedSince20)
@deprecated('`load_file` is deprecated.', category=None)
def load_file(
path: str | Path,
*,
Expand All @@ -66,7 +66,7 @@ def load_file(
allow_pickle: bool = False,
json_loads: Callable[[str], Any] = json.loads,
) -> Any:
warnings.warn('load_file is deprecated.', DeprecationWarning, stacklevel=2)
warnings.warn('`load_file` is deprecated.', category=PydanticDeprecatedSince20, stacklevel=2)
path = Path(path)
b = path.read_bytes()
if content_type is None:
Expand Down
21 changes: 14 additions & 7 deletions pydantic/deprecated/tools.py
Expand Up @@ -24,12 +24,13 @@


@deprecated(
'parse_obj_as is deprecated. Use pydantic.TypeAdapter.validate_python instead.', category=PydanticDeprecatedSince20
'`parse_obj_as` is deprecated. Use `pydantic.TypeAdapter.validate_python` instead.',
category=None,
)
def parse_obj_as(type_: type[T], obj: Any, type_name: NameFactory | None = None) -> T:
warnings.warn(
'parse_obj_as is deprecated. Use pydantic.TypeAdapter.validate_python instead.',
DeprecationWarning,
'`parse_obj_as` is deprecated. Use `pydantic.TypeAdapter.validate_python` instead.',
category=PydanticDeprecatedSince20,
stacklevel=2,
)
if type_name is not None: # pragma: no cover
Expand All @@ -42,7 +43,8 @@ def parse_obj_as(type_: type[T], obj: Any, type_name: NameFactory | None = None)


@deprecated(
'schema_of is deprecated. Use pydantic.TypeAdapter.json_schema instead.', category=PydanticDeprecatedSince20
'`schema_of` is deprecated. Use `pydantic.TypeAdapter.json_schema` instead.',
category=None,
)
def schema_of(
type_: Any,
Expand All @@ -54,7 +56,9 @@ def schema_of(
) -> dict[str, Any]:
"""Generate a JSON schema (as dict) for the passed model or dynamically generated one."""
warnings.warn(
'schema_of is deprecated. Use pydantic.TypeAdapter.json_schema instead.', DeprecationWarning, stacklevel=2
'`schema_of` is deprecated. Use `pydantic.TypeAdapter.json_schema` instead.',
category=PydanticDeprecatedSince20,
stacklevel=2,
)
res = TypeAdapter(type_).json_schema(
by_alias=by_alias,
Expand All @@ -75,7 +79,8 @@ def schema_of(


@deprecated(
'schema_json_of is deprecated. Use pydantic.TypeAdapter.json_schema instead.', category=PydanticDeprecatedSince20
'`schema_json_of` is deprecated. Use `pydantic.TypeAdapter.json_schema` instead.',
category=None,
)
def schema_json_of(
type_: Any,
Expand All @@ -88,7 +93,9 @@ def schema_json_of(
) -> str:
"""Generate a JSON schema (as JSON) for the passed model or dynamically generated one."""
warnings.warn(
'schema_json_of is deprecated. Use pydantic.TypeAdapter.json_schema instead.', DeprecationWarning, stacklevel=2
'`schema_json_of` is deprecated. Use `pydantic.TypeAdapter.json_schema` instead.',
category=PydanticDeprecatedSince20,
stacklevel=2,
)
return json.dumps(
schema_of(type_, title=title, by_alias=by_alias, ref_template=ref_template, schema_generator=schema_generator),
Expand Down

0 comments on commit 54d1185

Please sign in to comment.