| 32 | |
| 33 | |
| 34 | class ImportVisitor(ast.NodeVisitor): |
| 35 | def __init__(self) -> None: |
| 36 | self.__imports = set() |
| 37 | |
| 38 | @property |
| 39 | def test_imports(self) -> set[str]: |
| 40 | imports = set() |
| 41 | for module in self.__imports: |
| 42 | if not module.startswith("test."): |
| 43 | continue |
| 44 | name = module.removeprefix("test.") |
| 45 | |
| 46 | if name == "support" or name.startswith("support."): |
| 47 | continue |
| 48 | |
| 49 | imports.add(name) |
| 50 | |
| 51 | return imports |
| 52 | |
| 53 | @property |
| 54 | def lib_imports(self) -> set[str]: |
| 55 | return {module for module in self.__imports if not module.startswith("test.")} |
| 56 | |
| 57 | def visit_Import(self, node): |
| 58 | for alias in node.names: |
| 59 | self.__imports.add(alias.name) |
| 60 | |
| 61 | def visit_ImportFrom(self, node): |
| 62 | try: |
| 63 | module = node.module |
| 64 | except AttributeError: |
| 65 | # Ignore `from . import my_internal_module` |
| 66 | return |
| 67 | |
| 68 | if module is None: # Ignore `from . import my_internal_module` |
| 69 | return |
| 70 | |
| 71 | for alias in node.names: |
| 72 | # We only care about what we import if it was from the "test" module |
| 73 | if module == "test": |
| 74 | name = f"{module}.{alias.name}" |
| 75 | else: |
| 76 | name = module |
| 77 | |
| 78 | self.__imports.add(name) |
| 79 | |
| 80 | def visit_Call(self, node) -> None: |
| 81 | """ |
| 82 | In test files, there's sometimes use of: |
| 83 | |
| 84 | ```python |
| 85 | import test.support |
| 86 | from test.support import script_helper |
| 87 | |
| 88 | script = support.findfile("_test_atexit.py") |
| 89 | script_helper.run_test_script(script) |
| 90 | ``` |
| 91 |
no outgoing calls
no test coverage detected