-
Notifications
You must be signed in to change notification settings - Fork 259
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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): | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On Python 3.4 and 3.5 it seems that There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 :
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, | ||
|
Uh oh!
There was an error while loading. Please reload this page.