def test_type_comments(self): source = ( "from typing import Any\n" "from typing import Tuple\n" "from typing import Union\n" "def function(a, b):\n" " # type: (Any, str) -> Union[Tuple[None, None], Tuple[str, str]]\n" " pass\n") expected_names = [ Name(lineno=1, name="Any"), Name(lineno=1, name="Union"), Name(lineno=1, name="Tuple"), Name(lineno=1, name="Tuple"), ] expected_imports = [ ImportFrom(lineno=1, column=1, name="Any", star=False, suggestions=[]), ImportFrom( lineno=2, column=1, name="Tuple", star=False, suggestions=[], ), ImportFrom(lineno=3, column=1, name="Union", star=False, suggestions=[]), ] self.assertUnimportEqual( source, expected_names, expected_imports, )
def test_used_and_unused(self): self.assertUnimportEqual( """\ from lib2to3.fixer_util import * from lib2to3.pytree import * from lib2to3.pgen2 import token BlankLine, FromImport, Leaf, Newline, Node token.NAME, token.STAR """, expected_names=[ Name(lineno=4, name="BlankLine"), Name(lineno=4, name="FromImport"), Name(lineno=4, name="Leaf"), Name(lineno=4, name="Newline"), Name(lineno=4, name="Node"), Name(lineno=5, name="token.NAME"), Name(lineno=5, name="token.STAR"), ], expected_imports=[ ImportFrom( lineno=1, column=1, name="lib2to3.fixer_util", package="lib2to3.fixer_util", star=True, suggestions=[ "BlankLine", "FromImport", "Leaf", "Newline", "Node", "token", ], ), ImportFrom( lineno=2, column=1, name="lib2to3.pytree", package="lib2to3.pytree", star=True, suggestions=["Leaf", "Node"], ), ImportFrom( lineno=3, column=1, name="token", package="lib2to3.pgen2", star=False, suggestions=[], ), ], )
def visit_FunctionDef(self, node: ast.FunctionDef) -> None: """ When include_star_import becomes True, instead of suggesting star, it analyses function names and suggests one of the analyzed function's name. E.g.: from os import * print(walk) At this point from os import * becomes > from os import walk """ if ( not Relate.first_occurrence(node, ast.ClassDef) and not self.include_star_import ): self.functions.append(Name(lineno=node.lineno, name=node.name))
def test_extend(self): self.assertUnimportEqual( """\ from os import * __all__ = ["walk"] __all__.extend(["removedirs"]) """, expected_names=[ Name(lineno=2, name="__all__"), Name(lineno=3, name="__all__.extend"), Name(lineno=2, name="walk", is_all=True), Name(lineno=3, name="removedirs", is_all=True), ], expected_imports=[ ImportFrom( lineno=1, column=1, name="os", package="os", star=True, suggestions=["removedirs", "walk"], ) ], )
def test_defined_all(self): self.assertUnimportEqual( source="""\ from ast import * __all__ = [] __all__.append("x") """, expected_names=[ Name(lineno=3, name="__all__"), Name(lineno=4, name="__all__.append"), Name(lineno=4, name="x", is_all=True), ], expected_imports=[ ImportFrom( lineno=1, column=1, name="ast", package="ast", star=True, suggestions=[], ) ], )
def test_cast_import(self): source = ("import typing\n" "if typing.TYPE_CHECKING:\n" " from PyQt5.QtWebKit import QWebHistory\n" "\n" "HistoryType = typing.cast('QWebHistory', None)\n" "\n") expected_names = [ Name(lineno=2, name="typing.TYPE_CHECKING"), Name(lineno=5, name="HistoryType"), Name(lineno=5, name="QWebHistory"), Name(lineno=5, name="typing.cast"), ] expected_imports = [ Import(lineno=1, column=1, name="typing"), ImportFrom( lineno=3, column=1, name="QWebHistory", star=False, suggestions=[], ), ] self.assertUnimportEqual(source, expected_names, expected_imports)
def test_cast_attribute(self): self.assertUnimportEqual( source="""\ from typing import TYPE_CHECKING if TYPE_CHECKING: from PyQt5 import QtWebKit HistoryType = cast('QtWebKit.QWebHistory', return_value) """, expected_names=[ Name(lineno=2, name="TYPE_CHECKING"), Name(lineno=5, name="HistoryType"), Name(lineno=5, name="QtWebKit.QWebHistory"), Name(lineno=5, name="cast"), Name(lineno=5, name="return_value"), ], expected_imports=[ ImportFrom( lineno=1, column=1, name="TYPE_CHECKING", package="typing", star=False, suggestions=[], ), ImportFrom( lineno=3, column=1, name="QtWebKit", package="PyQt5", star=False, suggestions=[], ), ], )
def test_names_with_import(self): self.assertUnimportEqual( source="""\ variable = 1 import os class TestClass(): def test_function(self): pass def test_function(): pass """, expected_names=[Name(lineno=1, name="variable")], expected_imports=[ Import(lineno=2, column=1, name="os", package="os") ], )
def test_type_comments(self): self.assertUnimportEqual( source="""\ from typing import Any from typing import Tuple from typing import Union def function(a, b): # type: (Any, str) -> Union[Tuple[None, None], Tuple[str, str]] pass """, expected_names=[ Name(lineno=4, name="Any"), Name(lineno=4, name="str"), Name(lineno=4, name="Union"), Name(lineno=4, name="Tuple"), Name(lineno=4, name="Tuple"), Name(lineno=4, name="str"), Name(lineno=4, name="str"), ], expected_imports=[ ImportFrom( lineno=1, column=1, name="Any", package="typing", star=False, suggestions=[], ), ImportFrom( lineno=2, column=1, name="Tuple", package="typing", star=False, suggestions=[], ), ImportFrom( lineno=3, column=1, name="Union", package="typing", star=False, suggestions=[], ), ], )
from typing import List, Union from unimport.statement import Import, ImportFrom, Name __all__ = ["NAMES", "IMPORTS", "UNUSED_IMPORTS"] NAMES: List[Name] = [ Name(lineno=7, name="test_list", is_all=False), Name(lineno=7, name="List", is_all=False), Name(lineno=7, name="str", is_all=False), Name(lineno=7, name="spam", is_all=False), Name(lineno=7, name="eggs", is_all=False), ] IMPORTS: List[Union[Import, ImportFrom]] = [ Import(lineno=1, column=1, name="sys", package="sys"), ImportFrom( lineno=2, column=1, name="List", package="typing", star=False, suggestions=[], ), ] UNUSED_IMPORTS: List[Union[Import, ImportFrom]] = [ Import(lineno=1, column=1, name="sys", package="sys") ]
from typing import List, Union from unimport.statement import Import, ImportFrom, Name __all__ = ["NAMES", "IMPORTS", "UNUSED_IMPORTS"] NAMES: List[Name] = [ Name(lineno=8, name="Any", is_all=False), Name(lineno=8, name="str", is_all=False), Name(lineno=8, name="Union", is_all=False), Name(lineno=8, name="Tuple", is_all=False), Name(lineno=8, name="Tuple", is_all=False), Name(lineno=8, name="str", is_all=False), Name(lineno=8, name="str", is_all=False), ] IMPORTS: List[Union[Import, ImportFrom]] = [ ImportFrom( lineno=3, column=1, name="Any", package="typing", star=False, suggestions=[], ), ImportFrom( lineno=4, column=1, name="Tuple", package="typing", star=False, suggestions=[],
def visit_Name(self, node: ast.Name) -> None: if not isinstance(node.parent, ast.Attribute): # type: ignore self.names.append(Name(lineno=node.lineno, name=node.id))
from typing import List, Union from unimport.statement import Import, ImportFrom, Name __all__ = ["NAMES", "IMPORTS", "UNUSED_IMPORTS"] NAMES: List[Name] = [ Name(lineno=3, name="print", is_all=False), Name(lineno=3, name="t", is_all=False), Name(lineno=9, name="t", is_all=False), Name(lineno=12, name="f", is_all=False), Name(lineno=16, name="print", is_all=False), Name(lineno=16, name="t", is_all=False), ] IMPORTS: List[Union[Import, ImportFrom]] = [ Import(lineno=1, column=1, name="t", package="t"), ImportFrom(lineno=6, column=1, name="t", package="l", star=False, suggestions=[]), ImportFrom(lineno=7, column=1, name="y", package="x", star=False, suggestions=[]), ImportFrom(lineno=7, column=2, name="z",
from typing import List, Union from unimport.statement import Import, ImportFrom, Name __all__ = ["NAMES", "IMPORTS", "UNUSED_IMPORTS"] NAMES: List[Name] = [ Name(lineno=4, name="AUTHENTICATION_BACKENDS", is_all=False) ] IMPORTS: List[Union[Import, ImportFrom]] = [ ImportFrom( lineno=1, column=1, name="AUTHENTICATION_BACKENDS", package="django.conf.global_settings", star=False, suggestions=[], ), ImportFrom( lineno=1, column=2, name="TEMPLATE_CONTEXT_PROCESSORS", package="django.conf.global_settings", star=False, suggestions=[], ), ] UNUSED_IMPORTS: List[Union[Import, ImportFrom]] = [ ImportFrom( lineno=1, column=2,
from typing import List, Union from unimport.statement import Import, ImportFrom, Name __all__ = ["NAMES", "IMPORTS", "UNUSED_IMPORTS"] NAMES: List[Name] = [ Name(lineno=4, name="Dict", is_all=False), Name(lineno=4, name="Literal", is_all=False), Name(lineno=4, name="Dict", is_all=False), Name(lineno=5, name="pas", is_all=False), ] IMPORTS: List[Union[Import, ImportFrom]] = [ ImportFrom( lineno=1, column=1, name="Dict", package="typing", star=False, suggestions=[], ), ImportFrom( lineno=1, column=2, name="Literal", package="typing", star=False, suggestions=[], ), ] UNUSED_IMPORTS: List[Union[Import, ImportFrom]] = []
from typing import List, Union from unimport.statement import Import, ImportFrom, Name __all__ = ["NAMES", "IMPORTS", "UNUSED_IMPORTS"] NAMES: List[Name] = [ Name(lineno=8, name="x", is_all=False), Name(lineno=10, name="x", is_all=False), Name(lineno=12, name="x", is_all=False), ] IMPORTS: List[Union[Import, ImportFrom]] = [ Import(lineno=1, column=1, name="x", package="x"), Import(lineno=4, column=1, name="x", package="x"), Import(lineno=7, column=1, name="x", package="x"), ] UNUSED_IMPORTS: List[Union[Import, ImportFrom]] = []
from typing import List, Union from unimport.statement import Import, ImportFrom, Name __all__ = ["NAMES", "IMPORTS", "UNUSED_IMPORTS"] NAMES: List[Name] = [ Name(lineno=6, name="print", is_all=False), Name(lineno=6, name="t", is_all=False), Name(lineno=7, name="ImportError", is_all=False), Name(lineno=10, name="math.pi", is_all=False), ] IMPORTS: List[Union[Import, ImportFrom]] = [ ImportFrom(lineno=2, column=1, name="y", package="x", star=False, suggestions=[]), ImportFrom(lineno=2, column=2, name="z", package="x", star=False, suggestions=[]), ] UNUSED_IMPORTS: List[Union[Import, ImportFrom]] = [ ImportFrom(lineno=2, column=2, name="z", package="x",
from typing import List, Union from unimport.statement import Import, ImportFrom, Name __all__ = ["NAMES", "IMPORTS", "UNUSED_IMPORTS"] NAMES: List[Name] = [ Name(lineno=3, name="ImportError", is_all=False), Name(lineno=4, name="print", is_all=False), ] IMPORTS: List[Union[Import, ImportFrom]] = [] UNUSED_IMPORTS: List[Union[Import, ImportFrom]] = []
from typing import List, Union from unimport.statement import Import, ImportFrom, Name __all__ = ["NAMES", "IMPORTS", "UNUSED_IMPORTS"] NAMES: List[Name] = [ Name(lineno=6, name="print", is_all=False), Name(lineno=6, name="t", is_all=False), ] IMPORTS: List[Union[Import, ImportFrom]] = [ Import(lineno=1, column=1, name="t", package="t"), ImportFrom( lineno=2, column=1, name="t", package="l", star=False, suggestions=[] ), ImportFrom( lineno=3, column=1, name="y", package="x", star=False, suggestions=[] ), ImportFrom( lineno=3, column=2, name="z", package="x", star=False, suggestions=[] ), ImportFrom( lineno=3, column=3, name="t", package="x", star=False, suggestions=[] ), ] UNUSED_IMPORTS: List[Union[Import, ImportFrom]] = [ ImportFrom( lineno=3, column=2, name="z", package="x", star=False, suggestions=[] ), ImportFrom(
def clear(): Name.clear() Import.clear() ImportFrom.clear() Scope.clear()
def visit_Name(self, node: ast.Name) -> None: if not isinstance(node.parent, ast.Attribute): # type: ignore Name.register(lineno=node.lineno, name=node.id, node=node)
from typing import List, Union from unimport.statement import Import, ImportFrom, Name __all__ = ["NAMES", "IMPORTS", "UNUSED_IMPORTS"] NAMES: List[Name] = [ Name(lineno=4, name="List", is_all=False), Name(lineno=4, name="Dict", is_all=False), ] IMPORTS: List[Union[Import, ImportFrom]] = [ ImportFrom( lineno=1, column=1, name="Dict", package="typing", star=False, suggestions=[], ), ImportFrom( lineno=1, column=2, name="List", package="typing", star=False, suggestions=[], ), ] UNUSED_IMPORTS: List[Union[Import, ImportFrom]] = []
from typing import List, Union from unimport.statement import Import, ImportFrom, Name __all__ = ["NAMES", "IMPORTS", "UNUSED_IMPORTS"] NAMES: List[Name] = [Name(lineno=5, name="x", is_all=False)] IMPORTS: List[Union[Import, ImportFrom]] = [ Import(lineno=1, column=1, name="x", package="x") ] UNUSED_IMPORTS: List[Union[Import, ImportFrom]] = []
def visit_Name(self, node: ast.Name) -> None: self.names.append(Name(lineno=node.lineno, name=node.id))
from typing import List, Union from unimport.statement import Import, ImportFrom, Name NAMES: List[Name] = [ Name(lineno=12, name="some", is_all=False), Name(lineno=13, name="calls", is_all=False), Name(lineno=16, name="after", is_all=False), ] IMPORTS: List[Union[Import, ImportFrom]] = [ Import(lineno=1, column=1, name="x", package="x"), Import(lineno=2, column=1, name="x.y", package="x.y"), Import(lineno=3, column=1, name="x.y.z", package="x.y.z"), Import(lineno=4, column=1, name="x", package="x"), Import(lineno=4, column=2, name="x.y", package="x.y"), Import(lineno=5, column=1, name="x", package="x"), Import(lineno=5, column=2, name="x.y", package="x.y"), Import(lineno=5, column=3, name="x.y.z", package="x.y.z"), Import(lineno=6, column=1, name="x.y", package="x.y"), Import(lineno=6, column=2, name="x.y.z", package="x.y.z"), Import(lineno=6, column=3, name="x.y.z", package="x.y.z"), Import(lineno=7, column=1, name="x.y", package="x.y"), Import(lineno=7, column=2, name="x.y", package="x.y"), Import(lineno=7, column=3, name="x.y.z", package="x.y.z"), ImportFrom(lineno=8, column=1, name="y", package="x", star=False, suggestions=[]), ImportFrom(lineno=9,
from typing import List, Union from unimport.statement import Import, ImportFrom, Name __all__ = ["NAMES", "IMPORTS", "UNUSED_IMPORTS"] NAMES: List[Name] = [ Name(lineno=11, name="y", is_all=False), Name(lineno=11, name="q", is_all=False), Name(lineno=11, name="e", is_all=False), Name(lineno=11, name="t", is_all=False), ] IMPORTS: List[Union[Import, ImportFrom]] = [ ImportFrom(lineno=1, column=1, name="q", package="x", star=False, suggestions=[]), ImportFrom(lineno=1, column=2, name="e", package="x", star=False, suggestions=[]), ImportFrom(lineno=1, column=3, name="r", package="x", star=False, suggestions=[]),
from typing import List, Union from unimport.statement import Import, ImportFrom, Name __all__ = ["NAMES", "IMPORTS", "UNUSED_IMPORTS"] NAMES: List[Name] = [ Name(lineno=12, name="p", is_all=False), Name(lineno=12, name="Path", is_all=False), Name(lineno=13, name="print", is_all=False), Name(lineno=13, name="ll", is_all=False), ] IMPORTS: List[Union[Import, ImportFrom]] = [ ImportFrom(lineno=1, column=1, name="y", package="x", star=False, suggestions=[]), ImportFrom(lineno=2, column=1, name="y", package="x", star=False, suggestions=[]), ImportFrom(lineno=3, column=1, name="x", package="t", star=False, suggestions=[]),
from typing import List, Union from unimport.statement import Import, ImportFrom, Name __all__ = ["NAMES", "IMPORTS", "UNUSED_IMPORTS"] NAMES: List[Name] = [ Name(lineno=4, name="CodeRange", is_all=False), Name(lineno=4, name="PositionProvider", is_all=False), ] IMPORTS: List[Union[Import, ImportFrom]] = [ ImportFrom( lineno=1, column=1, name="libcst.metadata", package="libcst.metadata", star=True, suggestions=["CodeRange", "PositionProvider"], ) ] UNUSED_IMPORTS: List[Union[Import, ImportFrom]] = [ ImportFrom( lineno=1, column=1, name="libcst.metadata", package="libcst.metadata", star=True, suggestions=["CodeRange", "PositionProvider"], ) ]
from typing import List, Union from unimport.statement import Import, ImportFrom, Name __all__ = ["NAMES", "IMPORTS", "UNUSED_IMPORTS"] NAMES: List[Name] = [ Name(lineno=3, name="Exception", is_all=False), Name(lineno=8, name="BaseException", is_all=False), Name(lineno=13, name="OSError", is_all=False), Name(lineno=18, name="OSError", is_all=False), Name(lineno=18, name="AttributeError", is_all=False), ] IMPORTS: List[Union[Import, ImportFrom]] = [] UNUSED_IMPORTS: List[Union[Import, ImportFrom]] = []
from typing import List, Union from unimport.statement import Import, ImportFrom, Name __all__ = ["NAMES", "IMPORTS", "UNUSED_IMPORTS"] NAMES: List[Name] = [ Name(lineno=5, name="print", is_all=False), Name(lineno=5, name="bar", is_all=False), ] IMPORTS: List[Union[Import, ImportFrom]] = [ Import(lineno=1, column=1, name="c", package="a"), Import(lineno=1, column=2, name="k", package="l"), Import(lineno=1, column=3, name="ii", package="i"), Import(lineno=2, column=1, name="bar", package="bar"), Import(lineno=2, column=2, name="i", package="i"), Import(lineno=2, column=3, name="z", package="x"), Import(lineno=3, column=1, name="bar", package="bar"), Import(lineno=3, column=2, name="i", package="i"), Import(lineno=3, column=3, name="z", package="x"), ] UNUSED_IMPORTS: List[Union[Import, ImportFrom]] = [ Import(lineno=3, column=3, name="z", package="x"), Import(lineno=3, column=2, name="i", package="i"), Import(lineno=2, column=3, name="z", package="x"), Import(lineno=2, column=2, name="i", package="i"), Import(lineno=2, column=1, name="bar", package="bar"), Import(lineno=1, column=3, name="ii", package="i"), Import(lineno=1, column=2, name="k", package="l"), Import(lineno=1, column=1, name="c", package="a"), ]