Skip to content

Commit

Permalink
Merge pull request #200 from lsst/tickets/DM-48788
Browse files Browse the repository at this point in the history
DM-48788: Switch configuration to ruff format and enable ruff isort checks
  • Loading branch information
timj authored Feb 7, 2025
2 parents 8149115 + 93413c2 commit e8ea385
Show file tree
Hide file tree
Showing 21 changed files with 148 additions and 130 deletions.
18 changes: 8 additions & 10 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,35 +27,33 @@ jobs:
channels: conda-forge,defaults
channel-priority: strict
show-channel-urls: true
miniforge-variant: Miniforge3
use-mamba: true

- name: Install eups and any other conda packages
shell: bash -l {0}
run: |
mamba install -y -q eups
# We have two cores so we can speed up the testing with xdist
- name: Install pytest packages
shell: bash -l {0}
run: mamba install -y -q pytest pytest-xdist pytest-cov
conda install -y -q eups
- name: Install numexpr for tests
shell: bash -l {0}
run: |
mamba install numexpr
conda install numexpr
- name: Install lsst-ts-xml for test coverage of installing into lsst namespace
shell: bash -l {0}
run: |
mamba install lsst-ts-xml
conda install lsst-ts-xml
- name: Install dependencies
shell: bash -l {0}
run: |
pip install uv
uv pip install --system -r requirements.txt
# We have two cores so we can speed up the testing with xdist
- name: Install pytest packages
shell: bash -l {0}
run: uv pip install --system pytest pytest-xdist pytest-cov

- name: List installed packages
shell: bash -l {0}
run: |
Expand Down
16 changes: 0 additions & 16 deletions .github/workflows/lint.yaml

This file was deleted.

19 changes: 4 additions & 15 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,14 @@ repos:
- "--unsafe"
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/psf/black-pre-commit-mirror
rev: 24.10.0
hooks:
- id: black
# It is recommended to specify the latest version of Python
# supported by your project here, or alternatively use
# pre-commit's default_language_version, see
# https://pre-commit.com/#top_level-default_language_version
language_version: python3.11
- repo: https://github.com/pycqa/isort
rev: 5.13.2
hooks:
- id: isort
name: isort (python)
- id: check-toml
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.7.4
rev: v0.9.4
hooks:
- id: ruff
args: [--fix]
- id: ruff-format
- repo: https://github.com/numpy/numpydoc
rev: "v1.8.0"
hooks:
Expand Down
15 changes: 14 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ target-version = ["py311"]
[tool.isort]
profile = "black"
line_length = 110
known_first_party = ["lsst"]

