Enable ruff on Lib/test/test_typing.py (#110179)

This commit is contained in:
Alex Waygood 2023-10-02 21:13:48 +01:00 committed by GitHub
parent 1dd9dee45d
commit 014aacda62
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 54 deletions

View File

@ -1,6 +1,6 @@
repos: repos:
- repo: https://github.com/astral-sh/ruff-pre-commit - repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.0.288 rev: v0.0.292
hooks: hooks:
- id: ruff - id: ruff
name: Run Ruff on Lib/test/ name: Run Ruff on Lib/test/

View File

@ -26,7 +26,6 @@ extend-exclude = [
"test_keywordonlyarg.py", "test_keywordonlyarg.py",
"test_pkg.py", "test_pkg.py",
"test_subclassinit.py", "test_subclassinit.py",
"test_typing.py",
"test_unittest/testmock/testpatch.py", "test_unittest/testmock/testpatch.py",
"test_yield_from.py", "test_yield_from.py",
"time_hashlib.py", "time_hashlib.py",

View File

@ -185,7 +185,7 @@ class BottomTypeTestsMixin:
class A(self.bottom_type): class A(self.bottom_type):
pass pass
with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE): with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE):
class A(type(self.bottom_type)): class B(type(self.bottom_type)):
pass pass
def test_cannot_instantiate(self): def test_cannot_instantiate(self):
@ -282,7 +282,7 @@ class SelfTests(BaseTestCase):
pass pass
with self.assertRaisesRegex(TypeError, with self.assertRaisesRegex(TypeError,
r'Cannot subclass typing\.Self'): r'Cannot subclass typing\.Self'):
class C(Self): class D(Self):
pass pass
def test_cannot_init(self): def test_cannot_init(self):
@ -339,7 +339,7 @@ class LiteralStringTests(BaseTestCase):
pass pass
with self.assertRaisesRegex(TypeError, with self.assertRaisesRegex(TypeError,
r'Cannot subclass typing\.LiteralString'): r'Cannot subclass typing\.LiteralString'):
class C(LiteralString): class D(LiteralString):
pass pass
def test_cannot_init(self): def test_cannot_init(self):
@ -483,7 +483,7 @@ class TypeVarTests(BaseTestCase):
T = TypeVar("T") T = TypeVar("T")
with self.assertRaisesRegex(TypeError, with self.assertRaisesRegex(TypeError,
CANNOT_SUBCLASS_INSTANCE % 'TypeVar'): CANNOT_SUBCLASS_INSTANCE % 'TypeVar'):
class V(T): pass class W(T): pass
def test_cannot_instantiate_vars(self): def test_cannot_instantiate_vars(self):
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
@ -1244,20 +1244,20 @@ class TypeVarTupleTests(BaseTestCase):
Ts = TypeVarTuple('Ts') Ts = TypeVarTuple('Ts')
with self.assertRaisesRegex(TypeError, with self.assertRaisesRegex(TypeError,
CANNOT_SUBCLASS_INSTANCE % 'TypeVarTuple'): CANNOT_SUBCLASS_INSTANCE % 'TypeVarTuple'):
class C(Ts): pass class D(Ts): pass
with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE): with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE):
class C(type(Unpack)): pass class E(type(Unpack)): pass
with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE): with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE):
class C(type(*Ts)): pass class F(type(*Ts)): pass
with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE): with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE):
class C(type(Unpack[Ts])): pass class G(type(Unpack[Ts])): pass
with self.assertRaisesRegex(TypeError, with self.assertRaisesRegex(TypeError,
r'Cannot subclass typing\.Unpack'): r'Cannot subclass typing\.Unpack'):
class C(Unpack): pass class H(Unpack): pass
with self.assertRaisesRegex(TypeError, r'Cannot subclass typing.Unpack\[Ts\]'): with self.assertRaisesRegex(TypeError, r'Cannot subclass typing.Unpack\[Ts\]'):
class C(*Ts): pass class I(*Ts): pass
with self.assertRaisesRegex(TypeError, r'Cannot subclass typing.Unpack\[Ts\]'): with self.assertRaisesRegex(TypeError, r'Cannot subclass typing.Unpack\[Ts\]'):
class C(Unpack[Ts]): pass class J(Unpack[Ts]): pass
def test_variadic_class_args_are_correct(self): def test_variadic_class_args_are_correct(self):
T = TypeVar('T') T = TypeVar('T')
@ -1431,12 +1431,12 @@ class TypeVarTupleTests(BaseTestCase):
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
class C(Generic[*Ts1, *Ts1]): pass class C(Generic[*Ts1, *Ts1]): pass
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
class C(Generic[Unpack[Ts1], Unpack[Ts1]]): pass class D(Generic[Unpack[Ts1], Unpack[Ts1]]): pass
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
class C(Generic[*Ts1, *Ts2, *Ts1]): pass class E(Generic[*Ts1, *Ts2, *Ts1]): pass
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
class C(Generic[Unpack[Ts1], Unpack[Ts2], Unpack[Ts1]]): pass class F(Generic[Unpack[Ts1], Unpack[Ts2], Unpack[Ts1]]): pass
def test_type_concatenation_in_variadic_class_argument_list_succeeds(self): def test_type_concatenation_in_variadic_class_argument_list_succeeds(self):
Ts = TypeVarTuple('Ts') Ts = TypeVarTuple('Ts')
@ -1804,11 +1804,11 @@ class UnionTests(BaseTestCase):
class C(Union): class C(Union):
pass pass
with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE): with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE):
class C(type(Union)): class D(type(Union)):
pass pass
with self.assertRaisesRegex(TypeError, with self.assertRaisesRegex(TypeError,
r'Cannot subclass typing\.Union\[int, str\]'): r'Cannot subclass typing\.Union\[int, str\]'):
class C(Union[int, str]): class E(Union[int, str]):
pass pass
def test_cannot_instantiate(self): def test_cannot_instantiate(self):
@ -2557,10 +2557,10 @@ class ProtocolTests(BaseTestCase):
class P(C, Protocol): class P(C, Protocol):
pass pass
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
class P(Protocol, C): class Q(Protocol, C):
pass pass
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
class P(BP, C, Protocol): class R(BP, C, Protocol):
pass pass
class D(BP, C): pass class D(BP, C): pass
@ -2836,7 +2836,7 @@ class ProtocolTests(BaseTestCase):
meth: Callable[[], None] meth: Callable[[], None]
meth2: Callable[[int, str], bool] meth2: Callable[[int, str], bool]
def meth(self): pass def meth(self): pass
def meth(self, x, y): return True def meth2(self, x, y): return True
self.assertNotIsSubclass(AnnotatedButNotAProtocol, CallableMembersProto) self.assertNotIsSubclass(AnnotatedButNotAProtocol, CallableMembersProto)
self.assertIsSubclass(NotAProtocolButAnImplicitSubclass, CallableMembersProto) self.assertIsSubclass(NotAProtocolButAnImplicitSubclass, CallableMembersProto)
@ -3658,11 +3658,11 @@ class ProtocolTests(BaseTestCase):
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
class P(Protocol[T, T]): pass class P(Protocol[T, T]): pass
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
class P(Protocol[int]): pass class Q(Protocol[int]): pass
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
class P(Protocol[T], Protocol[S]): pass class R(Protocol[T], Protocol[S]): pass
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
class P(typing.Mapping[T, S], Protocol[T]): pass class S(typing.Mapping[T, S], Protocol[T]): pass
def test_generic_protocols_repr(self): def test_generic_protocols_repr(self):
T = TypeVar('T') T = TypeVar('T')
@ -4094,12 +4094,12 @@ class GenericTests(BaseTestCase):
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
class MyGeneric(Generic[T], Generic[S]): ... class MyGeneric(Generic[T], Generic[S]): ...
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
class MyGeneric(List[T], Generic[S]): ... class MyGeneric2(List[T], Generic[S]): ...
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
Generic[()] Generic[()]
class C(Generic[T]): pass class D(Generic[T]): pass
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
C[()] D[()]
def test_generic_subclass_checks(self): def test_generic_subclass_checks(self):
for typ in [list[int], List[int], for typ in [list[int], List[int],
@ -4836,7 +4836,7 @@ class GenericTests(BaseTestCase):
class Subclass(Test): class Subclass(Test):
pass pass
with self.assertRaises(FinalException): with self.assertRaises(FinalException):
class Subclass(Test[int]): class Subclass2(Test[int]):
pass pass
def test_nested(self): def test_nested(self):
@ -5074,15 +5074,15 @@ class ClassVarTests(BaseTestCase):
class C(type(ClassVar)): class C(type(ClassVar)):
pass pass
with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE): with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE):
class C(type(ClassVar[int])): class D(type(ClassVar[int])):
pass pass
with self.assertRaisesRegex(TypeError, with self.assertRaisesRegex(TypeError,
r'Cannot subclass typing\.ClassVar'): r'Cannot subclass typing\.ClassVar'):
class C(ClassVar): class E(ClassVar):
pass pass
with self.assertRaisesRegex(TypeError, with self.assertRaisesRegex(TypeError,
r'Cannot subclass typing\.ClassVar\[int\]'): r'Cannot subclass typing\.ClassVar\[int\]'):
class C(ClassVar[int]): class F(ClassVar[int]):
pass pass
def test_cannot_init(self): def test_cannot_init(self):
@ -5124,15 +5124,15 @@ class FinalTests(BaseTestCase):
class C(type(Final)): class C(type(Final)):
pass pass
with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE): with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE):
class C(type(Final[int])): class D(type(Final[int])):
pass pass
with self.assertRaisesRegex(TypeError, with self.assertRaisesRegex(TypeError,
r'Cannot subclass typing\.Final'): r'Cannot subclass typing\.Final'):
class C(Final): class E(Final):
pass pass
with self.assertRaisesRegex(TypeError, with self.assertRaisesRegex(TypeError,
r'Cannot subclass typing\.Final\[int\]'): r'Cannot subclass typing\.Final\[int\]'):
class C(Final[int]): class F(Final[int]):
pass pass
def test_cannot_init(self): def test_cannot_init(self):
@ -7265,15 +7265,15 @@ class NamedTupleTests(BaseTestCase):
class X(NamedTuple, A): class X(NamedTuple, A):
x: int x: int
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
class X(NamedTuple, tuple): class Y(NamedTuple, tuple):
x: int x: int
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
class X(NamedTuple, NamedTuple): class Z(NamedTuple, NamedTuple):
x: int x: int
class A(NamedTuple): class B(NamedTuple):
x: int x: int
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
class X(NamedTuple, A): class C(NamedTuple, B):
y: str y: str
def test_generic(self): def test_generic(self):
@ -8037,15 +8037,15 @@ class RequiredTests(BaseTestCase):
class C(type(Required)): class C(type(Required)):
pass pass
with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE): with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE):
class C(type(Required[int])): class D(type(Required[int])):
pass pass
with self.assertRaisesRegex(TypeError, with self.assertRaisesRegex(TypeError,
r'Cannot subclass typing\.Required'): r'Cannot subclass typing\.Required'):
class C(Required): class E(Required):
pass pass
with self.assertRaisesRegex(TypeError, with self.assertRaisesRegex(TypeError,
r'Cannot subclass typing\.Required\[int\]'): r'Cannot subclass typing\.Required\[int\]'):
class C(Required[int]): class F(Required[int]):
pass pass
def test_cannot_init(self): def test_cannot_init(self):
@ -8085,15 +8085,15 @@ class NotRequiredTests(BaseTestCase):
class C(type(NotRequired)): class C(type(NotRequired)):
pass pass
with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE): with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE):
class C(type(NotRequired[int])): class D(type(NotRequired[int])):
pass pass
with self.assertRaisesRegex(TypeError, with self.assertRaisesRegex(TypeError,
r'Cannot subclass typing\.NotRequired'): r'Cannot subclass typing\.NotRequired'):
class C(NotRequired): class E(NotRequired):
pass pass
with self.assertRaisesRegex(TypeError, with self.assertRaisesRegex(TypeError,
r'Cannot subclass typing\.NotRequired\[int\]'): r'Cannot subclass typing\.NotRequired\[int\]'):
class C(NotRequired[int]): class F(NotRequired[int]):
pass pass
def test_cannot_init(self): def test_cannot_init(self):
@ -8192,7 +8192,7 @@ class RETests(BaseTestCase):
TypeError, TypeError,
r"type 're\.Pattern' is not an acceptable base type", r"type 're\.Pattern' is not an acceptable base type",
): ):
class A(typing.Pattern): class B(typing.Pattern):
pass pass
@ -8539,7 +8539,7 @@ class TypeAliasTests(BaseTestCase):
pass pass
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
class C(type(TypeAlias)): class D(type(TypeAlias)):
pass pass
def test_repr(self): def test_repr(self):
@ -8929,19 +8929,19 @@ class ParamSpecTests(BaseTestCase):
with self.assertRaisesRegex(TypeError, NOT_A_BASE_TYPE % 'ParamSpec'): with self.assertRaisesRegex(TypeError, NOT_A_BASE_TYPE % 'ParamSpec'):
class C(ParamSpec): pass class C(ParamSpec): pass
with self.assertRaisesRegex(TypeError, NOT_A_BASE_TYPE % 'ParamSpecArgs'): with self.assertRaisesRegex(TypeError, NOT_A_BASE_TYPE % 'ParamSpecArgs'):
class C(ParamSpecArgs): pass class D(ParamSpecArgs): pass
with self.assertRaisesRegex(TypeError, NOT_A_BASE_TYPE % 'ParamSpecKwargs'): with self.assertRaisesRegex(TypeError, NOT_A_BASE_TYPE % 'ParamSpecKwargs'):
class C(ParamSpecKwargs): pass class E(ParamSpecKwargs): pass
P = ParamSpec('P') P = ParamSpec('P')
with self.assertRaisesRegex(TypeError, with self.assertRaisesRegex(TypeError,
CANNOT_SUBCLASS_INSTANCE % 'ParamSpec'): CANNOT_SUBCLASS_INSTANCE % 'ParamSpec'):
class C(P): pass class F(P): pass
with self.assertRaisesRegex(TypeError, with self.assertRaisesRegex(TypeError,
CANNOT_SUBCLASS_INSTANCE % 'ParamSpecArgs'): CANNOT_SUBCLASS_INSTANCE % 'ParamSpecArgs'):
class C(P.args): pass class G(P.args): pass
with self.assertRaisesRegex(TypeError, with self.assertRaisesRegex(TypeError,
CANNOT_SUBCLASS_INSTANCE % 'ParamSpecKwargs'): CANNOT_SUBCLASS_INSTANCE % 'ParamSpecKwargs'):
class C(P.kwargs): pass class H(P.kwargs): pass
class ConcatenateTests(BaseTestCase): class ConcatenateTests(BaseTestCase):
@ -9022,15 +9022,15 @@ class TypeGuardTests(BaseTestCase):
class C(type(TypeGuard)): class C(type(TypeGuard)):
pass pass
with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE): with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE):
class C(type(TypeGuard[int])): class D(type(TypeGuard[int])):
pass pass
with self.assertRaisesRegex(TypeError, with self.assertRaisesRegex(TypeError,
r'Cannot subclass typing\.TypeGuard'): r'Cannot subclass typing\.TypeGuard'):
class C(TypeGuard): class E(TypeGuard):
pass pass
with self.assertRaisesRegex(TypeError, with self.assertRaisesRegex(TypeError,
r'Cannot subclass typing\.TypeGuard\[int\]'): r'Cannot subclass typing\.TypeGuard\[int\]'):
class C(TypeGuard[int]): class F(TypeGuard[int]):
pass pass
def test_cannot_init(self): def test_cannot_init(self):