Skip to content

Commit

Permalink
Make everything work
Browse files Browse the repository at this point in the history
  • Loading branch information
dmontagu authored and adriangb committed Sep 20, 2023
1 parent 8772898 commit ea8538b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 20 deletions.
12 changes: 1 addition & 11 deletions pydantic/config.py
@@ -1,15 +1,12 @@
"""Configuration for Pydantic models."""
from __future__ import annotations as _annotations

import warnings
from typing import TYPE_CHECKING, Any, Callable, Dict, Type, Union

from typing_extensions import Literal, TypeAlias, TypedDict

from ._migration import getattr_migration
from .deprecated.config import BaseConfig
from .deprecated.config import Extra as _Extra
from .warnings import PydanticDeprecatedSince20
from .deprecated.config import BaseConfig, Extra

if TYPE_CHECKING:
from ._internal._generate_schema import GenerateSchema as _GenerateSchema
Expand All @@ -24,13 +21,6 @@
Callable[[Dict[str, Any], Type[Any]], None],
]

with warnings.catch_warnings():
# The relevant deprecation warning is still raised on attribute access,
# so it is okay to suppress the one coming from the @deprecated decorator here.
warnings.filterwarnings('ignore', category=PydanticDeprecatedSince20)

Extra = _Extra()

ExtraValues = Literal['allow', 'ignore', 'forbid']


Expand Down
22 changes: 13 additions & 9 deletions pydantic/deprecated/config.py
Expand Up @@ -50,18 +50,22 @@ def __init_subclass__(cls, **kwargs: Any) -> None:
return super().__init_subclass__(**kwargs)


class _ExtraMeta(type):
def __getattribute__(self, __name: str) -> Any:
# The @deprecated decorator accesses other attributes, so we only emit a warning for the expected ones
if __name in {'allow', 'ignore', 'forbid'}:
warnings.warn(
"`pydantic.config.Extra` is deprecated, use literal values instead (e.g. `extra='allow'`)",
DeprecationWarning,
stacklevel=2,
)
return super().__getattribute__(__name)


@deprecated(
"Extra is deprecated. Use literal values instead (e.g. `extra='allow'`)", category=PydanticDeprecatedSince20
)
class Extra:
class Extra(metaclass=_ExtraMeta):
allow: Literal['allow'] = 'allow'
ignore: Literal['ignore'] = 'ignore'
forbid: Literal['forbid'] = 'forbid'

def __getattribute__(self, __name: str) -> Any:
warnings.warn(
"`pydantic.config.Extra` is deprecated, use literal values instead (e.g. `extra='allow'`)",
DeprecationWarning,
stacklevel=2,
)
return super().__getattribute__(__name)

0 comments on commit ea8538b

Please sign in to comment.