Skip to content

Commit

Permalink
Ignore fewer flake8 rules when linting tests (#413)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexWaygood authored May 27, 2024
1 parent f90a8dc commit e792bce
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 94 deletions.
8 changes: 3 additions & 5 deletions .flake8
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
[flake8]

max-line-length = 90
ignore =
# irrelevant plugins
Expand All @@ -11,8 +10,7 @@ ignore =
W503,
# consistency with mypy
W504
exclude =
# tests have more relaxed formatting rules
# and its own specific config in .flake8-tests
src/test_typing_extensions.py,
per-file-ignores =
# stylistic rules we don't care about in tests
src/test_typing_extensions.py:E302,E306,E501,E701,E704,
noqa_require_code = true
31 changes: 0 additions & 31 deletions .flake8-tests

This file was deleted.

3 changes: 0 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,6 @@ jobs:
- name: Lint implementation
run: flake8 --color always

- name: Lint tests
run: flake8 --config=.flake8-tests src/test_typing_extensions.py --color always

create-issue-on-failure:
name: Create an issue if daily tests failed
runs-on: ubuntu-latest
Expand Down
94 changes: 39 additions & 55 deletions src/test_typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from typing_extensions import Protocol, runtime, runtime_checkable, Annotated, final, is_typeddict
from typing_extensions import TypeVarTuple, Unpack, dataclass_transform, reveal_type, Never, assert_never, LiteralString
from typing_extensions import assert_type, get_type_hints, get_origin, get_args, get_original_bases
from typing_extensions import clear_overloads, get_overloads, overload
from typing_extensions import clear_overloads, get_overloads, overload, Iterator
from typing_extensions import NamedTuple, TypeIs, no_type_check, Dict
from typing_extensions import override, deprecated, Buffer, TypeAliasType, TypeVar, get_protocol_members, is_protocol
from typing_extensions import Doc, NoDefault, List, Union, AnyStr, Iterable, Generic, Optional, Set, Tuple, Callable
Expand Down Expand Up @@ -220,7 +220,7 @@ def test_cannot_subclass(self):
class A(self.bottom_type):
pass
with self.assertRaises(TypeError):
class A(type(self.bottom_type)):
class B(type(self.bottom_type)):
pass

def test_cannot_instantiate(self):
Expand Down Expand Up @@ -322,7 +322,6 @@ def static_method_good_order():
def static_method_bad_order():
return 42


self.assertIsSubclass(Derived, Base)
instance = Derived()
self.assertEqual(instance.normal_method(), 42)
Expand Down Expand Up @@ -685,7 +684,7 @@ def test_cannot_subclass(self):
class C(type(ClassVar)):
pass
with self.assertRaises(TypeError):
class C(type(ClassVar[int])):
class D(type(ClassVar[int])):
pass

def test_cannot_init(self):
Expand Down Expand Up @@ -726,7 +725,7 @@ def test_cannot_subclass(self):
class C(type(Final)):
pass
with self.assertRaises(TypeError):
class C(type(Final[int])):
class D(type(Final[int])):
pass

def test_cannot_init(self):
Expand Down Expand Up @@ -771,7 +770,7 @@ def test_cannot_subclass(self):
class C(type(Required)):
pass
with self.assertRaises(TypeError):
class C(type(Required[int])):
class D(type(Required[int])):
pass

def test_cannot_init(self):
Expand Down Expand Up @@ -816,7 +815,7 @@ def test_cannot_subclass(self):
class C(type(NotRequired)):
pass
with self.assertRaises(TypeError):
class C(type(NotRequired[int])):
class D(type(NotRequired[int])):
pass

def test_cannot_init(self):
Expand All @@ -836,15 +835,15 @@ def test_no_isinstance(self):

class IntVarTests(BaseTestCase):
def test_valid(self):
T_ints = IntVar("T_ints")
IntVar("T_ints")

def test_invalid(self):
with self.assertRaises(TypeError):
T_ints = IntVar("T_ints", int)
IntVar("T_ints", int)
with self.assertRaises(TypeError):
T_ints = IntVar("T_ints", bound=int)
IntVar("T_ints", bound=int)
with self.assertRaises(TypeError):
T_ints = IntVar("T_ints", covariant=True)
IntVar("T_ints", covariant=True)


class LiteralTests(BaseTestCase):
Expand Down Expand Up @@ -1191,7 +1190,6 @@ async def __aexit__(self, etype, eval, tb):
return None



class A:
y: float
class B(A):
Expand Down Expand Up @@ -1336,7 +1334,7 @@ def test_respect_no_type_check(self):
@no_type_check
class NoTpCheck:
class Inn:
def __init__(self, x: 'not a type'): ...
def __init__(self, x: 'not a type'): ... # noqa: F722 # (yes, there's a syntax error in this annotation, that's the point)
self.assertTrue(NoTpCheck.__no_type_check__)
self.assertTrue(NoTpCheck.Inn.__init__.__no_type_check__)
self.assertEqual(gth(self.ann_module2.NTC.meth), {})
Expand Down Expand Up @@ -2034,10 +2032,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
class E(C, BP): pass
Expand Down Expand Up @@ -2350,7 +2348,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 @@ -3196,11 +3194,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 P2(Protocol[int]): pass
with self.assertRaises(TypeError):
class P(Protocol[T], Protocol[S]): pass
class P3(Protocol[T], Protocol[S]): pass
with self.assertRaises(TypeError):
class P(typing.Mapping[T, S], Protocol[T]): pass
class P4(typing.Mapping[T, S], Protocol[T]): pass

def test_generic_protocols_repr(self):
T = TypeVar('T')
Expand Down Expand Up @@ -3735,9 +3733,8 @@ def test_basics_functional_syntax(self):

@skipIf(sys.version_info < (3, 13), "Change in behavior in 3.13")
def test_keywords_syntax_raises_on_3_13(self):
with self.assertRaises(TypeError):
with self.assertWarns(DeprecationWarning):
Emp = TypedDict('Emp', name=str, id=int)
with self.assertRaises(TypeError), self.assertWarns(DeprecationWarning):
TypedDict('Emp', name=str, id=int)

@skipIf(sys.version_info >= (3, 13), "3.13 removes support for kwargs")
def test_basics_keywords_syntax(self):
Expand Down Expand Up @@ -4178,7 +4175,6 @@ class C(B[int]):
with self.assertRaises(TypeError):
C[str]


class Point3D(Point2DGeneric[T], Generic[T, KT]):
c: KT

Expand Down Expand Up @@ -4826,7 +4822,7 @@ def test_canonical_usage_with_variable_annotation(self):
exec('Alias: TypeAlias = Employee', globals(), ns)

def test_canonical_usage_with_type_comment(self):
Alias: TypeAlias = Employee
Alias: TypeAlias = Employee # noqa: F841

def test_cannot_instantiate(self):
with self.assertRaises(TypeError):
Expand All @@ -4849,7 +4845,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 @@ -5078,11 +5074,15 @@ def test_valid_uses(self):

C1 = Callable[Concatenate[int, P], int]
C2 = Callable[Concatenate[int, T, P], T]
self.assertEqual(C1.__origin__, C2.__origin__)
self.assertNotEqual(C1, C2)

# Test collections.abc.Callable too.
if sys.version_info[:2] >= (3, 9):
C3 = collections.abc.Callable[Concatenate[int, P], int]
C4 = collections.abc.Callable[Concatenate[int, T, P], T]
self.assertEqual(C3.__origin__, C4.__origin__)
self.assertNotEqual(C3, C4)

def test_invalid_uses(self):
P = ParamSpec('P')
Expand Down Expand Up @@ -5152,7 +5152,7 @@ def test_cannot_subclass(self):
class C(type(TypeGuard)):
pass
with self.assertRaises(TypeError):
class C(type(TypeGuard[int])):
class D(type(TypeGuard[int])):
pass

def test_cannot_init(self):
Expand Down Expand Up @@ -5196,7 +5196,7 @@ def test_cannot_subclass(self):
class C(type(TypeIs)):
pass
with self.assertRaises(TypeError):
class C(type(TypeIs[int])):
class D(type(TypeIs[int])):
pass

def test_cannot_init(self):
Expand Down Expand Up @@ -5242,7 +5242,7 @@ def test_cannot_subclass(self):
class C(type(LiteralString)):
pass
with self.assertRaises(TypeError):
class C(LiteralString):
class D(LiteralString):
pass

def test_cannot_init(self):
Expand Down Expand Up @@ -5785,17 +5785,6 @@ def double(self):
return 2 * self.x


class XRepr(NamedTuple):
x: int
y: int = 1

def __str__(self):
return f'{self.x} -> {self.y}'

def __add__(self, other):
return 0


class NamedTupleTests(BaseTestCase):
class NestedEmployee(NamedTuple):
name: str
Expand Down Expand Up @@ -5887,11 +5876,11 @@ class X(NamedTuple, A):
TypeError,
'can only inherit from a NamedTuple type and Generic'
):
class X(NamedTuple, tuple):
class Y(NamedTuple, tuple):
x: int

