Skip to content

Commit

Permalink
pythongh-71339: Use assertIsSubclass() and assertNotIsSubclass() in t…
Browse files Browse the repository at this point in the history
…est_typing
  • Loading branch information
serhiy-storchaka committed Jan 14, 2025
1 parent e5d8abd commit e970def
Showing 1 changed file with 55 additions and 56 deletions.
111 changes: 55 additions & 56 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,18 @@ def test_errors(self):

def test_can_subclass(self):
class Mock(Any): pass
self.assertTrue(issubclass(Mock, Any))
self.assertIsSubclass(Mock, Any)
self.assertIsInstance(Mock(), Mock)

class Something: pass
self.assertFalse(issubclass(Something, Any))
self.assertNotIsSubclass(Something, Any)
self.assertNotIsInstance(Something(), Mock)

class MockSomething(Something, Mock): pass
self.assertTrue(issubclass(MockSomething, Any))
self.assertTrue(issubclass(MockSomething, MockSomething))
self.assertTrue(issubclass(MockSomething, Something))
self.assertTrue(issubclass(MockSomething, Mock))
self.assertIsSubclass(MockSomething, Any)
self.assertIsSubclass(MockSomething, MockSomething)
self.assertIsSubclass(MockSomething, Something)
self.assertIsSubclass(MockSomething, Mock)
ms = MockSomething()
self.assertIsInstance(ms, MockSomething)
self.assertIsInstance(ms, Something)
Expand Down Expand Up @@ -1997,11 +1997,11 @@ def test_basics(self):
self.assertNotEqual(u, Union)

def test_union_isinstance(self):
self.assertTrue(isinstance(42, Union[int, str]))
self.assertTrue(isinstance('abc', Union[int, str]))
self.assertFalse(isinstance(3.14, Union[int, str]))
self.assertTrue(isinstance(42, Union[int, list[int]]))
self.assertTrue(isinstance(42, Union[int, Any]))
self.assertIsInstance(42, Union[int, str])
self.assertIsInstance('abc', Union[int, str])
self.assertNotIsInstance(3.14, Union[int, str])
self.assertIsInstance(42, Union[int, list[int]])
self.assertIsInstance(42, Union[int, Any])

def test_union_isinstance_type_error(self):
with self.assertRaises(TypeError):
Expand All @@ -2018,9 +2018,9 @@ def test_union_isinstance_type_error(self):
isinstance(42, Union[Any, str])

def test_optional_isinstance(self):
self.assertTrue(isinstance(42, Optional[int]))
self.assertTrue(isinstance(None, Optional[int]))
self.assertFalse(isinstance('abc', Optional[int]))
self.assertIsInstance(42, Optional[int])
self.assertIsInstance(None, Optional[int])
self.assertNotIsInstance('abc', Optional[int])

def test_optional_isinstance_type_error(self):
with self.assertRaises(TypeError):
Expand All @@ -2033,14 +2033,14 @@ def test_optional_isinstance_type_error(self):
isinstance(None, Optional[Any])

def test_union_issubclass(self):
self.assertTrue(issubclass(int, Union[int, str]))
self.assertTrue(issubclass(str, Union[int, str]))
self.assertFalse(issubclass(float, Union[int, str]))
self.assertTrue(issubclass(int, Union[int, list[int]]))
self.assertTrue(issubclass(int, Union[int, Any]))
self.assertFalse(issubclass(int, Union[str, Any]))
self.assertTrue(issubclass(int, Union[Any, int]))
self.assertFalse(issubclass(int, Union[Any, str]))
self.assertIsSubclass(int, Union[int, str])
self.assertIsSubclass(str, Union[int, str])
self.assertNotIsSubclass(float, Union[int, str])
self.assertIsSubclass(int, Union[int, list[int]])
self.assertIsSubclass(int, Union[int, Any])
self.assertNotIsSubclass(int, Union[str, Any])
self.assertIsSubclass(int, Union[Any, int])
self.assertNotIsSubclass(int, Union[Any, str])

def test_union_issubclass_type_error(self):
with self.assertRaises(TypeError):
Expand All @@ -2057,12 +2057,12 @@ def test_union_issubclass_type_error(self):
issubclass(int, Union[list[int], str])

