Skip to content

Commit

Permalink
treewide: replace flake8 with ruff (#444)
Browse files Browse the repository at this point in the history
* treewide: replace flake8 with ruff

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

* treewide: add `ruff --fix` to `make reformat`, apply fixes

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

Signed-off-by: William Woodruff <[email protected]>
  • Loading branch information
woodruffw authored Dec 22, 2022
1 parent 7e17c54 commit ed4805f
Show file tree
Hide file tree
Showing 14 changed files with 31 additions and 27 deletions.
2 changes: 0 additions & 2 deletions .flake8

This file was deleted.

2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ make lint

* [`black`](https://github.com/psf/black): Code formatting
* [`isort`](https://github.com/PyCQA/isort): Import sorting, ordering
* [`flake8`](https://flake8.pycqa.org/en/latest/): PEP-8 linting, style enforcement
* [`ruff`](https://github.com/charliermarsh/ruff): 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
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,14 @@ lint: env/pyvenv.cfg
. env/bin/activate && \
black --check $(ALL_PY_SRCS) && \
isort --check $(ALL_PY_SRCS) && \
flake8 $(ALL_PY_SRCS) && \
ruff $(ALL_PY_SRCS) && \
mypy $(PY_MODULE) && \
interrogate -c pyproject.toml .

.PHONY: reformat
reformat:
. env/bin/activate && \
ruff --fix $(ALL_PY_SRCS) && \
black $(ALL_PY_SRCS) && \
isort $(ALL_PY_SRCS)

Expand Down
4 changes: 2 additions & 2 deletions pip_audit/_audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import logging
from dataclasses import dataclass
from typing import Iterator, Tuple
from typing import Iterator

from pip_audit._dependency_source import DependencySource
from pip_audit._service import Dependency, VulnerabilityResult, VulnerabilityService
Expand Down Expand Up @@ -47,7 +47,7 @@ def __init__(

def audit(
self, source: DependencySource
) -> Iterator[Tuple[Dependency, list[VulnerabilityResult]]]:
) -> Iterator[tuple[Dependency, list[VulnerabilityResult]]]:
"""
Perform the auditing step, collecting dependencies from `source`.
Expand Down
4 changes: 2 additions & 2 deletions pip_audit/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import sys
from contextlib import ExitStack, contextmanager
from pathlib import Path
from typing import IO, Iterator, NoReturn, Type, cast
from typing import IO, Iterator, NoReturn, cast

from pip_audit import __version__
from pip_audit._audit import AuditOptions, Auditor
Expand Down Expand Up @@ -147,7 +147,7 @@ def __str__(self) -> str:
return self.value


def _enum_help(msg: str, e: Type[enum.Enum]) -> str: # pragma: no cover
def _enum_help(msg: str, e: type[enum.Enum]) -> str: # pragma: no cover
"""
Render a `--help`-style string for the given enumeration.
"""
Expand Down
4 changes: 2 additions & 2 deletions pip_audit/_dependency_source/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from __future__ import annotations

from abc import ABC, abstractmethod
from typing import Iterator, Tuple
from typing import Iterator

from packaging.requirements import Requirement

Expand Down Expand Up @@ -77,7 +77,7 @@ def resolve(self, req: Requirement) -> list[Dependency]: # pragma: no cover

def resolve_all(
self, reqs: Iterator[Requirement]
) -> Iterator[Tuple[Requirement, list[Dependency]]]:
) -> Iterator[tuple[Requirement, list[Dependency]]]:
"""
Resolve a collection of `Requirement`s into their respective `Dependency` sets.
Expand Down
6 changes: 3 additions & 3 deletions pip_audit/_dependency_source/requirement.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from contextlib import ExitStack
from pathlib import Path
from tempfile import NamedTemporaryFile
from typing import IO, Iterator, Tuple, cast
from typing import IO, Iterator, cast

from packaging.requirements import Requirement
from packaging.specifiers import SpecifierSet
Expand Down Expand Up @@ -241,7 +241,7 @@ def _collect_preresolved_deps(
self,
reqs: Iterator[InstallRequirement],
require_hashes: bool = False,
) -> Iterator[Tuple[Requirement, Dependency]]:
) -> Iterator[tuple[Requirement, Dependency]]:
"""
Collect pre-resolved (pinned) dependencies, optionally enforcing a
hash requirement policy.
Expand Down Expand Up @@ -292,7 +292,7 @@ def _build_hash_options_mapping(self, hash_options: list[str]) -> dict[str, list

def _collect_cached_deps(
self, filename: Path, reqs: list[InstallRequirement]
) -> Iterator[Tuple[Requirement, Dependency]]:
) -> Iterator[tuple[Requirement, Dependency]]:
"""
Collect resolved dependencies for a given requirements file, retrieving them from the
dependency cache if possible.
Expand Down
4 changes: 2 additions & 2 deletions pip_audit/_format/columns.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from __future__ import annotations

from itertools import zip_longest
from typing import Any, Iterable, Tuple, cast
from typing import Any, Iterable, cast

from packaging.version import Version

Expand All @@ -15,7 +15,7 @@
from .interface import VulnerabilityFormat


def tabulate(rows: Iterable[Iterable[Any]]) -> Tuple[list[str], list[int]]:
def tabulate(rows: Iterable[Iterable[Any]]) -> tuple[list[str], list[int]]:
"""Return a list of formatted rows and a list of column sizes.
For example::
>>> tabulate([['foobar', 2000], [0xdeadbeef]])
Expand Down
4 changes: 2 additions & 2 deletions pip_audit/_service/osv.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import json
import logging
from pathlib import Path
from typing import Any, Tuple, cast
from typing import Any, cast

import requests
from packaging.version import Version
Expand Down Expand Up @@ -43,7 +43,7 @@ def __init__(self, cache_dir: Path | None = None, timeout: int | None = None):
self.session = caching_session(cache_dir, use_pip=False)
self.timeout = timeout

def query(self, spec: Dependency) -> Tuple[Dependency, list[VulnerabilityResult]]:
def query(self, spec: Dependency) -> tuple[Dependency, list[VulnerabilityResult]]:
"""
Queries OSV for the given `Dependency` specification.
Expand Down
4 changes: 2 additions & 2 deletions pip_audit/_service/pypi.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import logging
from pathlib import Path
from typing import Tuple, cast
from typing import cast

import requests
from packaging.version import InvalidVersion, Version
Expand Down Expand Up @@ -45,7 +45,7 @@ def __init__(self, cache_dir: Path | None = None, timeout: int | None = None) ->
self.session = caching_session(cache_dir)
self.timeout = timeout

def query(self, spec: Dependency) -> Tuple[Dependency, list[VulnerabilityResult]]:
def query(self, spec: Dependency) -> tuple[Dependency, list[VulnerabilityResult]]:
"""
Queries PyPI for the given `Dependency` specification.
Expand Down
6 changes: 3 additions & 3 deletions pip_audit/_virtual_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import logging
import venv
from types import SimpleNamespace
from typing import Iterator, Tuple
from typing import Iterator

from packaging.version import Version

Expand Down Expand Up @@ -54,7 +54,7 @@ def __init__(self, install_args: list[str], state: AuditState = AuditState()):
"""
super().__init__(with_pip=True)
self._install_args = install_args
self._packages: list[Tuple[str, Version]] | None = None
self._packages: list[tuple[str, Version]] | None = None
self._state = state

def post_setup(self, context: SimpleNamespace) -> None:
Expand Down Expand Up @@ -124,7 +124,7 @@ def post_setup(self, context: SimpleNamespace) -> None:
self._packages.append((package["name"], Version(package["version"])))

@property
def installed_packages(self) -> Iterator[Tuple[str, Version]]:
def installed_packages(self) -> Iterator[tuple[str, Version]]:
"""
A property to inspect the list of packages installed in the virtual environment.
Expand Down
7 changes: 6 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ test = [
]
lint = [
"black>=22.3.0",
"flake8",
"ruff",
"interrogate",
"isort",
"mypy",
Expand Down Expand Up @@ -108,3 +108,8 @@ warn_unused_ignores = true
[tool.bump]
input = "pip_audit/__init__.py"
reset = true

[tool.ruff]
line-length = 100
select = ["E", "F", "W", "UP"]
target-version = "py37"
2 changes: 1 addition & 1 deletion test/dependency_source/test_pyproject.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def _init_pyproject(filename: Path, contents: str) -> pyproject.PyProjectSource:


def _check_file(filename: Path, expected_contents: dict) -> None:
with open(filename, mode="r") as f:
with open(filename) as f:
assert toml.load(f) == expected_contents


Expand Down
6 changes: 3 additions & 3 deletions test/dependency_source/test_requirement.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def _check_fixes(

# Check the requirements files
for (expected_req, req_path) in zip(expected_reqs, req_paths):
with open(req_path, "r") as f:
with open(req_path) as f:
# NOTE: We don't make any guarantees about non-semantic whitespace
# preservation, hence the strip.
assert expected_req == f.read().strip()
Expand Down Expand Up @@ -302,7 +302,7 @@ def test_requirement_source_fix_parse_failure(monkeypatch, req_file):
# Check that the requirements files remain unchanged
# If we encounter a failure while applying a fix, the fix should be rolled back from all files
for (expected_req, req_path) in zip(input_reqs, req_paths):
with open(req_path, "r") as f:
with open(req_path) as f:
assert expected_req == f.read().strip()


Expand Down Expand Up @@ -341,7 +341,7 @@ def mock_replace(*_args, **_kwargs):
# in the process of writing it out and didn't flush.
expected_reqs = ["flask==1.0", "flask==0.5\nrequests==2.0\nflask==0.3"]
for (expected_req, req_path) in zip(expected_reqs, req_paths):
with open(req_path, "r") as f:
with open(req_path) as f:
assert expected_req == f.read().strip()


Expand Down

0 comments on commit ed4805f

Please sign in to comment.