From b8a2b553e6a20a35d38ee57f312b69bb212a3de8 Mon Sep 17 00:00:00 2001 From: osmantamer Date: Sat, 12 Nov 2022 19:31:37 +0300 Subject: [PATCH 1/3] Unexport shouldn't include TypeVar's #9 --- src/unexport/rule.py | 15 +++++++++++ tests/test_refactor.py | 60 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/src/unexport/rule.py b/src/unexport/rule.py index fdfb61f..f760211 100644 --- a/src/unexport/rule.py +++ b/src/unexport/rule.py @@ -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): + 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 diff --git a/tests/test_refactor.py b/tests/test_refactor.py index 69d220b..f3ebf3a 100644 --- a/tests/test_refactor.py +++ b/tests/test_refactor.py @@ -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") + """, + ) ] From 1da58b9b145789743cc2233773267479caa9e0ec Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 12 Nov 2022 16:33:10 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/unexport/rule.py | 8 +++++--- tests/test_refactor.py | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/unexport/rule.py b/src/unexport/rule.py index f760211..6fe69ef 100644 --- a/src/unexport/rule.py +++ b/src/unexport/rule.py @@ -135,12 +135,14 @@ def _rule_node_is_all_item(node) -> bool: 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): + 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): + elif "typing" == getattr(node.parent.value.func.value, "id", None) and "TypeVar" == getattr( + node.parent.value.func, "attr", None + ): 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: + if "typing" in modules: return False return True diff --git a/tests/test_refactor.py b/tests/test_refactor.py index f3ebf3a..31c3032 100644 --- a/tests/test_refactor.py +++ b/tests/test_refactor.py @@ -237,7 +237,7 @@ def __init__(self, name):... T = TypeVar("T") """, - ) + ), ] From 6a8cae3e8327b1c27a54afded48df9239847f355 Mon Sep 17 00:00:00 2001 From: osmantamer Date: Sat, 12 Nov 2022 19:39:37 +0300 Subject: [PATCH 3/3] Unexport shouldn't include TypeVar's #9 --- src/unexport/rule.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/unexport/rule.py b/src/unexport/rule.py index 6fe69ef..37d2ece 100644 --- a/src/unexport/rule.py +++ b/src/unexport/rule.py @@ -137,9 +137,7 @@ def _rule_node_is_typevar(node) -> bool: 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 - ): + 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])