with self.assertRaisesRegex(TypeError, 'duplicate base class'):
class X(NamedTuple, NamedTuple):
class Z(NamedTuple, NamedTuple):
x: int

class A(NamedTuple):
Expand All @@ -5900,7 +5889,7 @@ class A(NamedTuple):
TypeError,
'can only inherit from a NamedTuple type and Generic'
):
class X(NamedTuple, A):
class XX(NamedTuple, A):
y: str

def test_generic(self):
Expand Down Expand Up @@ -6156,11 +6145,6 @@ class NamedTupleClass(NamedTuple):
attr = annoying
namedtuple_exception = cm.exception

expected_note = (
"Error calling __set_name__ on 'Annoying' instance "
"'attr' in 'NamedTupleClass'"
)

self.assertIs(type(namedtuple_exception), RuntimeError)
self.assertIs(type(namedtuple_exception), type(normal_exception))
self.assertEqual(len(namedtuple_exception.args), len(normal_exception.args))
Expand Down Expand Up @@ -6316,8 +6300,8 @@ def test_or(self):
X = TypeVar('X')
# use a string because str doesn't implement
# __or__/__ror__ itself
self.assertEqual(X | "x", Union[X, "x"])
self.assertEqual("x" | X, Union["x", X])
self.assertEqual(X | "x", Union[X, "x"]) # noqa: F821
self.assertEqual("x" | X, Union["x", X]) # noqa: F821
# make sure the order is correct
self.assertEqual(get_args(X | "x"), (X, typing.ForwardRef("x")))
self.assertEqual(get_args("x" | X), (typing.ForwardRef("x"), X))
Expand Down Expand Up @@ -6345,7 +6329,7 @@ def test_cannot_subclass(self):
class V(TypeVar): pass
T = TypeVar("T")
with self.assertRaises(TypeError):
class V(T): pass
class W(T): pass

