typing, annotationlib: clean tests (#133087)

- Add @cpython_only decorator to lazy import tests
- Rename reference to SOURCE format
- Always two newlines between test case classes
- Merge two classes of ForwardRef tests
- Use get_annotations instead of annotationlib.get_annotations
- Format test_annotationlib with Black (not expecting that we'll keep this up
  but it's close to Black-formatted right now)
This commit is contained in:
Jelle Zijlstra 2025-04-28 08:38:11 -07:00 committed by GitHub
parent 4cec0b510b
commit 7f16f1bc11
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 327 additions and 355 deletions

File diff suppressed because it is too large Load Diff

View File

@ -53,7 +53,10 @@
captured_stderr, cpython_only, infinite_recursion, requires_docstrings, import_helper, run_code,
EqualToForwardRef,
)
from test.typinganndata import ann_module695, mod_generics_cache, _typed_dict_helper
from test.typinganndata import (
ann_module695, mod_generics_cache, _typed_dict_helper,
ann_module, ann_module2, ann_module3, ann_module5, ann_module6, ann_module8
)
CANNOT_SUBCLASS_TYPE = 'Cannot subclass special typing classes'
@ -377,6 +380,7 @@ def test_alias(self):
self.assertEqual(get_args(alias_2), (LiteralString,))
self.assertEqual(get_args(alias_3), (LiteralString,))
class TypeVarTests(BaseTestCase):
def test_basic_plain(self):
T = TypeVar('T')
@ -629,7 +633,7 @@ class TypeParameterDefaultsTests(BaseTestCase):
def test_typevar(self):
T = TypeVar('T', default=int)
self.assertEqual(T.__default__, int)
self.assertTrue(T.has_default())
self.assertIs(T.has_default(), True)
self.assertIsInstance(T, TypeVar)
class A(Generic[T]): ...
@ -639,19 +643,19 @@ def test_typevar_none(self):
U = TypeVar('U')
U_None = TypeVar('U_None', default=None)
self.assertIs(U.__default__, NoDefault)
self.assertFalse(U.has_default())
self.assertIs(U.has_default(), False)
self.assertIs(U_None.__default__, None)
self.assertTrue(U_None.has_default())
self.assertIs(U_None.has_default(), True)
class X[T]: ...
T, = X.__type_params__
self.assertIs(T.__default__, NoDefault)
self.assertFalse(T.has_default())
self.assertIs(T.has_default(), False)
def test_paramspec(self):
P = ParamSpec('P', default=(str, int))
self.assertEqual(P.__default__, (str, int))
self.assertTrue(P.has_default())
self.assertIs(P.has_default(), True)
self.assertIsInstance(P, ParamSpec)
class A(Generic[P]): ...
@ -664,19 +668,19 @@ def test_paramspec_none(self):
U = ParamSpec('U')
U_None = ParamSpec('U_None', default=None)
self.assertIs(U.__default__, NoDefault)
self.assertFalse(U.has_default())
self.assertIs(U.has_default(), False)
self.assertIs(U_None.__default__, None)
self.assertTrue(U_None.has_default())
self.assertIs(U_None.has_default(), True)
class X[**P]: ...
P, = X.__type_params__
self.assertIs(P.__default__, NoDefault)
self.assertFalse(P.has_default())
self.assertIs(P.has_default(), False)
def test_typevartuple(self):
Ts = TypeVarTuple('Ts', default=Unpack[Tuple[str, int]])
self.assertEqual(Ts.__default__, Unpack[Tuple[str, int]])
self.assertTrue(Ts.has_default())
self.assertIs(Ts.has_default(), True)
self.assertIsInstance(Ts, TypeVarTuple)
class A(Generic[Unpack[Ts]]): ...
@ -762,14 +766,14 @@ def test_typevartuple_none(self):
U = TypeVarTuple('U')
U_None = TypeVarTuple('U_None', default=None)
self.assertIs(U.__default__, NoDefault)
self.assertFalse(U.has_default())
self.assertIs(U.has_default(), False)
self.assertIs(U_None.__default__, None)
self.assertTrue(U_None.has_default())
self.assertIs(U_None.has_default(), True)
class X[**Ts]: ...
Ts, = X.__type_params__
self.assertIs(Ts.__default__, NoDefault)
self.assertFalse(Ts.has_default())
self.assertIs(Ts.has_default(), False)
def test_no_default_after_non_default(self):
DefaultStrT = TypeVar('DefaultStrT', default=str)
@ -1170,7 +1174,6 @@ class C(Generic[*Ts]): pass
)
class UnpackTests(BaseTestCase):
def test_accepts_single_type(self):
@ -2186,8 +2189,8 @@ def test_cannot_instantiate(self):
type(u)()
def test_union_generalization(self):
self.assertFalse(Union[str, typing.Iterable[int]] == str)
self.assertFalse(Union[str, typing.Iterable[int]] == typing.Iterable[int])
self.assertNotEqual(Union[str, typing.Iterable[int]], str)
self.assertNotEqual(Union[str, typing.Iterable[int]], typing.Iterable[int])
self.assertIn(str, Union[str, typing.Iterable[int]].__args__)
self.assertIn(typing.Iterable[int], Union[str, typing.Iterable[int]].__args__)
@ -2603,6 +2606,7 @@ def test_errors(self):
with self.assertRaisesRegex(TypeError, "few arguments for"):
C1[int]
class TypingCallableTests(BaseCallableTests, BaseTestCase):
Callable = typing.Callable
@ -2780,6 +2784,7 @@ class Coordinate(Protocol):
x: int
y: int
@runtime_checkable
class Point(Coordinate, Protocol):
label: str
@ -4120,12 +4125,12 @@ class PG(Protocol[T]):
def meth(self):
pass
self.assertTrue(P._is_protocol)
self.assertTrue(PR._is_protocol)
self.assertTrue(PG._is_protocol)
self.assertFalse(P._is_runtime_protocol)
self.assertTrue(PR._is_runtime_protocol)
self.assertTrue(PG[int]._is_protocol)
self.assertIs(P._is_protocol, True)
self.assertIs(PR._is_protocol, True)
self.assertIs(PG._is_protocol, True)
self.assertIs(P._is_runtime_protocol, False)
self.assertIs(PR._is_runtime_protocol, True)
self.assertIs(PG[int]._is_protocol, True)
self.assertEqual(typing._get_protocol_attrs(P), {'meth'})
self.assertEqual(typing._get_protocol_attrs(PR), {'x'})
self.assertEqual(frozenset(typing._get_protocol_attrs(PG)),
@ -5838,6 +5843,7 @@ def test_no_isinstance(self):
with self.assertRaises(TypeError):
issubclass(int, ClassVar)
class FinalTests(BaseTestCase):
def test_basics(self):
@ -6043,7 +6049,7 @@ def wrong(self) -> int:
instance = Child()
self.assertEqual(instance.correct, 2)
self.assertTrue(Child.correct.fget.__override__)
self.assertIs(Child.correct.fget.__override__, True)
self.assertEqual(instance.wrong, 2)
self.assertNotHasAttr(Child.wrong, "__override__")
self.assertNotHasAttr(Child.wrong.fset, "__override__")
@ -6084,9 +6090,9 @@ def on_bottom(self, a: int) -> int:
instance = WithOverride()
self.assertEqual(instance.on_top(1), 2)
self.assertTrue(instance.on_top.__override__)
self.assertIs(instance.on_top.__override__, True)
self.assertEqual(instance.on_bottom(1), 3)
self.assertTrue(instance.on_bottom.__override__)
self.assertIs(instance.on_bottom.__override__, True)
class CastTests(BaseTestCase):
@ -6124,8 +6130,6 @@ def test_errors(self):
# We need this to make sure that `@no_type_check` respects `__module__` attr:
from test.typinganndata import ann_module8
@no_type_check
class NoTypeCheck_Outer:
Inner = ann_module8.NoTypeCheck_Outer.Inner
@ -6193,7 +6197,7 @@ class D:
for klass in [A, A.B, A.B.C, A.D]:
with self.subTest(klass=klass):
self.assertTrue(klass.__no_type_check__)
self.assertIs(klass.__no_type_check__, True)
self.assertEqual(get_type_hints(klass), {})
for not_modified in [Other, B]:
@ -6210,19 +6214,19 @@ def st(x: int) -> int: ...
@classmethod
def cl(cls, y: int) -> int: ...
self.assertTrue(Some.st.__no_type_check__)
self.assertIs(Some.st.__no_type_check__, True)
self.assertEqual(get_type_hints(Some.st), {})
self.assertTrue(Some.cl.__no_type_check__)
self.assertIs(Some.cl.__no_type_check__, True)
self.assertEqual(get_type_hints(Some.cl), {})
def test_no_type_check_other_module(self):
self.assertTrue(NoTypeCheck_Outer.__no_type_check__)
self.assertIs(NoTypeCheck_Outer.__no_type_check__, True)
with self.assertRaises(AttributeError):
ann_module8.NoTypeCheck_Outer.__no_type_check__
with self.assertRaises(AttributeError):
ann_module8.NoTypeCheck_Outer.Inner.__no_type_check__
self.assertTrue(NoTypeCheck_WithFunction.__no_type_check__)
self.assertIs(NoTypeCheck_WithFunction.__no_type_check__, True)
with self.assertRaises(AttributeError):
ann_module8.NoTypeCheck_function.__no_type_check__
@ -6247,7 +6251,7 @@ class A:
# Corner case: `lambda` is both an assignment and a function:
bar: Callable[[int], int] = lambda arg: arg
self.assertTrue(A.bar.__no_type_check__)
self.assertIs(A.bar.__no_type_check__, True)
self.assertEqual(get_type_hints(A.bar), {})
def test_no_type_check_TypeError(self):
@ -6334,6 +6338,7 @@ def test_collect_parameters(self):
typing._collect_parameters
self.assertEqual(cm.filename, __file__)
@cpython_only
def test_lazy_import(self):
import_helper.ensure_lazy_imports("typing", {
"warnings",
@ -6449,10 +6454,6 @@ def test_overload_registry_repeated(self):
self.assertEqual(list(get_overloads(impl)), overloads)
from test.typinganndata import (
ann_module, ann_module2, ann_module3, ann_module5, ann_module6,
)
T_a = TypeVar('T_a')
class AwaitableWrapper(typing.Awaitable[T_a]):
@ -6665,8 +6666,8 @@ def test_respect_no_type_check(self):
class NoTpCheck:
class Inn:
def __init__(self, x: 'not a type'): ...
self.assertTrue(NoTpCheck.__no_type_check__)
self.assertTrue(NoTpCheck.Inn.__init__.__no_type_check__)
self.assertIs(NoTpCheck.__no_type_check__, True)
self.assertIs(NoTpCheck.Inn.__init__.__no_type_check__, True)
self.assertEqual(gth(ann_module2.NTC.meth), {})
class ABase(Generic[T]):
def meth(x: int): ...
@ -10196,6 +10197,7 @@ def test_var_substitution(self):
self.assertEqual(C[Concatenate[str, P2]], Concatenate[int, str, P2])
self.assertEqual(C[...], Concatenate[int, ...])
class TypeGuardTests(BaseTestCase):
def test_basics(self):
TypeGuard[int] # OK