Skip to content

Commit

Permalink
Replace isort and black with ruff (#692)
Browse files Browse the repository at this point in the history
* Use ruff for black and isort

Signed-off-by: William Woodruff <[email protected]>

* treewide: `ruff format`

Signed-off-by: William Woodruff <[email protected]>

* CONTRIBUTING: drop black, isort refs

Signed-off-by: William Woodruff <[email protected]>

* reflow again

Signed-off-by: William Woodruff <[email protected]>

---------

Signed-off-by: William Woodruff <[email protected]>
  • Loading branch information
woodruffw authored Oct 30, 2023
1 parent 9f3e85a commit 4aa1a53
Show file tree
Hide file tree
Showing 16 changed files with 136 additions and 67 deletions.
4 changes: 1 addition & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ make lint

`pip-audit` is automatically linted and formatted with a collection of tools:

* [`black`](https://github.com/psf/black): Code formatting
* [`isort`](https://github.com/PyCQA/isort): Import sorting, ordering
* [`ruff`](https://github.com/charliermarsh/ruff): PEP-8 linting, style enforcement
* [`ruff`](https://github.com/charliermarsh/ruff): Formatting, PEP-8 linting, style enforcement
* [`mypy`](https://mypy.readthedocs.io/en/stable/): Static type checking
* [`interrogate`](https://interrogate.readthedocs.io/en/latest/): Documentation coverage

Expand Down
7 changes: 2 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ $(VENV)/pyvenv.cfg: pyproject.toml
.PHONY: lint
lint: $(VENV)/pyvenv.cfg
. $(VENV_BIN)/activate && \
black --check $(ALL_PY_SRCS) && \
isort --check $(ALL_PY_SRCS) && \
ruff format --check $(ALL_PY_SRCS) && \
ruff $(ALL_PY_SRCS) && \
mypy $(PY_MODULE) && \
interrogate -c pyproject.toml .
Expand All @@ -66,9 +65,7 @@ lint: $(VENV)/pyvenv.cfg
reformat:
. $(VENV_BIN)/activate && \
ruff --fix $(ALL_PY_SRCS) && \
black $(ALL_PY_SRCS) && \
isort $(ALL_PY_SRCS)

ruff format $(ALL_PY_SRCS)
.PHONY: test tests
test tests: $(VENV)/pyvenv.cfg
. $(VENV_BIN)/activate && \
Expand Down
23 changes: 18 additions & 5 deletions pip_audit/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,10 @@ def _parser() -> argparse.ArgumentParser: # pragma: no cover
help="audit the given requirements file; this option can be used multiple times",
)
dep_source_args.add_argument(
"project_path", type=Path, nargs="?", help="audit a local Python project at the given path"
"project_path",
type=Path,
nargs="?",
help="audit a local Python project at the given path",
)
parser.add_argument(
"-f",
Expand All @@ -210,7 +213,8 @@ def _parser() -> argparse.ArgumentParser: # pragma: no cover
default=VulnerabilityServiceChoice.Pypi,
metavar="SERVICE",
help=_enum_help(
"the vulnerability service to audit dependencies against", VulnerabilityServiceChoice
"the vulnerability service to audit dependencies against",
VulnerabilityServiceChoice,
),
)
parser.add_argument(
Expand Down Expand Up @@ -250,7 +254,10 @@ def _parser() -> argparse.ArgumentParser: # pragma: no cover
help="display a progress spinner",
)
parser.add_argument(
"--timeout", type=int, default=15, help="set the socket timeout" # Match the `pip` default
"--timeout",
type=int,
default=15,
help="set the socket timeout", # Match the `pip` default
)
dep_source_args.add_argument(
"--path",
Expand Down Expand Up @@ -358,7 +365,10 @@ def _dep_source_from_project_path(
pyproject_path = project_path / "pyproject.toml"
if pyproject_path.is_file():
return PyProjectSource(
pyproject_path, index_url=index_url, extra_index_urls=extra_index_urls, state=state
pyproject_path,
index_url=index_url,
extra_index_urls=extra_index_urls,
state=state,
)

# TODO: Checks for setup.py and other project files will go here.
Expand Down Expand Up @@ -445,7 +455,10 @@ def audit() -> None: # pragma: no cover
)
else:
source = PipSource(
local=args.local, paths=args.paths, skip_editable=args.skip_editable, state=state
local=args.local,
paths=args.paths,
skip_editable=args.skip_editable,
state=state,
)

# `--dry-run` only affects the auditor if `--fix` is also not supplied,
Expand Down
11 changes: 9 additions & 2 deletions pip_audit/_dependency_source/pip.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
import pip_api
from packaging.version import InvalidVersion, Version

from pip_audit._dependency_source import DependencyFixError, DependencySource, DependencySourceError
from pip_audit._dependency_source import (
DependencyFixError,
DependencySource,
DependencySourceError,
)
from pip_audit._fix import ResolvedFixVersion
from pip_audit._service import Dependency, ResolvedDependency, SkippedDependency
from pip_audit._state import AuditState
Expand Down Expand Up @@ -147,7 +151,10 @@ def fix(self, fix_version: ResolvedFixVersion) -> None:
]
try:
subprocess.run(
fix_cmd, check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
fix_cmd,
check=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
except subprocess.CalledProcessError as cpe:
raise PipFixError(
Expand Down
6 changes: 5 additions & 1 deletion pip_audit/_dependency_source/pyproject.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
from packaging.requirements import Requirement
from packaging.specifiers import SpecifierSet

from pip_audit._dependency_source import DependencyFixError, DependencySource, DependencySourceError
from pip_audit._dependency_source import (
DependencyFixError,
DependencySource,
DependencySourceError,
)
from pip_audit._fix import ResolvedFixVersion
from pip_audit._service import Dependency, ResolvedDependency
from pip_audit._state import AuditState
Expand Down
6 changes: 5 additions & 1 deletion pip_audit/_dependency_source/requirement.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
from packaging.specifiers import SpecifierSet
from packaging.utils import canonicalize_name
from packaging.version import Version
from pip_requirements_parser import InstallRequirement, InvalidRequirementLine, RequirementsFile
from pip_requirements_parser import (
InstallRequirement,
InvalidRequirementLine,
RequirementsFile,
)

from pip_audit._dependency_source import (
DependencyFixError,
Expand Down
18 changes: 6 additions & 12 deletions pip_audit/_format/markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,12 @@ def _format_vuln_results(
if not vuln_rows:
return ""

return (
dedent(
f"""
return dedent(
f"""
{header}
{border}
"""
)
+ "\n".join(vuln_rows)
)
) + "\n".join(vuln_rows)

def _format_vuln(
self,
Expand Down Expand Up @@ -139,15 +136,12 @@ def _format_skipped_deps(
if not skipped_dep_rows:
return ""

return (
dedent(
f"""
return dedent(
f"""
{header}
{border}
"""
)
+ "\n".join(skipped_dep_rows)
)
) + "\n".join(skipped_dep_rows)

def _format_skipped_dep(self, dep: service.SkippedDependency) -> str:
return f"{dep.name} | {dep.skip_reason}"
3 changes: 2 additions & 1 deletion pip_audit/_subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ def run(args: Sequence[str], *, log_stdout: bool = False, state: AuditState = Au
stdout += process.stdout.read(4096) # type: ignore
stderr += process.stderr.read(4096) # type: ignore
state.update_state(
f"Running {pretty_args}", stdout.decode(errors="replace") if log_stdout else None
f"Running {pretty_args}",
stdout.decode(errors="replace") if log_stdout else None,
)

if process.returncode != 0:
Expand Down
14 changes: 2 additions & 12 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,10 @@ requires-python = ">=3.7"
[project.optional-dependencies]
test = ["coverage[toml]", "pretend", "pytest", "pytest-cov"]
lint = [
"black>=22.3.0",
# NOTE(ww): ruff is under active development, so we pin conservatively here
# and let Dependabot periodically perform this update.
"ruff < 0.1.4",
"interrogate",
"isort",
"mypy",
"types-html5lib",
"types-requests",
Expand All @@ -66,15 +64,6 @@ Homepage = "https://pypi.org/project/pip-audit/"
Issues = "https://github.com/pypa/pip-audit/issues"
Source = "https://github.com/pypa/pip-audit"

[tool.isort]
line_length = 100
multi_line_output = 3
known_first_party = "pip_audit"
include_trailing_comma = true

[tool.black]
line-length = 100

[tool.interrogate]
# don't enforce documentation coverage for packaging, testing, the virtual
# environment, or the CLI (which is documented separately).
Expand Down Expand Up @@ -106,5 +95,6 @@ reset = true
[tool.ruff]
# Never enforce `E501` (line length violations).
ignore = ["E501"]
select = ["E", "F", "W", "UP"]
select = ["E", "F", "I", "W", "UP"]
target-version = "py37"
line-length = 100
4 changes: 3 additions & 1 deletion test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

def pytest_addoption(parser):
parser.addoption(
"--skip-online", action="store_true", help="skip tests that require network connectivity"
"--skip-online",
action="store_true",
help="skip tests that require network connectivity",
)


Expand Down
6 changes: 4 additions & 2 deletions test/dependency_source/test_pip.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ def test_pip_source_fix(monkeypatch):
source = pip.PipSource()

fix_version = ResolvedFixVersion(
dep=ResolvedDependency(name="pip-api", version=Version("1.0")), version=Version("1.5")
dep=ResolvedDependency(name="pip-api", version=Version("1.0")),
version=Version("1.5"),
)

def run_mock(args, **kwargs):
Expand All @@ -167,7 +168,8 @@ def test_pip_source_fix_failure(monkeypatch):
source = pip.PipSource()

fix_version = ResolvedFixVersion(
dep=ResolvedDependency(name="pip-api", version=Version("1.0")), version=Version("1.5")
dep=ResolvedDependency(name="pip-api", version=Version("1.0")),
version=Version("1.5"),
)

def run_mock(args, **kwargs):
Expand Down
15 changes: 11 additions & 4 deletions test/dependency_source/test_pyproject.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
import toml
from packaging.version import Version

from pip_audit._dependency_source import DependencyFixError, DependencySourceError, pyproject
from pip_audit._dependency_source import (
DependencyFixError,
DependencySourceError,
pyproject,
)
from pip_audit._fix import ResolvedFixVersion
from pip_audit._service import ResolvedDependency
from pip_audit._state import AuditState
Expand Down Expand Up @@ -125,7 +129,8 @@ def test_pyproject_source_fix(req_file):
""",
)
fix = ResolvedFixVersion(
dep=ResolvedDependency(name="flask", version=Version("0.5")), version=Version("1.0")
dep=ResolvedDependency(name="flask", version=Version("0.5")),
version=Version("1.0"),
)
source.fix(fix)
_check_file(source.filename, {"project": {"dependencies": ["flask==1.0"]}})
Expand All @@ -142,7 +147,8 @@ def test_pyproject_source_fix_no_project_section(req_file):
""",
)
fix = ResolvedFixVersion(
dep=ResolvedDependency(name="flask", version=Version("0.5")), version=Version("1.0")
dep=ResolvedDependency(name="flask", version=Version("0.5")),
version=Version("1.0"),
)
with pytest.raises(DependencyFixError):
source.fix(fix)
Expand All @@ -159,7 +165,8 @@ def test_pyproject_source_fix_no_deps(monkeypatch, req_file):
""",
)
fix = ResolvedFixVersion(
dep=ResolvedDependency(name="flask", version=Version("0.5")), version=Version("1.0")
dep=ResolvedDependency(name="flask", version=Version("0.5")),
version=Version("1.0"),
)
source.fix(fix)

Expand Down
28 changes: 19 additions & 9 deletions test/dependency_source/test_requirement.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ def test_requirement_source_fix(req_file):
[req_file()],
[
ResolvedFixVersion(
dep=ResolvedDependency(name="flask", version=Version("0.5")), version=Version("1.0")
dep=ResolvedDependency(name="flask", version=Version("0.5")),
version=Version("1.0"),
)
],
)
Expand Down Expand Up @@ -251,7 +252,8 @@ def test_requirement_source_fix_multiple_files(req_file):
[req_file(), req_file()],
[
ResolvedFixVersion(
dep=ResolvedDependency(name="flask", version=Version("0.5")), version=Version("1.0")
dep=ResolvedDependency(name="flask", version=Version("0.5")),
version=Version("1.0"),
)
],
)
Expand All @@ -264,7 +266,8 @@ def test_requirement_source_fix_specifier_match(req_file):
[req_file(), req_file()],
[
ResolvedFixVersion(
dep=ResolvedDependency(name="flask", version=Version("0.5")), version=Version("1.0")
dep=ResolvedDependency(name="flask", version=Version("0.5")),
version=Version("1.0"),
)
],
)
Expand Down Expand Up @@ -302,7 +305,8 @@ def test_requirement_source_fix_preserve_marker(req_file):
[req_file(), req_file()],
[
ResolvedFixVersion(
dep=ResolvedDependency(name="flask", version=Version("0.5")), version=Version("1.0")
dep=ResolvedDependency(name="flask", version=Version("0.5")),
version=Version("1.0"),
)
],
)
Expand All @@ -318,7 +322,8 @@ def test_requirement_source_fix_comments(req_file):
[req_file(), req_file()],
[
ResolvedFixVersion(
dep=ResolvedDependency(name="flask", version=Version("0.5")), version=Version("1.0")
dep=ResolvedDependency(name="flask", version=Version("0.5")),
version=Version("1.0"),
)
],
)
Expand All @@ -342,7 +347,8 @@ def test_requirement_source_fix_parse_failure(monkeypatch, req_file):
with pytest.raises(DependencyFixError):
source.fix(
ResolvedFixVersion(
dep=ResolvedDependency(name="flask", version=Version("0.5")), version=Version("1.0")
dep=ResolvedDependency(name="flask", version=Version("0.5")),
version=Version("1.0"),
)
)
assert len(logger.warning.calls) == 1
Expand Down Expand Up @@ -378,7 +384,8 @@ def mock_replace(*_args, **_kwargs):
with pytest.raises(DependencyFixError):
source.fix(
ResolvedFixVersion(
dep=ResolvedDependency(name="flask", version=Version("0.5")), version=Version("1.0")
dep=ResolvedDependency(name="flask", version=Version("0.5")),
version=Version("1.0"),
)
)
# One for the parsing error and one for each file that we failed to rollback
Expand Down Expand Up @@ -716,7 +723,8 @@ def test_requirement_source_fix_invalid_lines(req_file):
with pytest.raises(DependencyFixError):
source.fix(
ResolvedFixVersion(
dep=ResolvedDependency(name="flask", version=Version("0.5")), version=Version("1.0")
dep=ResolvedDependency(name="flask", version=Version("0.5")),
version=Version("1.0"),
)
)

Expand Down Expand Up @@ -827,7 +835,9 @@ def test_requirement_source_disable_pip_unpinned_url(req_file):

def test_requirement_source_disable_pip_editable_with_egg_fragment(req_file):
source = _init_requirement(
[(req_file(), "-e file:flask.py#egg=flask==2.0.1")], disable_pip=True, no_deps=True
[(req_file(), "-e file:flask.py#egg=flask==2.0.1")],
disable_pip=True,
no_deps=True,
)

specs = list(source.collect())
Expand Down
Loading

0 comments on commit 4aa1a53

Please sign in to comment.