Configuration¶
unref reads the [tool.unref] table of the closest pyproject.toml, from the current directory up:
[tool.unref]
used-decorators = ["app.command", "app.route"]
exclude = ["generated"]
As with ty, the project root is the directory of the closest pyproject.toml with a [tool.unref]
table, or else of the closest pyproject.toml. Its paths are relative to the project root, which is
checked by default.
Another file can be given with --config. In a file not named pyproject.toml, the options are at the
top level. Its paths are relative to the current directory, as in Ruff.
Options¶
| Option | Description |
|---|---|
used-names |
Names of the definitions that count as used. |
used-decorators |
Decorators that make the definitions they decorate count as used. |
exclude |
Files and directories that are not part of the project. |
force-exclude |
Leave out excluded paths even when they are given directly. |
respect-gitignore |
Leave out the files that git ignores. |
python |
Python environment or interpreter that the project runs in. |
dummy-variable-rgx |
Names of the variables that are unused on purpose. |
frameworks |
Frameworks whose built-in rules apply. |
output-format |
How the findings are printed. |
show-transitive |
Also print what only other unused definitions reach. |
overrides |
Options for some of the definitions, see Overrides. |
used-names¶
Names that count as used, such as those a framework looks up. Patterns, with *, ? and [abc].
[tool.unref]
used-names = ["handle_*", "visit_*"]
used-decorators¶
Decorators that make what they decorate count as used, such as a route or a command. A decorator is its
full dotted name as written, without the @. Arguments are ignored: app.command covers
@app.command("name"), but not @command.
[tool.unref]
used-decorators = ["app.command"]
exclude¶
Files and directories that are not checked, and whose references do not count. Patterns in the syntax of
.gitignore, anchored at the project root, as ty's exclude:
[tool.unref]
exclude = ["generated", "scripts/legacy.py", "**/fixtures/"]
As with ty, common directories such as .git, .venv, dist and node_modules are always left out
first, and so are the files that git ignores. !pattern brings one back: exclude = ["!**/dist/"]. A
path given to unref directly is checked unless force-exclude is set.
An excluded file's references do not count either: a definition that only an excluded file uses is
reported as unused. --show-files prints the files that are checked.
force-exclude¶
Leave out the files and directories given to unref directly when they are excluded or git ignores
them, as Ruff's option. Defaults to false.
[tool.unref]
force-exclude = true
respect-gitignore¶
Leave out the files and directories that git ignores, as
Ruff's option: those of the .gitignore files,
the .git/info/exclude file of the repository and the global ignore file of core.excludesFile. Defaults
to true.
[tool.unref]
respect-gitignore = false
python¶
The Python environment or interpreter of the project, as ty's --python:
a virtual environment, an interpreter, or a sys.prefix. A base class from a package installed there,
such as pydantic.BaseModel, is followed through its source, so the members it defines count as used.
Nothing is imported or run.
[tool.unref]
python = ".venv"
Defaults to the first of these that exists, in this order:
- The active virtual environment, unless it is the one that
unrefis installed in, such as that of a pre-commit or prek hook. - The
.venvof the project root. - The virtual environment that
unrefis installed in.
dummy-variable-rgx¶
Names of variables that are unused on purpose, where Python needs a name to bind, such as an unpacking: key, _rest = pair(). Same option and default as
Ruff's, which matches _, __ and
_rest.
[tool.unref]
dummy-variable-rgx = "^_$|^unused_"
frameworks¶
Frameworks whose built-in rules apply, see Frameworks. A framework that is not listed
keeps its default. In a file that is not pyproject.toml, the table is [frameworks].
[tool.unref.frameworks]
unittest = false
output-format¶
How the findings are printed, as Ruff's option.
[tool.unref]
output-format = "github"
| Format | Default | Prints |
|---|---|---|
concise |
true |
One line for each finding: src/app.py:40:5: unused-function 'parse_date'. |
github |
false |
A GitHub Actions annotation for each finding, shown on the file in a pull request. |
show-transitive¶
By default, only the top of each unused chain is printed: a definition that only other unused
definitions call, read or contain is left out. Definitions that only reach each other, such as two
functions that call each other, are all printed. Set it to print every finding, as
Ruff's show-fixes prints every fix. The exit code
is the same either way.
[tool.unref]
show-transitive = true
Command line¶
unref [paths] checks the given files and directories, or the project root.
| Option | Description |
|---|---|
--config |
Configuration file. Defaults to the closest pyproject.toml. |
--python |
The python option. Overrides the configuration. |
--output-format |
The output-format option. Overrides the configuration. |
--show-transitive, --no-show-transitive |
The show-transitive option. Overrides the configuration. |
--exclude |
Patterns that replace the exclude option. Repeat it or separate the patterns with commas. |
--extend-exclude |
Patterns added to the exclude option. |
--force-exclude, --no-force-exclude |
The force-exclude option. Overrides the configuration. |
--respect-gitignore, --no-respect-gitignore |
The respect-gitignore option. Overrides the configuration. |
--show-files |
Print the files that would be checked, and exit. |
--exit-zero, -e |
Exit with status 0 even if something is found. |
--jobs, -j |
Number of threads. Defaults to one for each CPU. |
--version |
Print the version and exit. |
The patterns of --exclude and --extend-exclude are anchored at the current directory, as those of
exclude are at the project root: generated matches only ./generated, and **/generated matches
one at any depth. Absolute paths work too.
Entry points¶
The objects that the entry points in pyproject.toml name count as used, and so does what they use:
[project.scripts]
tool = "tool.cli:main"
[project.entry-points."tool.plugins"]
plugin = "tool.plugins:Plugin.load"
Here, main, Plugin and Plugin.load count as used.
Overrides¶
Options that apply only to some definitions. An override selects them with at least one selector:
| Selector | Selects |
|---|---|
paths |
Definitions in these files and directories. |
base-classes |
Definitions in subclasses of these classes, such as ast.NodeVisitor. |
class-decorators |
Definitions in classes with these decorators, such as plugins.register. |
class-names |
Definitions in classes whose names match these patterns, such as *Values. |
With several selectors, an override applies to what matches all of them. It then takes these options, which add to those of the project:
| Option | Description |
|---|---|
used-names |
As used-names, for the selected definitions. |
used-decorators |
As used-decorators, for the selected definitions. |
[[tool.unref.overrides]]
paths = ["migrations"]
used-names = ["Migration"]
[[tool.unref.overrides]]
base-classes = ["ast.NodeVisitor"]
used-names = ["visit_*"]
[[tool.unref.overrides]]
class-decorators = ["plugins.register"]
used-names = ["*"]
[[tool.unref.overrides]]
class-names = ["*Values"]
used-names = ["*"]
A base class or decorator is its dotted name as the project imports it. Subclasses of subclasses count
too, including through classes from outside the project. A class name is a pattern, as in
used-names, matched against the class's own name: *Values matches Outer.SizeValues.
In a file that is not pyproject.toml, the table is [[overrides]].