cpython: fd889884fe08 (original) (raw)

Mercurial > cpython

changeset 106206:fd889884fe08 3.6

Issue #28556: merge 5 more typing changes from upstream (#340, #344, #348, #349, #350) (3.5->3.6) [#28556]

Guido van Rossum guido@python.org
date Tue, 17 Jan 2017 20:43:30 -0800
parents d47c5a8cccec(current diff)1a9e12a852b2(diff)
children 794dad4b849f 145da99b3df2
files Lib/test/test_typing.py Lib/typing.py
diffstat 2 files changed, 76 insertions(+), 12 deletions(-)[+] [-] Lib/test/test_typing.py 37 Lib/typing.py 51

line wrap: on

line diff

--- a/Lib/test/test_typing.py +++ b/Lib/test/test_typing.py @@ -12,7 +12,7 @@ from typing import T, KT, VT # Not in _ from typing import Union, Optional from typing import Tuple, List, MutableMapping from typing import Callable -from typing import Generic, ClassVar +from typing import Generic, ClassVar, GenericMeta from typing import cast from typing import get_type_hints from typing import no_type_check, no_type_check_decorator @@ -23,6 +23,7 @@ from typing import IO, TextIO, BinaryIO from typing import Pattern, Match import abc import typing +import weakref try: import collections.abc as collections_abc except ImportError: @@ -281,6 +282,15 @@ class UnionTests(BaseTestCase): self.assertFalse(Union[str, typing.Iterable[int]] == typing.Iterable[int]) self.assertTrue(Union[str, typing.Iterable] == typing.Iterable)

+ def test_optional(self): o = Optional[int] u = Union[int, None] @@ -718,6 +728,12 @@ class GenericTests(BaseTestCase): self.assertEqual(C.orig_bases, (List[T][U][V],)) self.assertEqual(D.orig_bases, (C, List[T][U][V]))

+ def test_extended_generic_rules_eq(self): T = TypeVar('T') U = TypeVar('U') @@ -896,6 +912,14 @@ class GenericTests(BaseTestCase): self.assertEqual(t, copy(t)) self.assertEqual(t, deepcopy(t))

+ def test_parameterized_slots(self): T = TypeVar('T') class C(Generic[T]): @@ -1918,7 +1942,9 @@ class NamedTupleTests(BaseTestCase): self.assertEqual(jim.id, 1) self.assertEqual(Emp.name, 'Emp') self.assertEqual(Emp._fields, ('name', 'id'))

@skipUnless(PY36, 'Python 3.6 required') def test_annotation_usage(self): @@ -1929,7 +1955,9 @@ class NamedTupleTests(BaseTestCase): self.assertEqual(tim.cool, 9000) self.assertEqual(CoolEmployee.name, 'CoolEmployee') self.assertEqual(CoolEmployee._fields, ('name', 'cool'))

@skipUnless(PY36, 'Python 3.6 required') def test_namedtuple_keyword_usage(self): @@ -1939,7 +1967,8 @@ class NamedTupleTests(BaseTestCase): self.assertEqual(nick.name, 'Nick') self.assertEqual(LocalEmployee.name, 'LocalEmployee') self.assertEqual(LocalEmployee._fields, ('name', 'age'))

--- a/Lib/typing.py +++ b/Lib/typing.py @@ -27,6 +27,8 @@ except ImportError: # ABCs (from collections.abc). 'AbstractSet', # collections.abc.Set.

def init(self, *args, **kwds): pass @@ -514,7 +516,7 @@ def _replace_arg(arg, tvars, args): if tvars is None: tvars = []

@@ -523,6 +525,16 @@ def _replace_arg(arg, tvars, args): return arg +# Special typing constructs Union, Optional, Generic, Callable and Tuple +# use three special attributes for internal bookkeeping of generic types: +# * parameters is a tuple of unique free type parameters of a generic +# type, for example, Dict[T, T].parameters == (T,); +# * origin keeps a reference to a type that was subscripted, +# e.g., Union[T, int].origin == Union; +# * args is a tuple of all arguments used in subscripting, +# e.g., Dict[T, int].args == (T, int). + + def _subs_tree(cls, tvars=None, args=None): """An internal helper function: calculate substitution tree for generic cls after replacing its type parameters with @@ -757,9 +769,12 @@ class _Union(_FinalTypingBase, _root=Tru return (Union,) + tree_args def eq(self, other):

def hash(self): return self.tree_hash @@ -883,10 +898,26 @@ def _no_slots_copy(dct): class GenericMeta(TypingMeta, abc.ABCMeta):

+

def new(cls, name, bases, namespace, tvars=None, args=None, origin=None, extra=None, orig_bases=None):

@@ -1906,7 +1937,9 @@ def _make_nmtuple(name, types): msg = "NamedTuple('Name', [(f0, t0), (f1, t1), ...]); each t must be a type" types = [(n, _type_check(t, msg)) for n, t in types] nm_tpl = collections.namedtuple(name, [n for n, t in types])