8000 Fixed required/optional keys with old-style TypedDict by domdfcoding · Pull Request #778 · python/typing · GitHub
[go: up one dir, main page]

Skip to content

Fixed required/optional keys with old-style TypedDict #778

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 3 commits into from
Apr 11, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion typing_extensions/src_py3/test_typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1530,7 +1530,7 @@ def test_typeddict_create_errors(self):

def test_typeddict_errors(self):
Emp = TypedDict('Emp', {'name': str, 'id': int})
if sys.version_info[:2] >= (3, 9):
if sys.version_info >= (3, 9, 2):
self.assertEqual(TypedDict.__module__, 'typing')
else:
self.assertEqual(TypedDict.__module__, 'typing_extensions')
Expand Down Expand Up @@ -1586,11 +1586,15 @@ def test_total(self):
self.assertEqual(D(), {})
self.assertEqual(D(x=1), {'x': 1})
self.assertEqual(D.__total__, False)
self.assertEqual(D.__required_keys__, frozenset())
self.assertEqual(D.__optional_keys__, {'x'})

if PY36:
self.assertEqual(Options(), {})
self.assertEqual(Options(log_level=2), {'log_level': 2})
self.assertEqual(Options.__total__, False)
self.assertEqual(Options.__required_keys__, frozenset())
self.assertEqual(Options.__optional_keys__, {'log_level', 'log_path'})

@skipUnless(PY36, 'Python 3.6 required')
def test_optional_keys(self):
Expand Down
13 changes: 10 additions & 3 deletions typing_extensions/src_py3/typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1618,9 +1618,11 @@ def __index__(self) -> int:
pass


if sys.version_info[:2] >= (3, 9):
if sys.version_info >= (3, 9, 2):
# The standard library TypedDict in Python 3.8 does not store runtime information
# about which (if any) keys are optional. See https://bugs.python.org/issue38834
# The standard library TypedDict in Python 3.9.0/1 does not honour the "total"
# keyword with old-style TypedDict(). See https://bugs.python.org/issue42059
TypedDict = typing.TypedDict
else:
def _check_fails(cls, other):
Expand Down Expand Up @@ -1677,19 +1679,24 @@ def _typeddict_new(*args, total=True, **kwargs):
raise TypeError("TypedDict takes either a dict or keyword arguments,"
" but not both")

ns = {'__annotations__': dict(fields), '__total__': total}
ns = {'__annotations__': dict(fields)}
try:
# Setting correct module is necessary to make typed dict classes pickleable.
ns['__module__'] = sys._getframe(1).f_globals.get('__name__', '__main__')
except (AttributeError, ValueError):
pass

return _TypedDictMeta(typename, (), ns)
return _TypedDictMeta(typename, (), ns, total=total)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On Python 3.4 and 3.5 it seems that _TypedDictMeta(...) calls __init__ rather than __new__, but __init__ isn't implemented. See, for instance: https://travis-ci.com/github/python/typing/jobs/473002561#L338

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks nasty. I wonder if it's related to this note in https://docs.python.org/3/whatsnew/3.6.html#changes-in-the-python-api :

As part of PEP 487, the handling of keyword arguments passed to type (other than the metaclass hint, metaclass) is now consistently delegated to object.init_subclass(). This means that type.new() and type.init() both now accept arbitrary keyword arguments, but object.init_subclass() (which is called from type.new()) will reject them by default. Custom metaclasses accepting additional keyword arguments will need to adjust their calls to type.new() (whether direct or via super) accordingly.

I just tried and failed to get pyenv to compile 3.5 for me so I can't try this out myself :( If it's too hard to get this to work I wouldn't be opposed to fixing it for 3.6+ only for now.


_typeddict_new.__text_signature__ = ('($cls, _typename, _fields=None,'
' /, *, total=True, **kwargs)')

class _TypedDictMeta(type):
def __init__(cls, name, bases, ns, total=True):
# In Python 3.4 and 3.5 the __init__ method also needs to support the keyword arguments.
# See https://www.python.org/dev/peps/pep-0487/#implementation-details
super(_TypedDictMeta, cls).__init__(name, bases, ns)

def __new__(cls, name, bases, ns, total=True):
# Create new typed dict class object.
# This method is called directly when TypedDict is subclassed,
Expand Down
0