Skip to content

Commit

Permalink
Moves one of the regression tests from TC004 to TC009
Browse files Browse the repository at this point in the history
It would never have emitted a TC004, even if we made a mistake it would
always have emitted a TC009. I've added an inverse test to convince
ourselves that this is true.
  • Loading branch information
Daverball authored and sondrelg committed Nov 25, 2023
1 parent 50ba61f commit d54b13c
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 61 deletions.
16 changes: 0 additions & 16 deletions tests/test_tc004.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,22 +127,6 @@ def test_function(a, /, b, *, c, **d):
"""),
set(),
),
# regression test for #131
# a common pattern for inheriting from generics that aren't runtime subscriptable
(
textwrap.dedent("""
from wtforms import Field
if TYPE_CHECKING:
BaseField = Field[int]
else:
BaseField = Field
class IntegerField(BaseField):
pass
"""),
set(),
),
# Regression test for #131
# handle scopes correctly
(
Expand Down
120 changes: 75 additions & 45 deletions tests/test_tc009.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,92 +18,92 @@
# Used in file
(
textwrap.dedent("""
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING
if TYPE_CHECKING:
datetime = Any
if TYPE_CHECKING:
datetime = Any
x = datetime
"""),
x = datetime
"""),
{'5:4 ' + TC009.format(name='datetime')},
),
# Used in function
(
textwrap.dedent("""
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING
if TYPE_CHECKING:
class date: ...
if TYPE_CHECKING:
class date: ...
def example():
return date()
"""),
def example():
return date()
"""),
{'5:4 ' + TC009.format(name='date')},
),
# Used, but only used inside the type checking block
(
textwrap.dedent("""
if TYPE_CHECKING:
class date: ...
if TYPE_CHECKING:
class date: ...
CustomType = date
"""),
CustomType = date
"""),
set(),
),
# Used for typing only
(
textwrap.dedent("""
if TYPE_CHECKING:
class date: ...
if TYPE_CHECKING:
class date: ...
def example(*args: date, **kwargs: date):
return
def example(*args: date, **kwargs: date):
return
my_type: Type[date] | date
"""),
my_type: Type[date] | date
"""),
set(),
),
(
textwrap.dedent("""
from __future__ import annotations
from __future__ import annotations
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING
if TYPE_CHECKING:
class AsyncIterator: ...
if TYPE_CHECKING:
class AsyncIterator: ...
class Example:
class Example:
async def example(self) -> AsyncIterator[list[str]]:
yield 0
"""),
async def example(self) -> AsyncIterator[list[str]]:
yield 0
"""),
set(),
),
(
textwrap.dedent("""
from typing import TYPE_CHECKING
from weakref import WeakKeyDictionary
from typing import TYPE_CHECKING
from weakref import WeakKeyDictionary
if TYPE_CHECKING:
Any = str
if TYPE_CHECKING:
Any = str
d = WeakKeyDictionary["Any", "Any"]()
"""),
d = WeakKeyDictionary["Any", "Any"]()
"""),
set(),
),
(
textwrap.dedent("""
if TYPE_CHECKING:
a = int
b: TypeAlias = str
class c(Protocol): ...
class d(TypedDict): ...
def test_function(a, /, b, *, c, **d):
print(a, b, c, d)
"""),
if TYPE_CHECKING:
a = int
b: TypeAlias = str
class c(Protocol): ...
class d(TypedDict): ...
def test_function(a, /, b, *, c, **d):
print(a, b, c, d)
"""),
set(),
),
# Regression test for #131
Expand Down Expand Up @@ -131,9 +131,39 @@ class Foo(Protocol):
bar: Foo = Foo()
"""),
"""),
set(),
),
# regression test for #131
# a common pattern for inheriting from generics that aren't runtime subscriptable
(
textwrap.dedent("""
from wtforms import Field
if TYPE_CHECKING:
BaseField = Field[int]
else:
BaseField = Field
class IntegerField(BaseField):
pass
"""),
set(),
),
# inverse regression test for #131
# here we forgot the else so it will complain about BaseField
(
textwrap.dedent("""
from wtforms import Field
if TYPE_CHECKING:
BaseField = Field[int]
class IntegerField(BaseField):
pass
"""),
{'5:4 ' + TC009.format(name='BaseField')},
),
]

if sys.version_info >= (3, 12):
Expand Down

0 comments on commit d54b13c

Please sign in to comment.