Skip to content

Commit 9014a94

Browse files
Add --config parameter to specify pyproject.toml path (#352)
If you have a specific path to pyproject.toml in your project (e.g. not in the root), this allows Vulture to read a config from that path. --------- Co-authored-by: Jendrik Seipp <[email protected]>
1 parent 3b12e69 commit 9014a94

File tree

5 files changed

+38
-1
lines changed

5 files changed

+38
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Use `ruff` for linting (Anh Trinh, #347).
44
* Use `ruff` for formatting (Anh Trinh, #349).
55
* Replace `tox` by `pre-commit` for linting and formatting (Anh Trinh, #349).
6+
* Add `--config` flag to specify path to pyproject.toml configuration file (Glen Robertson #352).
67

78
# 2.11 (2024-01-06)
89

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ sort_by_size = true
185185
verbose = true
186186
```
187187

188+
Vulture will automatically look for a `pyproject.toml` in the current working directory.
189+
190+
To use a `pyproject.toml` in another directory, you can use the `--config path/to/pyproject.toml` flag.
191+
188192
## Version control integration
189193

190194
You can use a [pre-commit](https://pre-commit.com/#install) hook to run

tests/test_config.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44

55
from io import BytesIO
6+
import pathlib
67
from textwrap import dedent
78

89
import pytest
@@ -33,6 +34,7 @@ def test_cli_args():
3334
exclude=["file*.py", "dir/"],
3435
ignore_decorators=["deco1", "deco2"],
3536
ignore_names=["name1", "name2"],
37+
config="pyproject.toml",
3638
make_whitelist=True,
3739
min_confidence=10,
3840
sort_by_size=True,
@@ -164,6 +166,7 @@ def test_config_merging():
164166
exclude=["cli_exclude"],
165167
ignore_decorators=["cli_deco"],
166168
ignore_names=["cli_name"],
169+
config="pyproject.toml",
167170
make_whitelist=True,
168171
min_confidence=20,
169172
sort_by_size=True,
@@ -172,6 +175,23 @@ def test_config_merging():
172175
assert result == expected
173176

174177

178+
def test_toml_config_custom_path():
179+
"""
180+
Ensure that TOML pyproject.toml files can be read from a custom path,
181+
other than the current working directory.
182+
183+
Test file is in tests/toml/mock_pyproject.toml
184+
"""
185+
here = pathlib.Path(__file__).parent
186+
tomlfile_path = here.joinpath("toml", "mock_pyproject.toml")
187+
cliargs = [
188+
f"--config={tomlfile_path}",
189+
"cli_path",
190+
]
191+
result = make_config(cliargs)
192+
assert result["ignore_names"] == ["name_from_toml_file"]
193+
194+
175195
def test_config_merging_missing():
176196
"""
177197
If we have set a boolean value in the TOML file, but not on the CLI, we

tests/toml/mock_pyproject.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# This file exists for the test case: test_config::test_toml_config_custom_path
2+
3+
[tool.vulture]
4+
verbose = true
5+
ignore_names = ["name_from_toml_file"]

vulture/config.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#: Possible configuration options and their respective defaults
1616
DEFAULTS = {
17+
"config": "pyproject.toml",
1718
"min_confidence": 0,
1819
"paths": [],
1920
"exclude": [],
@@ -158,6 +159,12 @@ def csv(exclude):
158159
default=missing,
159160
help="Sort unused functions and classes by their lines of code.",
160161
)
162+
parser.add_argument(
163+
"--config",
164+
type=str,
165+
default="pyproject.toml",
166+
help="Path to pyproject.toml config file.",
167+
)
161168
parser.add_argument(
162169
"-v", "--verbose", action="store_true", default=missing
163170
)
@@ -195,7 +202,7 @@ def make_config(argv=None, tomlfile=None):
195202
config = _parse_toml(tomlfile)
196203
detected_toml_path = str(tomlfile)
197204
else:
198-
toml_path = pathlib.Path("pyproject.toml").resolve()
205+
toml_path = pathlib.Path(cli_config["config"]).resolve()
199206
if toml_path.is_file():
200207
with open(toml_path, "rb") as fconfig:
201208
config = _parse_toml(fconfig)

0 commit comments

Comments
 (0)