Skip to content

Commit

Permalink
Use ruff instead of black and use new ruff config syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaume-alliander committed Sep 2, 2024
1 parent be8433a commit 2bfafad
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
15 changes: 6 additions & 9 deletions SConstruct.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,16 @@ def _exec(command: str, env: Mapping | None = None) -> int:
if "all" in COMMAND_LINE_TARGETS:
COMMAND_LINE_TARGETS += ["quality", "format"]

# Ruff fixes and lints, hence its appearance twice.
if "format" in COMMAND_LINE_TARGETS:
COMMAND_LINE_TARGETS += ["black", "ruff"]

if "quality" in COMMAND_LINE_TARGETS:
COMMAND_LINE_TARGETS += ["ruff", "lock", "type", "test", "coverage", "license"]
COMMAND_LINE_TARGETS += ["lint", "lock", "type", "test", "coverage", "license"]

# Formatting targets, which might change files. Let's run them *before* the linters and friends.
# This is why ruff is the first of the quality target, as it fixes things as well.
if "black" in COMMAND_LINE_TARGETS:
cmd = f"black SConstruct.py {_SUBJECT} {_TEST_SUBJECT} examples"
if "format" in COMMAND_LINE_TARGETS:
cmd = f"ruff format SConstruct.py {_SUBJECT} {_TEST_SUBJECT} examples"
if _CHECK_ONLY:
cmd += " --check"
cmd += " --diff"
_exec(cmd)

# Quality target.
Expand All @@ -57,9 +54,9 @@ def _exec(command: str, env: Mapping | None = None) -> int:
# RUF100 means: remove #noqa when not relevant.
# A lot are adding during generation because due to comments, some lines can be too long. They do not all
# end up being too long, so let's clean up the not relevant one.
cmd = f"ruff {_SUBJECT} {param}"
cmd = f"ruff check {_SUBJECT} {param}"
# Tries to fix what it can, forget about the rest.
cmd_test = f"ruff {_TEST_SUBJECT} --fix-only"
cmd_test = f"ruff check {_TEST_SUBJECT} --fix-only"
if _CHECK_ONLY:
cmd += " --no-fix"
cmd_test += " --no-fix"
Expand Down
5 changes: 2 additions & 3 deletions pycgmes/utils/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ def cgmes_attribute_names_in_profile(self, profile: BaseProfile | None) -> set[F
# The field is defined as a pydantic. Field, not a dataclass.field,
# so access to metadata is a tad different. Furthermore, pyright is confused by extra.
if (
profile is None
or (profile in f.default.json_schema_extra["in_profiles"]) # pyright: ignore[reportGeneralTypeIssues]
profile is None or (profile in f.default.json_schema_extra["in_profiles"]) # pyright: ignore[reportAttributeAccessIssue]
)
if f.name != "mRID"
}
Expand All @@ -113,7 +112,7 @@ def cgmes_attributes_in_profile(self, profile: BaseProfile | None) -> dict[str,
with thus the parent class included in the attribute name.
"""
# What will be returned, has the qualname as key...
qual_attrs: dict[str, "CgmesAttribute"] = {}
qual_attrs: dict[str, CgmesAttribute] = {}
# ... but we check existence with the unqualified (short) name.
seen_attrs = set()

Expand Down
12 changes: 10 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ poetry = ">=1.5.0" # key 'priority' in source
pydantic = ">=2" # Used for dataclasses

[tool.poetry.group.dev.dependencies]
black = "*" # Formatter
coverage = { extras = ["toml"], version = "*" } # unit test coverage
pyright = "*" # replaces mypy
reuse = "*" # Check licence headers
Expand Down Expand Up @@ -88,6 +87,8 @@ typeCheckingMode = "basic"
line-length = 120
# Group violations by containing file.
output-format = "grouped"

[tool.ruff.lint]
select = [
# See https://docs.astral.sh/ruff/rules/
"E", # Default, pyflake
Expand Down Expand Up @@ -117,7 +118,11 @@ ignore = [
# revisit when https://github.com/astral-sh/ruff/issues/7806 is done
"N999", "N815", # Formatting due to CamelCase coming from CGMES.
]
[tool.ruff.pep8-naming]

[tool.ruff.lint.isort]
known-first-party = ["pycgmes", "tests"] # Useful if ruff does not run from the actual root of the project and to import form tests

[tool.ruff.lint.pep8-naming]
# extend= use the default ignored names and add those
extend-ignore-names = [
"G", # makes sense in the context of graph
Expand All @@ -126,6 +131,9 @@ extend-ignore-names = [
"N", # shortcut in stack
]

[tool.ruff.format]
docstring-code-format = true


[tool.pytest.ini_options]
addopts = ["--import-mode=importlib",] # modern way to import tests
Expand Down

0 comments on commit 2bfafad

Please sign in to comment.