[tool.lsst_versions]
write_to = "python/lsst/utils/version.py"
Expand Down Expand Up @@ -127,7 +128,7 @@ exclude_lines = [

[tool.ruff]
line-length = 110
target-version = "py311"
target-version = "py310"
exclude = [
"__init__.py",
]
Expand Down Expand Up @@ -156,17 +157,28 @@ select = [
"N", # pep8-naming
"W", # pycodestyle
"D", # pydocstyle
"I", # isort
"C4", # comprehensions
"UP", # pyupgrade
"RUF022", # sort __all__
]
extend-select = [
"RUF100", # Warn about unused noqa
]

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

[tool.ruff.lint.pycodestyle]
max-doc-length = 79

[tool.ruff.lint.pydocstyle]
convention = "numpy"

[tool.ruff.format]
docstring-code-format = true
docstring-code-line-length = 79

[tool.numpydoc_validation]
checks = [
"all", # All except the rules listed below.
Expand All @@ -179,6 +191,7 @@ checks = [
"RT01", # Unfortunately our @property trigger this.
"RT02", # Does not want named return value. DM style says we do.
"SS05", # pydocstyle is better at finding infinitive verb.
"GL03", # ruff format of docstrings disagrees.
]
exclude = [
"^test_.*", # Do not test docstrings in test code.
Expand Down
8 changes: 4 additions & 4 deletions python/lsst/utils/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import functools
from collections.abc import Callable
from threading import RLock
from typing import Any, ClassVar, Type, TypeVar
from typing import Any, ClassVar, TypeVar


class Singleton(type):
Expand Down Expand Up @@ -51,7 +51,7 @@ def __call__(cls) -> Any:
return cls._instances[cls]


_T = TypeVar("_T", bound="Type")
_T = TypeVar("_T", bound=Any)


def immutable(cls: _T) -> _T:
Expand Down Expand Up @@ -88,13 +88,13 @@ def __setattr__(self: _T, name: str, value: Any) -> None: # noqa: N807

# mypy says the variable here has signature (str, Any) i.e. no "self";
# I think it's just confused by descriptor stuff.
cls.__setattr__ = __setattr__ # type: ignore
cls.__setattr__ = __setattr__

def __getstate__(self: _T) -> dict: # noqa: N807
# Disable default state-setting when unpickled.
return {}

cls.__getstate__ = __getstate__ # type: ignore[assignment]
cls.__getstate__ = __getstate__

def __setstate__(self: _T, state: Any) -> None: # noqa: N807
# Disable default state-setting when copied.
Expand Down
9 changes: 4 additions & 5 deletions python/lsst/utils/introspection.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
from __future__ import annotations

__all__ = [
"find_outside_stacklevel",
"get_caller_name",
"get_class_of",
"get_full_type_name",
"get_instance_of",
"get_caller_name",
"find_outside_stacklevel",
"take_object_census",
"trace_object_references",
]
Expand Down Expand Up @@ -242,8 +242,7 @@ def find_outside_stacklevel(
.. code-block:: python
warnings.warn(
"A warning message",
stacklevel=find_outside_stacklevel("lsst.daf")
"A warning message", stacklevel=find_outside_stacklevel("lsst.daf")
)
"""
if sys.version_info < (3, 11, 0):
Expand Down Expand Up @@ -373,7 +372,7 @@ def trace_object_references(
>>> # In doctest, the trace extends up to the whole global dict
>>> # if you let it.
>>> trace_object_references(Foo, max_level=2) # doctest: +ELLIPSIS
... # doctest: +NORMALIZE_WHITESPACE
... # doctest: +NORMALIZE_WHITESPACE
([[<lsst.utils.introspection.Foo object at ...>],
[{'object': <lsst.utils.introspection.Foo object at ...>}],
[Holder(bar={'object': <lsst.utils.introspection.Foo object at ...>},
Expand Down
4 changes: 2 additions & 2 deletions python/lsst/utils/iteration.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def sequence_to_string(values: list[int | str]) -> str:
>>> getNameOfSet([1, 2, 3, 5, 7, 8, 9])
'1..3^5^7..9'
>>> getNameOfSet(['node1', 'node2', 'node3'])
>>> getNameOfSet(["node1", "node2", "node3"])
'node1..node3'
>>> getNameOfSet([10, 20, 30, 40])
'10..40:10'
Expand Down Expand Up @@ -252,7 +252,7 @@ def sequence_to_string(values: list[int | str]) -> str:
isinstance(item, str) for item in values
)
if not pure_ints_or_pure_strings:
types = set(type(item) for item in values)
types = {type(item) for item in values}
raise TypeError(f"All items in the input list must be either integers or strings, got {types}")

# Determine the stride for integers
Expand Down
8 changes: 4 additions & 4 deletions python/lsst/utils/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
__all__ = (
"TRACE",
"VERBOSE",
"getLogger",
"getTraceLogger",
"LsstLogAdapter",
"LsstLoggers",
"PeriodicLogger",
"getLogger",
"getTraceLogger",
"trace_set_at",
)

Expand All @@ -28,7 +28,7 @@
from collections.abc import Generator
from contextlib import contextmanager
from logging import LoggerAdapter
from typing import Any, TypeAlias, Union
from typing import Any, TypeAlias

try:
import lsst.log.utils as logUtils
Expand Down Expand Up @@ -349,7 +349,7 @@ def getLogger(name: str | None = None, logger: logging.Logger | None = None) ->
return LsstLogAdapter(logger, {})


LsstLoggers: TypeAlias = Union[logging.Logger, LsstLogAdapter]
LsstLoggers: TypeAlias = logging.Logger | LsstLogAdapter


def getTraceLogger(logger: str | LsstLoggers, trace_level: int) -> LsstLogAdapter:
Expand Down
12 changes: 6 additions & 6 deletions python/lsst/utils/packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@
log = logging.getLogger(__name__)

__all__ = [
"getVersionFromPythonModule",
"Packages",
"getAllPythonDistributions",
"getPythonPackages",
"getEnvironmentPackages",
"getCondaPackages",
"Packages",
"getEnvironmentPackages",
"getPythonPackages",
"getVersionFromPythonModule",
]


Expand Down Expand Up @@ -161,7 +161,7 @@ def getPythonPackages() -> dict[str, str]:

# Not iterating with sys.modules.iteritems() because it's not atomic and
# subject to race conditions
module_names = sorted(list(sys.modules.keys()))
module_names = sorted(sys.modules.keys())

# Use knowledge of package hierarchy to find the versions rather than
# using each name independently. Group all the module names into the
Expand Down Expand Up @@ -464,7 +464,7 @@ def getCondaPackages() -> dict[str, str]:
except KeyError:
continue

packages = {n: v for n, v in sorted(packages.items())}
packages = dict(sorted(packages.items()))

# Try to work out the conda environment name and include it as a fake
# package. The "obvious" way of running "conda info --json" does give
Expand Down
10 changes: 5 additions & 5 deletions python/lsst/utils/plotting/figures.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@

__all__ = [
"get_multiband_plot_colors",
"get_multiband_plot_symbols",
"get_multiband_plot_linestyles",
"get_multiband_plot_symbols",
"make_figure",
]

from typing import TYPE_CHECKING, Any, Dict
from typing import TYPE_CHECKING, Any

if TYPE_CHECKING:
from matplotlib.figure import Figure
Expand Down Expand Up @@ -62,7 +62,7 @@ def make_figure(**kwargs: Any) -> Figure:
return fig


def get_multiband_plot_colors(dark_background: bool = False) -> Dict:
def get_multiband_plot_colors(dark_background: bool = False) -> dict:
"""Get color mappings for multiband plots using SDSS filter names.
Notes
Expand Down Expand Up @@ -102,7 +102,7 @@ def get_multiband_plot_colors(dark_background: bool = False) -> Dict:
return plot_filter_colors_white_background


def get_multiband_plot_symbols() -> Dict:
def get_multiband_plot_symbols() -> dict:
"""Get symbol mappings for multiband plots using SDSS filter names.
Notes
Expand All @@ -125,7 +125,7 @@ def get_multiband_plot_symbols() -> Dict:
return plot_symbols


def get_multiband_plot_linestyles() -> Dict:
def get_multiband_plot_linestyles() -> dict:
"""Get line style mappings for multiband plots using SDSS filter names.
Notes
Expand Down
Loading

0 comments on commit e8ea385

Please sign in to comment.