diff options
author | Ivan Levkivskyi <levkivskyi@gmail.com> | 2019-05-26 08:39:24 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-26 08:39:24 (GMT) |
commit | 135c6a56e55d2f4f8718b3b9f03ce3c692b15f0f (patch) | |
tree | b585115cb0b9684430a7a3ffe02b2a0df584890a /Lib/typing.py | |
parent | b891c465bb7d38a597c5c2ad547d7b19194f4dad (diff) | |
download | cpython-135c6a56e55d2f4f8718b3b9f03ce3c692b15f0f.zip cpython-135c6a56e55d2f4f8718b3b9f03ce3c692b15f0f.tar.gz cpython-135c6a56e55d2f4f8718b3b9f03ce3c692b15f0f.tar.bz2 |
bpo-37049: PEP 589: Add TypedDict to typing module (GH-13573)
The implementation is straightforward and essentially is just copied from `typing_extensions`.
Diffstat (limited to 'Lib/typing.py')
-rw-r--r-- | Lib/typing.py | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/Lib/typing.py b/Lib/typing.py index 1044cc4..d3e84cd 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -89,6 +89,7 @@ __all__ = [ 'Set', 'FrozenSet', 'NamedTuple', # Not really a type. + 'TypedDict', # Not really a type. 'Generator', # One-off things. @@ -1490,6 +1491,89 @@ class NamedTuple(metaclass=NamedTupleMeta): return _make_nmtuple(typename, fields) +def _dict_new(cls, *args, **kwargs): + return dict(*args, **kwargs) + + +def _typeddict_new(cls, _typename, _fields=None, **kwargs): + total = kwargs.pop('total', True) + if _fields is None: + _fields = kwargs + elif kwargs: + raise TypeError("TypedDict takes either a dict or keyword arguments," + " but not both") + + ns = {'__annotations__': dict(_fields), '__total__': total} + 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) + + +def _check_fails(cls, other): + # Typed dicts are only for static structural subtyping. + raise TypeError('TypedDict does not support instance and class checks') + + +class _TypedDictMeta(type): + def __new__(cls, name, bases, ns, total=True): + """Create new typed dict class object. + + This method is called directly when TypedDict is subclassed, + or via _typeddict_new when TypedDict is instantiated. This way + TypedDict supports all three syntax forms described in its docstring. + Subclasses and instances of TypedDict return actual dictionaries + via _dict_new. + """ + ns['__new__'] = _typeddict_new if name == 'TypedDict' else _dict_new + tp_dict = super(_TypedDictMeta, cls).__new__(cls, name, (dict,), ns) + + anns = ns.get('__annotations__', {}) + msg = "TypedDict('Name', {f0: t0, f1: t1, ...}); each t must be a type" + anns = {n: _type_check(tp, msg) for n, tp in anns.items()} + for base in bases: + anns.update(base.__dict__.get('__annotations__', {})) + tp_dict.__annotations__ = anns + if not hasattr(tp_dict, '__total__'): + tp_dict.__total__ = total + return tp_dict + + __instancecheck__ = __subclasscheck__ = _check_fails + + +class TypedDict(dict, metaclass=_TypedDictMeta): + """A simple typed namespace. At runtime it is equivalent to a plain dict. + + TypedDict creates a dictionary type that expects all of its + instances to have a certain set of keys, where each key is + associated with a value of a consistent type. This expectation + is not checked at runtime but is only enforced by type checkers. + Usage:: + + class Point2D(TypedDict): + x: int + y: int + label: str + + a: Point2D = {'x': 1, 'y': 2, 'label': 'good'} # OK + b: Point2D = {'z': 3, 'label': 'bad'} # Fails type check + + assert Point2D(x=1, y=2, label='first') == dict(x=1, y=2, label='first') + + The type info can be accessed via Point2D.__annotations__. TypedDict + supports two additional equivalent forms:: + + Point2D = TypedDict('Point2D', x=int, y=int, label=str) + Point2D = TypedDict('Point2D', {'x': int, 'y': int, 'label': str}) + + The class syntax is only supported in Python 3.6+, while two other + syntax forms work for Python 2.7 and 3.2+ + """ + + def NewType(name, tp): """NewType creates simple unique types with almost zero runtime overhead. NewType(name, tp) is considered a subtype of tp |