Skip to content

Commit

Permalink
chore: switch to ruff
Browse files Browse the repository at this point in the history
Signed-off-by: Frost Ming <me@frostming.com>
  • Loading branch information
frostming committed Feb 2, 2024
1 parent b1f0e92 commit 0e0b49f
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 61 deletions.
22 changes: 5 additions & 17 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,17 @@ repos:
- id: end-of-file-fixer
- id: check-added-large-files

- repo: https://github.com/psf/black
rev: 23.12.1
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: 'v0.1.9'
hooks:
- id: black

- repo: https://github.com/pycqa/isort
rev: 5.13.2
hooks:
- id: isort

- repo: https://github.com/PyCQA/flake8
rev: 7.0.0
hooks:
- id: flake8
additional_dependencies:
- flake8-bugbear
- id: ruff
args: [--fix, --exit-non-zero-on-fix, --show-fixes]
- id: ruff-format

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.8.0
hooks:
- id: mypy
args: [src]
pass_filenames: false
additional_dependencies:
- types-requests
- packaging
7 changes: 2 additions & 5 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
try:
import importlib.metadata as importlib_metadata
except ModuleNotFoundError:
import importlib_metadata
import importlib.metadata

# -- Project information -----------------------------------------------------

Expand All @@ -22,7 +19,7 @@
author = "Frost Ming"

# The full version, including alpha/beta/rc tags
release = importlib_metadata.version("unearth")
release = importlib.metadata.version("unearth")

# -- General configuration ---------------------------------------------------

Expand Down
42 changes: 20 additions & 22 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,30 +69,28 @@ test = "pytest tests/"
lint = "pre-commit run --all-files"
doc = "sphinx-build -b html docs {args:docs/_build}"

[tool.black]
[tool.ruff]
line-length = 88
exclude = '''
/(
\.eggs
| \.git
| \.hg
| \.mypy_cache
| \.tox
| \.venv
| _build
| buck-out
| build
| dist
| tests/fixtures
)/
'''
target-version = "py38"

[tool.isort]
profile = "black"
atomic = true
skip_glob = ["*/setup.py"]
filter_files = true
known_first_party = ["unearth"]
[tool.ruff.lint]
extend-select = [
"I", # isort
"B", # flake8-bugbear
"C4", # flake8-comprehensions
"PGH", # pygrep-hooks
"RUF", # ruff
"W", # pycodestyle
"YTT", # flake8-2020
]
extend-ignore = ["B018", "B019"]
exclude = ["tests/fixtures"]

[tool.ruff.mccabe]
max-complexity = 10

[tool.ruff.isort]
known-first-party = ["unearth"]

[tool.pytest.ini_options]
filterwarnings = [
Expand Down
4 changes: 2 additions & 2 deletions src/unearth/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class KeyringModuleProvider(KeyringBaseProvider):
"""Keyring provider that uses the keyring module."""

def __init__(self) -> None:
import keyring # type: ignore
import keyring

self.keyring = keyring

Expand Down Expand Up @@ -365,7 +365,7 @@ def handle_401(self, resp: Response, **kwargs: Any) -> Response:
req.register_hook("response", self.save_credentials)

# Send our new request
new_resp = resp.connection.send(req, **kwargs) # type: ignore
new_resp = resp.connection.send(req, **kwargs) # type: ignore[attr-defined]
new_resp.history.append(resp)

return new_resp
Expand Down
8 changes: 4 additions & 4 deletions src/unearth/evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,10 @@ def check_requires_python(self, link: Link) -> None:
requires_python = SpecifierSet(
fix_legacy_specifier(link.requires_python)
)
except InvalidSpecifier:
except InvalidSpecifier as e:
raise LinkMismatchError(
f"Invalid requires-python: {link.requires_python}"
)
) from e
if not requires_python.contains(py_version, True):
raise LinkMismatchError(
"The target python version({}) doesn't match "
Expand Down Expand Up @@ -198,7 +198,7 @@ def evaluate_link(self, link: Link) -> Package | None:
try:
wheel_info = parse_wheel_filename(link.filename)
except (InvalidWheelFilename, InvalidVersion) as e:
raise LinkMismatchError(str(e))
raise LinkMismatchError(str(e)) from None
if self._canonical_name != wheel_info[0]:
raise LinkMismatchError(
f"The package name doesn't match {wheel_info[0]}"
Expand Down Expand Up @@ -251,7 +251,7 @@ def evaluate_link(self, link: Link) -> Package | None:
except InvalidVersion:
raise LinkMismatchError(
f"Invalid version in the filename {egg_info}: {version}"
)
) from None
except LinkMismatchError as e:
logger.debug("Skipping link %s: %s", link, e)
return None
Expand Down
2 changes: 1 addition & 1 deletion src/unearth/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def send(self, request: PreparedRequest, *args: Any, **kwargs: Any) -> Response:
)

