Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/upstream/main'
Browse files Browse the repository at this point in the history
# Conflicts:
#	CHANGELOG.md
#	vulture/core.py
  • Loading branch information
TravisDart committed Jun 21, 2024
2 parents 02804a3 + 072debc commit e56cc10
Show file tree
Hide file tree
Showing 17 changed files with 144 additions and 151 deletions.
2 changes: 1 addition & 1 deletion .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
- [ ] I have updated the documentation in the README.md file or my changes don't require an update.
- [ ] I have added an entry in CHANGELOG.md.
- [ ] I have added or adapted tests to cover my changes.
- [ ] I have run `tox -e fix-style` to format my code and checked the result with `tox -e style`.
- [ ] I have run `pre-commit run --all-files` to format and lint my code.
4 changes: 0 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ jobs:
- name: Run tests
run: python -m tox -e py

- name: Check style
if: ${{ matrix.python-version == 3.10 && startsWith(matrix.os, 'ubuntu') }}
run: python -m tox -e style

- name: Report coverage to Codecov
uses: codecov/codecov-action@v3
with:
Expand Down
13 changes: 13 additions & 0 deletions .github/workflows/pre-commit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: pre-commit

on:
pull_request:
push:
branches: [main]

jobs:
pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/[email protected]
- uses: pre-commit/[email protected]
20 changes: 4 additions & 16 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
repos:
- repo: https://github.com/psf/black
rev: 22.3.0
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.13
hooks:
- id: black
language_version: python3
exclude: rednotebook/external/.*\.py

- repo: https://github.com/pycqa/flake8
rev: '6.1.0'
hooks:
- id: flake8

