8000 gh-104786: Remove kwargs-based TypedDict creation by tomasr8 · Pull Request #104891 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-104786: Remove kwargs-based TypedDict creation #104891

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

Merged
merged 10 commits into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Remove kwargs-based TypedDict creation
  • Loading branch information
tomasr8 committed May 24, 2023
commit bfbbc29e112e4bcd24040b075fd7fbb148257b02
33 changes: 3 additions & 30 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -6951,35 +6951,6 @@ def test_basics_functional_syntax(self):
self.assertEqual(Emp.__annotations__, {'name': str, 'id': int})
self.assertEqual(Emp.__total__, True)

def test_basics_keywords_syntax(self):
with self.assertWarns(DeprecationWarning):
Emp = TypedDict('Emp', name=str, id=int)
self.assertIsSubclass(Emp, dict)
self.assertIsSubclass(Emp, typing.MutableMapping)
self.assertNotIsSubclass(Emp, collections.abc.Sequence)
jim = Emp(name='Jim', id=1)
self.assertIs(type(jim), dict)
self.assertEqual(jim['name'], 'Jim')
self.assertEqual(jim['id'], 1)
self.assertEqual(Emp.__name__, 'Emp')
self.assertEqual(Emp.__module__, __name__)
self.assertEqual(Emp.__bases__, (dict,))
self.assertEqual(Emp.__annotations__, {'name': str, 'id': int})
self.assertEqual(Emp.__total__, True)

def test_typeddict_special_keyword_names(self):
with self.assertWarns(DeprecationWarning):
TD = TypedDict("TD", cls=type, self=object, typename=str, _typename=int, fields=list, _fields=dict)
self.assertEqual(TD.__name__, 'TD')
self.assertEqual(TD.__annotations__, {'cls': type, 'self': object, 'typename': str, '_typename': int, 'fields': list, '_fields': dict})
a = TD(cls=str, self=42, typename='foo', _typename=53, fields=[('bar', tuple)], _fields={'baz', set})
self.assertEqual(a['cls'], str)
self.assertEqual(a['self'], 42)
self.assertEqual(a['typename'], 'foo')
self.assertEqual(a['_typename'], 53)
self.assertEqual(a['fields'], [('bar', tuple)])
self.assertEqual(a['_fields'], {'baz', set})

def test_typeddict_create_errors(self):
with self.assertRaises(TypeError):
TypedDict.__new__()
Expand All @@ -6988,7 +6959,9 @@ def test_typeddict_create_errors(self):
with self.assertRaises(TypeError):
TypedDict('Emp', [('name', str)], None)
with self.assertRaises(TypeError):
TypedDict(_typename='Emp', name=str, id=int)
TypedDict(_typename='Emp')
with self.assertRaises(TypeError):
TypedDict('Emp', name=str, id=int)

def test_typeddict_errors(self):
Emp = TypedDict('Emp', {'name': str, 'id': int})
Expand Down
15 changes: 2 additions & 13 deletions Lib/typing.py
4B26
Original file line number Diff line number Diff line change
Expand Up @@ -2822,7 +2822,7 @@ def __subclasscheck__(cls, other):
__instancecheck__ = __subclasscheck__


def TypedDict(typename, fields=None, /, *, total=True, **kwargs):
def TypedDict(typename, fields=None, /, *, total=True):
"""A simple typed namespace. At runtime it is equivalent to a plain dict.

TypedDict creates a dictionary type that expects all of its
Expand Down Expand Up @@ -2864,18 +2864,7 @@ class body be required.
syntax form works for Python 2.7 and 3.2+
"""
if fields is None:
fields = kwargs
elif kwargs:
raise TypeError("TypedDict takes either a dict or keyword arguments,"
" but not both")
if kwargs:
warnings.warn(
"The kwargs-based syntax for TypedDict definitions is deprecated "
"in Python 3.11, will be removed in Python 3.13, and may not be "
"understood by third-party type checkers.",
DeprecationWarning,
stacklevel=2,
)
fields = {}

ns = {'__annotations__': dict(fields)}
module = _caller()
Expand Down
0