resp.raw = open(path, "rb")
resp.close = resp.raw.close # type: ignore
resp.close = resp.raw.close # type: ignore[method-assign]

return resp

Expand Down
4 changes: 2 additions & 2 deletions src/unearth/vcs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def run_command(
if extra_env:
env = dict(os.environ, **extra_env)
try:
cmd = [self.name] + cmd # type: ignore
cmd = [self.name, *cmd]
display_cmd = subprocess.list2cmdline(map(str, cmd))
logger.debug("Running command %s", display_cmd)
result = subprocess.run(
Expand Down Expand Up @@ -248,7 +248,7 @@ def get_backend(self, name: str, verbosity: int = 0) -> VersionControl:
try:
return self._registry[name](verbosity=verbosity)
except KeyError:
raise VCSBackendError(name)
raise VCSBackendError(name) from None


vcs_support = VcsSupport()
10 changes: 8 additions & 2 deletions src/unearth/vcs/bazaar.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,14 @@ def fetch_new(
flag = ""
else:
flag = f"-{'v'*self.verbosity}"
cmd_args = ["branch", flag, *self.get_rev_args(rev), url, str(location)]
self.run_command(cmd_args) # type: ignore
cmd_args: list[str | HiddenText] = [
"branch",
flag,
*self.get_rev_args(rev),
url,
str(location),
]
self.run_command(cmd_args)

def update(
self, location: Path, rev: str | None, args: list[str | HiddenText]
Expand Down
4 changes: 3 additions & 1 deletion src/unearth/vcs/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ def get_remote_url(self, location: Path) -> str:
try:
found_remote = remotes[0]
except IndexError:
raise UnpackError(f"Remote not found for {display_path(location)}")
raise UnpackError(
f"Remote not found for {display_path(location)}"
) from None

for remote in remotes:
if remote.startswith("remote.origin.url "):
Expand Down
8 changes: 4 additions & 4 deletions src/unearth/vcs/svn.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,26 +65,26 @@ def fetch_new(
flag = "--quiet"
else:
flag = ""
cmd_args = [
cmd_args: list[str | HiddenText] = [
"checkout",
flag,
"--non-interactive",
*self.get_rev_args(rev),
url,
str(location),
]
self.run_command(cmd_args) # type: ignore
self.run_command(cmd_args)

def update(
self, location: Path, rev: str | None, args: list[str | HiddenText]
) -> None:
cmd_args = [
cmd_args: list[str] = [
"update",
"--non-interactive",
*self.get_rev_args(rev),
str(location),
]
self.run_command(cmd_args) # type: ignore
self.run_command(cmd_args)

def get_remote_url(self, location: Path) -> str:
orig_location = location
Expand Down
2 changes: 1 addition & 1 deletion tests/test_evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def test_evaluate_link_loose_filename(loose: bool, monkeypatch: pytest.MonkeyPat
evaluator = Evaluator("foo")
package = evaluator.evaluate_link(link)
if loose:
assert package.version == "2-2"
assert package is not None and package.version == "2-2"
else:
assert package is None

Expand Down

0 comments on commit 0e0b49f

Please sign in to comment.