Skip to content

Commit

Permalink
Merge pull request #490 from mfisher87/ruff
Browse files Browse the repository at this point in the history
Add Ruff to pre-commit (before isort + flake8 to test it as a possible replacement)
  • Loading branch information
maresb authored Aug 30, 2023
2 parents 1758dc7 + 52cb31b commit 2f075e7
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 28 deletions.
9 changes: 7 additions & 2 deletions .flake8
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
[flake8]
# NOTE: Can ruff replace flake8? See `tool.isort`, these two configs should be
# kept in sync until we pick one.
ignore = E203, E266, E501, W503, F403, F401
per-file-ignores =
conda_lock/src_parser/meta_yaml.py:E122

max-line-length = 89
max-complexity = 18
select = B,C,E,F,W,T4,B9
max-complexity = 19
select = B,C,E,F,W,T4,B9
17 changes: 12 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,28 @@ repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: trailing-whitespace
exclude: "^.*\\.patch$"
- id: check-ast
- id: trailing-whitespace
exclude: "^.*\\.patch$"
- id: check-ast

- repo: https://github.com/psf/black
rev: 23.7.0
hooks:
- id: black
language_version: python3

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.0.286
hooks:
- id: ruff
args: ["--fix"]

# Ruff should catch (and mostly fix) everything that flake8 and isort do; if
# either of these checks fails, can Ruff's config be updated to catch the same?
- repo: https://github.com/pycqa/flake8
rev: 6.1.0
hooks:
- id: flake8

- id: flake8
- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
Expand Down
10 changes: 6 additions & 4 deletions conda_lock/conda_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ def fn_to_dist_name(fn: str) -> str:
return fn