def test_optional_issubclass(self):
self.assertTrue(issubclass(int, Optional[int]))
self.assertTrue(issubclass(type(None), Optional[int]))
self.assertFalse(issubclass(str, Optional[int]))
self.assertTrue(issubclass(Any, Optional[Any]))
self.assertTrue(issubclass(type(None), Optional[Any]))
self.assertFalse(issubclass(int, Optional[Any]))
self.assertIsSubclass(int, Optional[int])
self.assertIsSubclass(type(None), Optional[int])
self.assertNotIsSubclass(str, Optional[int])
self.assertIsSubclass(Any, Optional[Any])
self.assertIsSubclass(type(None), Optional[Any])
self.assertNotIsSubclass(int, Optional[Any])

def test_optional_issubclass_type_error(self):
with self.assertRaises(TypeError):
Expand Down Expand Up @@ -3251,14 +3251,14 @@ def x(self): pass
class EmptyProtocol(Protocol): pass

@runtime_checkable
class SupportsStartsWith(Protocol):
class SupportsStartswith(Protocol):
def startswith(self, x: str) -> bool: ...

@runtime_checkable
class SupportsX(Protocol[T]):
def x(self): ...

for proto in EmptyProtocol, SupportsStartsWith, SupportsX:
for proto in EmptyProtocol, SupportsStartswith, SupportsX:
with self.subTest(proto=proto.__name__):
self.assertIsSubclass(proto, Protocol)

Expand All @@ -3272,8 +3272,8 @@ def x(self): ...
self.assertNotIsSubclass(object, Protocol)
self.assertNotIsInstance(object(), Protocol)

self.assertIsSubclass(str, SupportsStartsWith)
self.assertIsInstance('foo', SupportsStartsWith)
self.assertIsSubclass(str, SupportsStartswith)
self.assertIsInstance('foo', SupportsStartswith)
self.assertNotIsSubclass(str, Protocol)
self.assertNotIsInstance('foo', Protocol)

Expand Down Expand Up @@ -4050,8 +4050,8 @@ def test_generic_protocols_repr(self):

class P(Protocol[T, S]): pass

self.assertTrue(repr(P[T, S]).endswith('P[~T, ~S]'))
self.assertTrue(repr(P[int, str]).endswith('P[int, str]'))
self.assertEndsWith(repr(P[T, S]), 'P[~T, ~S]')
self.assertEndsWith(repr(P[int, str]), 'P[int, str]')

def test_generic_protocols_eq(self):
T = TypeVar('T')
Expand Down Expand Up @@ -4641,8 +4641,7 @@ class C(Generic[T]):
self.assertNotEqual(Z, Y[int])
self.assertNotEqual(Z, Y[T])

self.assertTrue(str(Z).endswith(
'.C[typing.Tuple[str, int]]'))
self.assertEndsWith(str(Z), '.C[typing.Tuple[str, int]]')

def test_new_repr(self):
T = TypeVar('T')
Expand Down Expand Up @@ -4870,12 +4869,12 @@ class A(Generic[T]):
self.assertNotEqual(typing.FrozenSet[A[str]],
typing.FrozenSet[mod_generics_cache.B.A[str]])

self.assertTrue(repr(Tuple[A[str]]).endswith('<locals>.A[str]]'))
self.assertTrue(repr(Tuple[B.A[str]]).endswith('<locals>.B.A[str]]'))
self.assertTrue(repr(Tuple[mod_generics_cache.A[str]])
.endswith('mod_generics_cache.A[str]]'))
self.assertTrue(repr(Tuple[mod_generics_cache.B.A[str]])
.endswith('mod_generics_cache.B.A[str]]'))
self.assertEndsWith(repr(Tuple[A[str]]), '<locals>.A[str]]')
self.assertEndsWith(repr(Tuple[B.A[str]]), '<locals>.B.A[str]]')
self.assertEndsWith(repr(Tuple[mod_generics_cache.A[str]]),
'mod_generics_cache.A[str]]')
self.assertEndsWith(repr(Tuple[mod_generics_cache.B.A[str]]),
'mod_generics_cache.B.A[str]]')