def test_cannot_instantiate_vars(self):
with self.assertRaises(TypeError):
Expand Down Expand Up @@ -6392,7 +6376,7 @@ def test_typevar(self):
self.assertIsInstance(typing_T, typing_extensions.TypeVar)

class A(Generic[T]): ...
Alias = Optional[T]
self.assertEqual(Optional[T].__args__, (T, type(None)))

def test_typevar_none(self):
U = typing_extensions.TypeVar('U')
Expand All @@ -6414,7 +6398,7 @@ def test_paramspec(self):
self.assertIsInstance(typing_P, ParamSpec)

class A(Generic[P]): ...
Alias = typing.Callable[P, None]
self.assertEqual(typing.Callable[P, None].__args__, (P, type(None)))

P_default = ParamSpec('P_default', default=...)
self.assertIs(P_default.__default__, ...)
Expand All @@ -6440,7 +6424,7 @@ def test_typevartuple(self):
self.assertIsInstance(typing_Ts, TypeVarTuple)

class A(Generic[Unpack[Ts]]): ...
Alias = Optional[Unpack[Ts]]
self.assertEqual(Optional[Unpack[Ts]].__args__, (Unpack[Ts], type(None)))

@skipIf(
sys.version_info < (3, 11, 1),
Expand Down Expand Up @@ -6494,7 +6478,7 @@ def test_no_default_after_non_default(self):
T = TypeVar('T')

with self.assertRaises(TypeError):
Test = Generic[DefaultStrT, T]
Generic[DefaultStrT, T]

def test_need_more_params(self):
DefaultStrT = typing_extensions.TypeVar('DefaultStrT', default=str)
Expand All @@ -6508,7 +6492,7 @@ class A(Generic[T, U, DefaultStrT]): ...
with self.assertRaises(
TypeError, msg="Too few arguments for .+; actual 1, expected at least 2"
):
Test = A[int]
A[int]

def test_pickle(self):
global U, U_co, U_contra, U_default # pickle wants to reference the class by name
Expand Down

0 comments on commit e792bce

Please sign in to comment.