mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
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", {})`.
This commit is contained in:
parent
d32e8d6070
commit
7b1f0f204a
5 changed files with 73 additions and 8 deletions
|
@ -7823,6 +7823,40 @@ class TypedDictTests(BaseTestCase):
|
|||
self.assertEqual(MultipleGenericBases.__orig_bases__, (GenericParent[int], GenericParent[float]))
|
||||
self.assertEqual(CallTypedDict.__orig_bases__, (TypedDict,))
|
||||
|
||||
def test_zero_fields_typeddicts(self):
|
||||
T1 = TypedDict("T1", {})
|
||||
class T2(TypedDict): pass
|
||||
class T3[tvar](TypedDict): pass
|
||||
S = TypeVar("S")
|
||||
class T4(TypedDict, Generic[S]): pass
|
||||
|
||||
expected_warning = re.escape(
|
||||
"Failing to pass a value for the 'fields' parameter is deprecated "
|
||||
"and will be disallowed in Python 3.15. "
|
||||
"To create a TypedDict class with 0 fields "
|
||||
"using the functional syntax, "
|
||||
"pass an empty dictionary, e.g. `T5 = TypedDict('T5', {})`."
|
||||
)
|
||||
with self.assertWarnsRegex(DeprecationWarning, fr"^{expected_warning}$"):
|
||||
T5 = TypedDict('T5')
|
||||
|
||||
expected_warning = re.escape(
|
||||
"Passing `None` as the 'fields' parameter is deprecated "
|
||||
"and will be disallowed in Python 3.15. "
|
||||
"To create a TypedDict class with 0 fields "
|
||||
"using the functional syntax, "
|
||||
"pass an empty dictionary, e.g. `T6 = TypedDict('T6', {})`."
|
||||
)
|
||||
with self.assertWarnsRegex(DeprecationWarning, fr"^{expected_warning}$"):
|
||||
T6 = TypedDict('T6', None)
|
||||
|
||||
for klass in T1, T2, T3, T4, T5, T6:
|
||||
with self.subTest(klass=klass.__name__):
|
||||
self.assertEqual(klass.__annotations__, {})
|
||||
self.assertEqual(klass.__required_keys__, set())
|
||||
self.assertEqual(klass.__optional_keys__, set())
|
||||
self.assertIsInstance(klass(), dict)
|
||||
|
||||
|
||||
class RequiredTests(BaseTestCase):
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue