Skip to content

Commit

Permalink
Enable ruff on Lib/test/test_typing.py (python#110179)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexWaygood authored Oct 2, 2023
1 parent 1dd9dee commit 014aacd
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 54 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.0.288
rev: v0.0.292
hooks:
- id: ruff
name: Run Ruff on Lib/test/
Expand Down
1 change: 0 additions & 1 deletion Lib/test/.ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ extend-exclude = [
"test_keywordonlyarg.py",
"test_pkg.py",
"test_subclassinit.py",
"test_typing.py",
"test_unittest/testmock/testpatch.py",
"test_yield_from.py",
"time_hashlib.py",
Expand Down
104 changes: 52 additions & 52 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def test_cannot_subclass(self):
class A(self.bottom_type):
pass
with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE):
class A(type(self.bottom_type)):
class B(type(self.bottom_type)):
pass

def test_cannot_instantiate(self):
Expand Down Expand Up @@ -282,7 +282,7 @@ class C(type(Self)):
pass
with self.assertRaisesRegex(TypeError,
r'Cannot subclass typing\.Self'):
class C(Self):
class D(Self):
pass

def test_cannot_init(self):
Expand Down Expand Up @@ -339,7 +339,7 @@ class C(type(LiteralString)):
pass
with self.assertRaisesRegex(TypeError,
r'Cannot subclass typing\.LiteralString'):
class C(LiteralString):
class D(LiteralString):
pass

def test_cannot_init(self):
Expand Down Expand Up @@ -483,7 +483,7 @@ class V(TypeVar): pass
T = TypeVar("T")
with self.assertRaisesRegex(TypeError,
CANNOT_SUBCLASS_INSTANCE % 'TypeVar'):
class V(T): pass
class W(T): pass

def test_cannot_instantiate_vars(self):
with self.assertRaises(TypeError):
Expand Down Expand Up @@ -1244,20 +1244,20 @@ class C(TypeVarTuple): pass
Ts = TypeVarTuple('Ts')
with self.assertRaisesRegex(TypeError,
CANNOT_SUBCLASS_INSTANCE % 'TypeVarTuple'):
class C(Ts): pass
class D(Ts): pass
with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE):
class C(type(Unpack)): pass
class E(type(Unpack)): pass
with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE):
class C(type(*Ts)): pass
class F(type(*Ts)): pass
with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE):
class C(type(Unpack[Ts])): pass
class G(type(Unpack[Ts])): pass
with self.assertRaisesRegex(TypeError,
r'Cannot subclass typing\.Unpack'):
class C(Unpack): pass
class H(Unpack): pass
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\]'):
class C(Unpack[Ts]): pass
class J(Unpack[Ts]): pass

def test_variadic_class_args_are_correct(self):
T = TypeVar('T')
Expand Down Expand Up @@ -1431,12 +1431,12 @@ def test_variadic_class_with_duplicate_typevartuples_fails(self):
with self.assertRaises(TypeError):
class C(Generic[*Ts1, *Ts1]): pass
with self.assertRaises(TypeError):
class C(Generic[Unpack[Ts1], Unpack[Ts1]]): pass
class D(Generic[Unpack[Ts1], Unpack[Ts1]]): pass

with self.assertRaises(TypeError):
class C(Generic[*Ts1, *Ts2, *Ts1]): pass
class E(Generic[*Ts1, *Ts2, *Ts1]): pass
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):
Ts = TypeVarTuple('Ts')
Expand Down Expand Up @@ -1804,11 +1804,11 @@ def test_cannot_subclass(self):
class C(Union):
pass
with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE):
class C(type(Union)):
class D(type(Union)):
pass
with self.assertRaisesRegex(TypeError,
r'Cannot subclass typing\.Union\[int, str\]'):
class C(Union[int, str]):
class E(Union[int, str]):
pass

def test_cannot_instantiate(self):
Expand Down Expand Up @@ -2557,10 +2557,10 @@ class BP(Protocol): pass
class P(C, Protocol):
pass
with self.assertRaises(TypeError):
class P(Protocol, C):
class Q(Protocol, C):
pass
with self.assertRaises(TypeError):
class P(BP, C, Protocol):
class R(BP, C, Protocol):
pass

class D(BP, C): pass
Expand Down Expand Up @@ -2836,7 +2836,7 @@ class NotAProtocolButAnImplicitSubclass3:
meth: Callable[[], None]
meth2: Callable[[int, str], bool]
def meth(self): pass
def meth(self, x, y): return True
def meth2(self, x, y): return True

self.assertNotIsSubclass(AnnotatedButNotAProtocol, CallableMembersProto)
self.assertIsSubclass(NotAProtocolButAnImplicitSubclass, CallableMembersProto)
Expand Down Expand Up @@ -3658,11 +3658,11 @@ def test_protocols_bad_subscripts(self):
with self.assertRaises(TypeError):
class P(Protocol[T, T]): pass
with self.assertRaises(TypeError):
class P(Protocol[int]): pass
class Q(Protocol[int]): pass
with self.assertRaises(TypeError):
class P(Protocol[T], Protocol[S]): pass
class R(Protocol[T], Protocol[S]): pass
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):
T = TypeVar('T')
Expand Down Expand Up @@ -4094,12 +4094,12 @@ class NewGeneric(Generic): ...
with self.assertRaises(TypeError):
class MyGeneric(Generic[T], Generic[S]): ...
with self.assertRaises(TypeError):
class MyGeneric(List[T], Generic[S]): ...
class MyGeneric2(List[T], Generic[S]): ...
with self.assertRaises(TypeError):
Generic[()]
class C(Generic[T]): pass
class D(Generic[T]): pass
with self.assertRaises(TypeError):
C[()]
D[()]

def test_generic_subclass_checks(self):
for typ in [list[int], List[int],
Expand Down Expand Up @@ -4836,7 +4836,7 @@ class Test(Generic[T], Final):
class Subclass(Test):
pass
with self.assertRaises(FinalException):
class Subclass(Test[int]):
class Subclass2(Test[int]):
pass

def test_nested(self):
Expand Down Expand Up @@ -5074,15 +5074,15 @@ def test_cannot_subclass(self):
class C(type(ClassVar)):
pass
with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE):
class C(type(ClassVar[int])):
class D(type(ClassVar[int])):
pass
with self.assertRaisesRegex(TypeError,
r'Cannot subclass typing\.ClassVar'):
class C(ClassVar):
class E(ClassVar):
pass
with self.assertRaisesRegex(TypeError,
r'Cannot subclass typing\.ClassVar\[int\]'):
class C(ClassVar[int]):
class F(ClassVar[int]):
pass

def test_cannot_init(self):
Expand Down Expand Up @@ -5124,15 +5124,15 @@ def test_cannot_subclass(self):
class C(type(Final)):
pass
with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE):
class C(type(Final[int])):
class D(type(Final[int])):
pass
with self.assertRaisesRegex(TypeError,
r'Cannot subclass typing\.Final'):
class C(Final):
class E(Final):
pass
with self.assertRaisesRegex(TypeError,
r'Cannot subclass typing\.Final\[int\]'):
class C(Final[int]):
class F(Final[int]):
pass

def test_cannot_init(self):
Expand Down Expand Up @@ -7265,15 +7265,15 @@ class A:
class X(NamedTuple, A):
x: int
with self.assertRaises(TypeError):
class X(NamedTuple, tuple):
class Y(NamedTuple, tuple):
x: int
with self.assertRaises(TypeError):
class X(NamedTuple, NamedTuple):
class Z(NamedTuple, NamedTuple):
x: int
class A(NamedTuple):
class B(NamedTuple):
x: int
with self.assertRaises(TypeError):
class X(NamedTuple, A):
class C(NamedTuple, B):
y: str

def test_generic(self):
Expand Down Expand Up @@ -8037,15 +8037,15 @@ def test_cannot_subclass(self):
class C(type(Required)):
pass
with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE):
class C(type(Required[int])):
class D(type(Required[int])):
pass
with self.assertRaisesRegex(TypeError,
r'Cannot subclass typing\.Required'):
class C(Required):
class E(Required):
pass
with self.assertRaisesRegex(TypeError,
r'Cannot subclass typing\.Required\[int\]'):
class C(Required[int]):
class F(Required[int]):
pass

def test_cannot_init(self):
Expand Down Expand Up @@ -8085,15 +8085,15 @@ def test_cannot_subclass(self):
class C(type(NotRequired)):
pass
with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE):
class C(type(NotRequired[int])):
class D(type(NotRequired[int])):
pass
with self.assertRaisesRegex(TypeError,
r'Cannot subclass typing\.NotRequired'):
class C(NotRequired):
class E(NotRequired):
pass
with self.assertRaisesRegex(TypeError,
r'Cannot subclass typing\.NotRequired\[int\]'):
class C(NotRequired[int]):
class F(NotRequired[int]):
pass

def test_cannot_init(self):
Expand Down Expand Up @@ -8192,7 +8192,7 @@ class A(typing.Match):
TypeError,
r"type 're\.Pattern' is not an acceptable base type",
):
class A(typing.Pattern):
class B(typing.Pattern):
pass


Expand Down Expand Up @@ -8539,7 +8539,7 @@ class C(TypeAlias):
pass

with self.assertRaises(TypeError):
class C(type(TypeAlias)):
class D(type(TypeAlias)):
pass

def test_repr(self):
Expand Down Expand Up @@ -8929,19 +8929,19 @@ def test_cannot_subclass(self):
with self.assertRaisesRegex(TypeError, NOT_A_BASE_TYPE % 'ParamSpec'):
class C(ParamSpec): pass
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'):
class C(ParamSpecKwargs): pass
class E(ParamSpecKwargs): pass
P = ParamSpec('P')
with self.assertRaisesRegex(TypeError,
CANNOT_SUBCLASS_INSTANCE % 'ParamSpec'):
class C(P): pass
class F(P): pass
with self.assertRaisesRegex(TypeError,
CANNOT_SUBCLASS_INSTANCE % 'ParamSpecArgs'):
class C(P.args): pass
class G(P.args): pass
with self.assertRaisesRegex(TypeError,
CANNOT_SUBCLASS_INSTANCE % 'ParamSpecKwargs'):
class C(P.kwargs): pass
class H(P.kwargs): pass


class ConcatenateTests(BaseTestCase):
Expand Down Expand Up @@ -9022,15 +9022,15 @@ def test_cannot_subclass(self):
class C(type(TypeGuard)):
pass
with self.assertRaisesRegex(TypeError, CANNOT_SUBCLASS_TYPE):
class C(type(TypeGuard[int])):
class D(type(TypeGuard[int])):
pass
with self.assertRaisesRegex(TypeError,
r'Cannot subclass typing\.TypeGuard'):
class C(TypeGuard):
class E(TypeGuard):
pass
with self.assertRaisesRegex(TypeError,
r'Cannot subclass typing\.TypeGuard\[int\]'):
class C(TypeGuard[int]):
class F(TypeGuard[int]):
pass

def test_cannot_init(self):
Expand Down

0 comments on commit 014aacd

Please sign in to comment.