Skip to content

Commit bcde4c6

Browse files
committed
Finish first round of python#136
1 parent 8e6f0a6 commit bcde4c6

2 files changed

Lines changed: 12 additions & 76 deletions

File tree

‎src/test_typing.py‎

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,12 @@ def test_basics(self):
504504
with self.assertRaises(TypeError):
505505
Y[str, str]
506506

507+
def test_generic_errors(self):
508+
with self.assertRaises(TypeError):
509+
isinstance([], List[int])
510+
with self.assertRaises(TypeError):
511+
issubclass(list, List[int])
512+
507513
def test_init(self):
508514
T = TypeVar('T')
509515
S = TypeVar('S')
@@ -753,39 +759,6 @@ def test_no_isinstance(self):
753759
issubclass(int, ClassVar)
754760

755761

756-
class VarianceTests(BaseTestCase):
757-
758-
def test_invariance(self):
759-
# Because of invariance, List[subclass of X] is not a subclass
760-
# of List[X], and ditto for MutableSequence.
761-
self.assertNotIsSubclass(typing.List[Manager], typing.List[Employee])
762-
self.assertNotIsSubclass(typing.MutableSequence[Manager],
763-
typing.MutableSequence[Employee])
764-
# It's still reflexive.
765-
self.assertIsSubclass(typing.List[Employee], typing.List[Employee])
766-
self.assertIsSubclass(typing.MutableSequence[Employee],
767-
typing.MutableSequence[Employee])
768-
769-
def test_covariance_sequence(self):
770-
# Check covariance for Sequence (which is just a generic class
771-
# for this purpose, but using a type variable with covariant=True).
772-
self.assertIsSubclass(typing.Sequence[Manager],
773-
typing.Sequence[Employee])
774-
self.assertNotIsSubclass(typing.Sequence[Employee],
775-
typing.Sequence[Manager])
776-
777-
def test_covariance_mapping(self):
778-
# Ditto for Mapping (covariant in the value, invariant in the key).
779-
self.assertIsSubclass(typing.Mapping[Employee, Manager],
780-
typing.Mapping[Employee, Employee])
781-
self.assertNotIsSubclass(typing.Mapping[Manager, Employee],
782-
typing.Mapping[Employee, Employee])
783-
self.assertNotIsSubclass(typing.Mapping[Employee, Manager],
784-
typing.Mapping[Manager, Manager])
785-
self.assertNotIsSubclass(typing.Mapping[Manager, Employee],
786-
typing.Mapping[Manager, Manager])
787-
788-
789762
class CastTests(BaseTestCase):
790763

791764
def test_basics(self):
@@ -1122,7 +1095,6 @@ def test_iterable(self):
11221095
# path and could fail. So call this a few times.
11231096
self.assertIsInstance([], typing.Iterable)
11241097
self.assertIsInstance([], typing.Iterable)
1125-
self.assertIsInstance([], typing.Iterable[int])
11261098
self.assertNotIsInstance(42, typing.Iterable)
11271099
# Just in case, also test issubclass() a few times.
11281100
self.assertIsSubclass(list, typing.Iterable)
@@ -1131,7 +1103,6 @@ def test_iterable(self):
11311103
def test_iterator(self):
11321104
it = iter([])
11331105
self.assertIsInstance(it, typing.Iterator)
1134-
self.assertIsInstance(it, typing.Iterator[int])
11351106
self.assertNotIsInstance(42, typing.Iterator)
11361107

11371108
@skipUnless(PY35, 'Python 3.5 required')
@@ -1332,10 +1303,6 @@ def foo():
13321303
yield 42
13331304
g = foo()
13341305
self.assertIsSubclass(type(g), typing.Generator)
1335-
self.assertIsSubclass(typing.Generator[Manager, Employee, Manager],
1336-
typing.Generator[Employee, Manager, Employee])
1337-
self.assertNotIsSubclass(typing.Generator[Manager, Manager, Manager],
1338-
typing.Generator[Employee, Employee, Employee])
13391306

13401307
def test_no_generator_instantiation(self):
13411308
with self.assertRaises(TypeError):

‎src/typing.py‎

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ def __new__(cls, parameters=None, _root=False):
538538
if not isinstance(t1, type):
539539
continue
540540
if any(isinstance(t2, type) and issubclass(t1, t2)
541-
for t2 in all_params - {t1} if not isinstance(t2, TypeVar)):
541+
for t2 in all_params - {t1} if not (isinstance(t2, GenericMeta) and t2.__origin__ is not None)):
542542
all_params.remove(t1)
543543
# It's not a union if there's only one type left.
544544
if len(all_params) == 1:
@@ -965,44 +965,13 @@ def __instancecheck__(self, instance):
965965
return self.__subclasscheck__(instance.__class__)
966966

967967
def __subclasscheck__(self, cls):
968-
if cls is Any:
969-
return True
970-
if isinstance(cls, GenericMeta):
971-
# For a covariant class C(Generic[T]),
972-
# C[X] is a subclass of C[Y] iff X is a subclass of Y.
973-
origin = self.__origin__
974-
if origin is not None and origin is cls.__origin__:
975-
assert len(self.__args__) == len(origin.__parameters__)
976-
assert len(cls.__args__) == len(origin.__parameters__)
977-
for p_self, p_cls, p_origin in zip(self.__args__,
978-
cls.__args__,
979-
origin.__parameters__):
980-
if isinstance(p_origin, TypeVar):
981-
if p_origin.__covariant__:
982-
# Covariant -- p_cls must be a subclass of p_self.
983-
if not issubclass(p_cls, p_self):
984-
break
985-
elif p_origin.__contravariant__:
986-
# Contravariant. I think it's the opposite. :-)
987-
if not issubclass(p_self, p_cls):
988-
break
989-
else:
990-
# Invariant -- p_cls and p_self must equal.
991-
if p_self != p_cls:
992-
break
993-
else:
994-
# If the origin's parameter is not a typevar,
995-
# insist on invariance.
996-
if p_self != p_cls:
997-
break
998-
else:
999-
return True
1000-
# If we break out of the loop, the superclass gets a chance.
968+
if self.__origin__ is not None and sys._getframe(1).f_globals['__name__'] != 'abc':
969+
raise TypeError('Parameterized Generic could not be used with class or instance checks')
1001970
if super().__subclasscheck__(cls):
1002971
return True
1003-
if self.__extra__ is None or isinstance(cls, GenericMeta):
1004-
return False
1005-
return issubclass(cls, self.__extra__)
972+
if self.__extra__ is not None:
973+
return issubclass(cls, self.__extra__)
974+
return False
1006975

1007976

1008977
# Prevent checks for Generic to crash when defining Generic.

0 commit comments

Comments
 (0)