def make_lock_files( # noqa: C901
def make_lock_files(
*,
conda: PathLike,
src_files: List[pathlib.Path],
Expand Down Expand Up @@ -768,7 +768,7 @@ def create_lockfile_from_spec(
*,
conda: PathLike,
spec: LockSpecification,
platforms: List[str] = [],
platforms: Optional[List[str]] = None,
lockfile_path: pathlib.Path,
update_spec: Optional[UpdateSpecification] = None,
metadata_choices: AbstractSet[MetadataOption] = frozenset(),
Expand All @@ -778,6 +778,8 @@ def create_lockfile_from_spec(
"""
Solve or update specification
"""
if platforms is None:
platforms = []
assert spec.virtual_package_repo is not None
virtual_package_channel = spec.virtual_package_repo.channel

Expand Down Expand Up @@ -943,10 +945,10 @@ def _render_lockfile_for_install(
if platform not in lock_content.metadata.platforms:
suggested_platforms_section = "platforms:\n- "
suggested_platforms_section += "\n- ".join(
[platform] + lock_content.metadata.platforms
[platform, *lock_content.metadata.platforms]
)
suggested_platform_args = "--platform=" + " --platform=".join(
[platform] + lock_content.metadata.platforms
[platform, *lock_content.metadata.platforms]
)
raise PlatformValidationError(
f"The lockfile {filename} does not contain a solution for the current "
Expand Down
2 changes: 1 addition & 1 deletion conda_lock/conda_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ def update_specs_for_arch(
proc = subprocess.run(
[
str(arg)
for arg in args + ["-p", prefix, "--json", "--dry-run", *to_update]
for arg in [*args, "-p", prefix, "--json", "--dry-run", *to_update]
],
env=conda_env_override(platform),
stdout=subprocess.PIPE,
Expand Down
3 changes: 3 additions & 0 deletions conda_lock/lockfile/v2prelim/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# isort: skip_file
# TODO: Remove the isort skip comment above if/when isort is no longer used. This skip
# exists because isort and ruff disagree about how to sort the imports in this file.
from collections import defaultdict
from typing import ClassVar, Dict, List, Optional

Expand Down
2 changes: 1 addition & 1 deletion conda_lock/models/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def _detect_used_env_var(

if value.startswith("$"):
return value.lstrip("$").strip("{}")
for suffix in preferred_env_var_suffix + [""]:
for suffix in [*preferred_env_var_suffix, ""]:
candidates = {v: k for k, v in os.environ.items() if k.upper().endswith(suffix)}
# try first with a simple match
key = candidates.get(value)
Expand Down
4 changes: 2 additions & 2 deletions conda_lock/src_parser/meta_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def __init__( # type: ignore
__mod__ = __rmod__ = __pos__ = __neg__ = __call__ = \
__getitem__ = __lt__ = __le__ = __gt__ = __ge__ = \
__complex__ = __pow__ = __rpow__ = \
lambda self, *args, **kwargs: self._return_undefined(self._undefined_name) # noqa: E122
lambda self, *args, **kwargs: self._return_undefined(self._undefined_name)
# fmt: on

# Accessing an attribute of an Undefined variable
Expand All @@ -60,7 +60,7 @@ def __getattr__(self, k: str) -> "UndefinedNeverFail":

# Unlike the methods above, Python requires that these
# few methods must always return the correct type
__str__ = __repr__ = lambda self: self._return_value(str()) # type: ignore # noqa: E731
__str__ = __repr__ = lambda self: self._return_value(str()) # type: ignore
__unicode__ = lambda self: self._return_value("") # noqa: E731
__int__ = lambda self: self._return_value(0) # type: ignore # noqa: E731
__float__ = lambda self: self._return_value(0.0) # type: ignore # noqa: E731
Expand Down
6 changes: 0 additions & 6 deletions environments/dev-environment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,13 @@ dependencies:
- check-manifest
- doctr
- filelock
- flake8
- flake8-builtins
- flake8-comprehensions
- flake8-mutable
- python-build
- freezegun
- isort
- mypy
- pre-commit
- pylint
- pytest
- pytest-cov
- pytest-flake8
- pytest-xdist
- pytest-timeout
- tomli
Expand Down
47 changes: 47 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ exclude = [
]

[tool.isort]
# NOTE: Can ruff replace isort? See `tool.ruff.isort`, these two configs should
# be kept in sync until we pick one.
atomic = true
force_grid_wrap = 0
include_trailing_comma = true
Expand Down Expand Up @@ -157,3 +159,48 @@ platforms = ["linux-64", "osx-64", "osx-arm64", "win-64", "osx-arm64", "linux-aa
# This is necessary to pull in the lockfile/filelock dependency
# since we don't handle the optional dependency.
cachecontrol-with-filecache = ">=0.12.9"


[tool.ruff]
target-version = "py38"
ignore = [
"E501",
"F401",
"F403",
# Disabled during migration to Ruff:
"A001",
"A002",
"A003",
"C401",
"C405",
"C408",
"C409",
"C413",
"C414",
"C416",
"RUF012",
"RUF015",
]
line-length = 89
select = [
"A", # flake8-builtins
# "B", # flake8-bugbear
"B006", # Do not use mutable data structures for argument defaults
"C4", # flake8-comprehensions
"C9", # mccabe
"E", # pycodestyle errors
"F", # pyflakes
"I", # isort
"RUF", # ruff rules
"W", # pycodestyle warnings
]

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

[tool.ruff.isort]
# NOTE: Can ruff replace isort? See `tool.isort`, these two configs should be
# kept in sync until we pick one.
lines-after-imports = 2
lines-between-types = 1
known-first-party = ["attr"]
7 changes: 0 additions & 7 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
black
check-manifest
doctr
flake8
flake8-builtins
flake8-comprehensions
flake8-mutable
build
freezegun
isort
mypy
pre_commit
pylint
pytest
pytest-cov
pytest-flake8
pytest-xdist
pytest-timeout
tomli; python_version<"3.11"
Expand Down

0 comments on commit 2f075e7

Please sign in to comment.