Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mark Extra as deprecated #7299

Merged
merged 6 commits into from Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 1 addition & 4 deletions pydantic/config.py
Expand Up @@ -6,8 +6,7 @@
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 .deprecated.config import BaseConfig, Extra

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

Extra = _Extra()

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


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


class Extra:
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(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)