An imported name is never used
What it finds
import json with json never used: the import costs start-up time, can fail on machines without the module, and tells readers about a dependency that is not there.
An import counts as used when its name is mentioned in code, or appears as a word in a string or a type comment, where __all__, string annotations and # type: comments name it. Left alone are __init__.py files and stubs, which import to re-export, imports written import x as x or from m import x as x, which say they re-export, imports of submodules such as import a.b, which are mostly written to load them, __future__ imports, and imports directly in a try block, which test whether a module is installed.
From the rule's source, unused_import.rs.
import osimport systx0 reports: sys is imported but never usedfrom typing import List, Dicttx0 reports: Dict is imported but never usedfrom collections import OrderedDict as OrderedDictfrom __future__ import annotationsimport json as jstx0 reports: js is imported but never used try: import numpyexcept ImportError: numpy = None def run(values: "List[int]"): return os.path.join(*values) Turn it on, off or suppress it
The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/python:unused-import" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
# tx0: ignore[tx0/python:unused-import] reason="..."tx0 explain tx0/python:unused-import shows the same in the terminal. Found a false positive? Report it.