- repo: https://github.com/asottile/pyupgrade
rev: v2.28.0
hooks:
- id: pyupgrade
exclude: rednotebook/external/.*\.py
- id: ruff
- id: ruff-format
14 changes: 10 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
# next (unreleased)
* Bump flake8, flake8-comprehensions and flake8-bugbear (Sebastian Csar, #341).

* Use `ruff` for linting (Anh Trinh, #347).
* Use `ruff` for formatting (Anh Trinh, #349).
* Replace `tox` by `pre-commit` for linting and formatting (Anh Trinh, #349).
* Add `--config` flag to specify path to pyproject.toml configuration file (Glen Robertson #352).

# 2.11 (2024-01-06)

* Switch to tomllib/tomli to support heterogeneous arrays (Sebastian Csar, #340).
* Provide whitelist parity for `MagicMock` and `Mock` (maxrake).
* Use .gitignore to exclude files if --exclude is missing from both pyproject.toml and the command line (whosayn, #344, #345).
* Properly detect when no .gitignore is present (travisdart, #346).
* Bump flake8, flake8-comprehensions and flake8-bugbear (Sebastian Csar, #341).
* Provide whitelist parity for `MagicMock` and `Mock` (maxrake, #342).

# 2.10 (2023-10-06)

Expand Down
6 changes: 6 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ of "Fixed issue12.". Please make sure that you only fix the issue at hand
or implement the desired new feature instead of making "drive-by" changes
like adding type hints.

### Formating and linting

Run `pre-commit` using:

$ pre-commit run --all-files

## Testing

Run `tox` using:
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,6 @@ If you want to ignore a whole file or directory, use the `--exclude` parameter
(e.g., `--exclude "*settings.py,*/docs/*.py,*/test_*.py,*/.venv/*.py"`). The
exclude patterns are matched against absolute paths.

Vulture 2.11+ parses the `.gitignore` file in the current working directory for
exclude patterns if the `--exclude` parameter is unused and if there are no
exclude patterns in the pyproject.toml file.

#### Flake8 noqa comments

<!-- Hide noqa docs until we decide whether we want to support it.
Expand Down Expand Up @@ -189,6 +185,10 @@ sort_by_size = true
verbose = true
```

Vulture will automatically look for a `pyproject.toml` in the current working directory.

To use a `pyproject.toml` in another directory, you can use the `--config path/to/pyproject.toml` flag.

## Version control integration

You can use a [pre-commit](https://pre-commit.com/#install) hook to run
Expand Down
87 changes: 70 additions & 17 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,19 +1,72 @@
# NOTE: you have to use single-quoted strings in TOML for regular expressions.
# It's the equivalent of r-strings in Python. Multiline strings are treated as
# verbose regular expressions by Black. Use [ ] to denote a significant space
# character.
[tool.ruff]
exclude = [
".eggs",
".git",
"_build",
"build",
"dist",
"htmlcov",
"vulture.egg-info",
".cache",
".coverage",
".pytest_cache",
".tox",
".venv",
".vscode",
]

[tool.black]
# Same as Black.
line-length = 79
include = '\.pyi?$'
exclude = '''
/(
\.eggs
| \.git
| \.tox
| \.venv
| _build
| build
| dist
)/
'''
indent-width = 4

target-version = "py38"

[tool.ruff.lint]
# ruff enables Pyflakes (`F`) and a subset of the pycodestyle (`E`) codes by default.
select = [
"F", # pyflakes
"E4", "E7", "E9", # pycodestyle
"B", # flake8-bugbear
"C4", # comprehensions
"UP", # pyupgrade
]
ignore = [
"C408", # unnecessary dict call
]

# Allow fix for all enabled rules (when `--fix`) is provided.
fixable = ["ALL"]
unfixable = []

# Allow unused variables when underscore-prefixed.
dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"

[tool.ruff.lint.per-file-ignores]
"vulture/whitelists/*.py" = ["B018"]

[tool.ruff.format]
# Like Black, use double quotes for strings.
quote-style = "double"

# Like Black, indent with spaces, rather than tabs.
indent-style = "space"

# Like Black, respect magic trailing commas.
skip-magic-trailing-comma = false

# Like Black, automatically detect the appropriate line ending.
line-ending = "auto"

# Enable auto-formatting of code examples in docstrings. Markdown,
# reStructuredText code/literal blocks and doctests are all supported.
#
# This is currently disabled by default, but it is planned for this
# to be opt-out in the future.
docstring-code-format = false

# Set the line length limit used when formatting code snippets in
# docstrings.
#
# This only has an effect when the `docstring-code-format` setting is
# enabled.
docstring-code-line-length = "dynamic"
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
pathspec >= 0.12.1
tomli >= 1.1.0; python_version < '3.11'
20 changes: 20 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

from io import BytesIO
import pathlib
from textwrap import dedent

import pytest
Expand Down Expand Up @@ -33,6 +34,7 @@ def test_cli_args():
exclude=["file*.py", "dir/"],
ignore_decorators=["deco1", "deco2"],
ignore_names=["name1", "name2"],
config="pyproject.toml",
make_whitelist=True,
min_confidence=10,
sort_by_size=True,
Expand Down Expand Up @@ -164,6 +166,7 @@ def test_config_merging():
exclude=["cli_exclude"],
ignore_decorators=["cli_deco"],
ignore_names=["cli_name"],
config="pyproject.toml",
make_whitelist=True,
min_confidence=20,
sort_by_size=True,
Expand All @@ -172,6 +175,23 @@ def test_config_merging():
assert result == expected


def test_toml_config_custom_path():
"""
Ensure that TOML pyproject.toml files can be read from a custom path,
other than the current working directory.
Test file is in tests/toml/mock_pyproject.toml
"""
here = pathlib.Path(__file__).parent
tomlfile_path = here.joinpath("toml", "mock_pyproject.toml")
cliargs = [
f"--config={tomlfile_path}",
"cli_path",
]
result = make_config(cliargs)
assert result["ignore_names"] == ["name_from_toml_file"]


def test_config_merging_missing():
"""
If we have set a boolean value in the TOML file, but not on the CLI, we
Expand Down
56 changes: 0 additions & 56 deletions tests/test_gitignore_patterns.py

This file was deleted.

5 changes: 5 additions & 0 deletions tests/toml/mock_pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# This file exists for the test case: test_config::test_toml_config_custom_path

[tool.vulture]
verbose = true
ignore_names = ["name_from_toml_file"]
29 changes: 0 additions & 29 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,3 @@ wheel_build_env = .pkg
filterwarnings =
error::DeprecationWarning
error::PendingDeprecationWarning

[testenv:style]
basepython = python3
deps =
black==22.3.0
flake8==6.1.0
flake8-2020==1.7.0
flake8-bugbear==23.9.16
flake8-comprehensions==3.14.0
pyupgrade==2.28.0
allowlist_externals =
bash
commands =
black --check --diff .
# B028: use !r conversion flag
# C408: unnecessary dict call
flake8 --extend-ignore=B028,C408 setup.py tests/ vulture/
bash -c "pyupgrade --py38-plus `find dev/ tests/ vulture/ -name '*.py'` setup.py"

[testenv:fix-style]
basepython = python3
deps =
black==22.3.0
pyupgrade==2.28.0
allowlist_externals =
bash
commands =
black .
bash -c "pyupgrade --py38-plus --exit-zero `find dev/ tests/ vulture/ -name '*.py'` setup.py"
9 changes: 8 additions & 1 deletion vulture/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#: Possible configuration options and their respective defaults
DEFAULTS = {
"config": "pyproject.toml",
"min_confidence": 0,
"paths": [],
"exclude": [],
Expand Down Expand Up @@ -158,6 +159,12 @@ def csv(exclude):
default=missing,
help="Sort unused functions and classes by their lines of code.",
)
parser.add_argument(
"--config",
type=str,
default="pyproject.toml",
help="Path to pyproject.toml config file.",
)
parser.add_argument(
"-v", "--verbose", action="store_true", default=missing
)
Expand Down Expand Up @@ -195,7 +202,7 @@ def make_config(argv=None, tomlfile=None):
config = _parse_toml(tomlfile)
detected_toml_path = str(tomlfile)
else:
toml_path = pathlib.Path("pyproject.toml").resolve()
toml_path = pathlib.Path(cli_config["config"]).resolve()
if toml_path.is_file():
with open(toml_path, "rb") as fconfig:
config = _parse_toml(fconfig)
Expand Down
Loading

0 comments on commit e56cc10

Please sign in to comment.