Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/unexport/rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,18 @@ def _rule_node_is_all_item(node) -> bool:
and isinstance(node.value.func.value, ast.Name)
and node.value.func.value.id == "__all__"
)


@Rule.register((ast.Name,)) # type: ignore
def _rule_node_is_typevar(node) -> bool:
modules: list[str] = []
if isinstance(node.parent.value, ast.Call):
if "TypeVar" == getattr(node.parent.value.func, "id", None):
modules.extend(body.module for body in node.parent.parent.body if isinstance(body, ast.ImportFrom))
elif "typing" == getattr(node.parent.value.func.value, "id", None) and "TypeVar" == getattr(node.parent.value.func, "attr", None): # type: ignore # noqa: E501
for body in node.parent.parent.body:
if isinstance(body, ast.Import):
modules.extend([import_alias.name for import_alias in body.names])
if "typing" in modules:
return False
return True
60 changes: 60 additions & 0 deletions tests/test_refactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,66 @@ def x():...
XXX = 1 # unexport: not-public
""",
),
(
"""\
from typing import TypeVar

T = TypeVar("T")
""",
"""\
from typing import TypeVar

T = TypeVar("T")
""",
),
(
"""\
from typing import TypeVar

T = TypeVar("T")

def func():
pass
""",
"""\
from typing import TypeVar

__all__ = ["func"]

T = TypeVar("T")

def func():
pass
""",
),
(
"""\
import typing

T = typing.TypeVar("T")
""",
"""\
import typing

T = typing.TypeVar("T")
""",
),
(
"""\
class TypeVar:
def __init__(self, name):...

T = TypeVar("T")
""",
"""\
__all__ = ["T", "TypeVar"]

class TypeVar:
def __init__(self, name):...

T = TypeVar("T")
""",
),
]


Expand Down