diff options
| author | Alex Waygood <Alex.Waygood@Gmail.com> | 2023-06-14 14:58:41 (GMT) |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-06-14 14:58:41 (GMT) |
| commit | 7b1f0f204a785485de1daf9d26828a81953537e4 (patch) | |
| tree | 6f11456f04444190aa5b2e3bf1cc5ff41f915575 /Lib/typing.py | |
| parent | d32e8d6070057eb7ad0eb2f9d9f1efab38b2cff4 (diff) | |
| download | cpython-7b1f0f204a785485de1daf9d26828a81953537e4.zip cpython-7b1f0f204a785485de1daf9d26828a81953537e4.tar.gz cpython-7b1f0f204a785485de1daf9d26828a81953537e4.tar.bz2 | |
gh-105570: Deprecate unusual ways of creating empty TypedDicts (#105780)
Deprecate two methods of creating typing.TypedDict classes with 0 fields using the functional syntax: `TD = TypedDict("TD")` and `TD = TypedDict("TD", None)`. Both will be disallowed in Python 3.15. To create a TypedDict class with 0 fields, either use `class TD(TypedDict): pass` or `TD = TypedDict("TD", {})`.
Diffstat (limited to 'Lib/typing.py')
| -rw-r--r-- | Lib/typing.py | 19 |
1 files changed, 17 insertions, 2 deletions
diff --git a/Lib/typing.py b/Lib/typing.py index 570cb80..1dd9398 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -2908,7 +2908,7 @@ class _TypedDictMeta(type): __instancecheck__ = __subclasscheck__ -def TypedDict(typename, fields=None, /, *, total=True): +def TypedDict(typename, fields=_sentinel, /, *, total=True): """A simple typed namespace. At runtime it is equivalent to a plain dict. TypedDict creates a dictionary type such that a type checker will expect all @@ -2955,7 +2955,22 @@ def TypedDict(typename, fields=None, /, *, total=True): See PEP 655 for more details on Required and NotRequired. """ - if fields is None: + if fields is _sentinel or fields is None: + import warnings + + if fields is _sentinel: + deprecated_thing = "Failing to pass a value for the 'fields' parameter" + else: + deprecated_thing = "Passing `None` as the 'fields' parameter" + + example = f"`{typename} = TypedDict({typename!r}, {{{{}}}})`" + deprecation_msg = ( + "{name} is deprecated and will be disallowed in Python {remove}. " + "To create a TypedDict class with 0 fields " + "using the functional syntax, " + "pass an empty dictionary, e.g. " + ) + example + "." + warnings._deprecated(deprecated_thing, message=deprecation_msg, remove=(3, 15)) fields = {} ns = {'__annotations__': dict(fields)} |