def test_extended_generic_rules_eq(self):
T = TypeVar('T')
Expand Down Expand Up @@ -5817,7 +5816,7 @@ def __call__(self, *args, **kwargs):
@Wrapper
def wrapped(): ...
self.assertIsInstance(wrapped, Wrapper)
self.assertIs(False, hasattr(wrapped, "__final__"))
self.assertNotHasAttr(wrapped, "__final__")

class Meta(type):
@property
Expand All @@ -5829,7 +5828,7 @@ class WithMeta(metaclass=Meta): ...
# Builtin classes throw TypeError if you try to set an
# attribute.
final(int)
self.assertIs(False, hasattr(int, "__final__"))
self.assertNotHasAttr(int, "__final__")

# Make sure it works with common builtin decorators
class Methods:
Expand Down Expand Up @@ -5910,19 +5909,19 @@ def static_method_bad_order():
self.assertEqual(Derived.class_method_good_order(), 42)
self.assertIs(True, Derived.class_method_good_order.__override__)
self.assertEqual(Derived.class_method_bad_order(), 42)
self.assertIs(False, hasattr(Derived.class_method_bad_order, "__override__"))
self.assertNotHasAttr(Derived.class_method_bad_order, "__override__")

self.assertEqual(Derived.static_method_good_order(), 42)
self.assertIs(True, Derived.static_method_good_order.__override__)
self.assertEqual(Derived.static_method_bad_order(), 42)
self.assertIs(False, hasattr(Derived.static_method_bad_order, "__override__"))
self.assertNotHasAttr(Derived.static_method_bad_order, "__override__")

# Base object is not changed:
self.assertIs(False, hasattr(Base.normal_method, "__override__"))
self.assertIs(False, hasattr(Base.class_method_good_order, "__override__"))
self.assertIs(False, hasattr(Base.class_method_bad_order, "__override__"))
self.assertIs(False, hasattr(Base.static_method_good_order, "__override__"))
self.assertIs(False, hasattr(Base.static_method_bad_order, "__override__"))
self.assertNotHasAttr(Base.normal_method, "__override__")
self.assertNotHasAttr(Base.class_method_good_order, "__override__")
self.assertNotHasAttr(Base.class_method_bad_order, "__override__")
self.assertNotHasAttr(Base.static_method_good_order, "__override__")
self.assertNotHasAttr(Base.static_method_bad_order, "__override__")

def test_property(self):
class Base:
Expand All @@ -5947,8 +5946,8 @@ def wrong(self) -> int:
self.assertEqual(instance.correct, 2)
self.assertTrue(Child.correct.fget.__override__)
self.assertEqual(instance.wrong, 2)
self.assertFalse(hasattr(Child.wrong, "__override__"))
self.assertFalse(hasattr(Child.wrong.fset, "__override__"))
self.assertNotHasAttr(Child.wrong, "__override__")
self.assertNotHasAttr(Child.wrong.fset, "__override__")

def test_silent_failure(self):
class CustomProp:
Expand All @@ -5965,7 +5964,7 @@ def some(self):
return 1

self.assertEqual(WithOverride.some, 1)
self.assertFalse(hasattr(WithOverride.some, "__override__"))
self.assertNotHasAttr(WithOverride.some, "__override__")

def test_multiple_decorators(self):
def with_wraps(f): # similar to `lru_cache` definition
Expand Down Expand Up @@ -10474,7 +10473,7 @@ def test_special_attrs2(self):
# to the variable name to which it is assigned". Thus, providing
# __qualname__ is unnecessary.
self.assertEqual(SpecialAttrsT.__name__, 'SpecialAttrsT')
self.assertFalse(hasattr(SpecialAttrsT, '__qualname__'))
self.assertNotHasAttr(SpecialAttrsT, '__qualname__')
self.assertEqual(SpecialAttrsT.__module__, __name__)
# Module-level type variables are picklable.
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
Expand All @@ -10483,7 +10482,7 @@ def test_special_attrs2(self):
self.assertIs(SpecialAttrsT, loaded)

self.assertEqual(SpecialAttrsP.__name__, 'SpecialAttrsP')
self.assertFalse(hasattr(SpecialAttrsP, '__qualname__'))
self.assertNotHasAttr(SpecialAttrsP, '__qualname__')
self.assertEqual(SpecialAttrsP.__module__, __name__)
# Module-level ParamSpecs are picklable.
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
Expand Down

0 comments on commit e970def

Please sign in to comment.