Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move to Ruff #147

Merged
merged 2 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .copier-answers.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Changes here will be overwritten by Copier; NEVER EDIT MANUALLY
_commit: c721391
_commit: 91ffffb
_src_path: gh:scipp/copier_template
description: SANS data reduction for the European Spallation Source
max_python: '3.12'
Expand Down
25 changes: 0 additions & 25 deletions .github/workflows/copier.yml

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ jobs:
name: docs_html
path: html/

- uses: JamesIves/github-pages-deploy-action@v4.5.0
- uses: JamesIves/github-pages-deploy-action@v4.6.1
if: ${{ inputs.publish }}
with:
branch: gh-pages
Expand Down
29 changes: 8 additions & 21 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,21 @@ repos:
- id: trailing-whitespace
args: [ --markdown-linebreak-ext=md ]
exclude: '\.svg'
- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort
name: isort (python)
- repo: https://github.com/psf/black-pre-commit-mirror
rev: 23.11.0
hooks:
- id: black
- repo: https://github.com/kynan/nbstripout
rev: 0.6.0
hooks:
- id: nbstripout
types: [ "jupyter" ]
args: [ "--drop-empty-cells",
"--extra-keys 'metadata.language_info.version cell.metadata.jp-MarkdownHeadingCollapsed cell.metadata.pycharm'" ]
- repo: https://github.com/pycqa/flake8
rev: 6.1.0
hooks:
- id: flake8
types: ["python"]
additional_dependencies: ["flake8-bugbear==23.9.16"]
- repo: https://github.com/pycqa/bandit
rev: 1.7.5
hooks:
- id: bandit
additional_dependencies: ["bandit[toml]"]
args: ["-c", "pyproject.toml"]
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.4.3
hooks:
- id: ruff
args: [ --fix ]
types_or: [ python, pyi, jupyter ]
- id: ruff-format
types_or: [ python, pyi ]
- repo: https://github.com/codespell-project/codespell
rev: v2.2.6
hooks:
Expand Down
2 changes: 1 addition & 1 deletion docs/_templates/doc_version.html
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<!-- This will display the version of the docs -->
Current {{ project }} version: {{ version }} (<a href="https://github.com/{{orgname}}/{{ project|lower }}/releases">older versions</a>).
Current ESSsans version: {{ version }} (<a href="https://github.com/scipp/esssans/releases">older versions</a>).
26 changes: 19 additions & 7 deletions docs/conf.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
# -*- coding: utf-8 -*-

import doctest
import os
import sys
from importlib.metadata import PackageNotFoundError
from importlib.metadata import version as get_version

from sphinx.util import logging

sys.path.insert(0, os.path.abspath('.'))

logger = logging.getLogger(__name__)

# General information about the project.
project = u'ESSsans'
copyright = u'2024 Scipp contributors'
author = u'Scipp contributors'
project = 'ESSsans'
copyright = '2024 Scipp contributors'
author = 'Scipp contributors'

html_show_sourcelink = True

Expand All @@ -34,6 +37,8 @@
import sciline.sphinxext.domain_types # noqa: F401

extensions.append('sciline.sphinxext.domain_types')
# See https://github.com/tox-dev/sphinx-autodoc-typehints/issues/457
suppress_warnings = ["config.cache"]
except ModuleNotFoundError:
pass

Expand Down Expand Up @@ -110,8 +115,15 @@
# built documents.
#

release = get_version("esssans")
version = ".".join(release.split('.')[:3]) # CalVer
try:
release = get_version("esssans")
version = ".".join(release.split('.')[:3]) # CalVer
except PackageNotFoundError:
logger.info(
"Warning: determining version from package metadata failed, falling back to "
"a dummy version number."
)
release = version = "0.0.0-dev"

warning_is_error = True

Expand Down
2 changes: 1 addition & 1 deletion docs/developer/coding-conventions.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Code formatting

There are no explicit code formatting conventions since we use `black` to enforce a format.
There are no explicit code formatting conventions since we use `ruff` to enforce a format.

## Docstring format

Expand Down
4 changes: 2 additions & 2 deletions docs/user-guide/common/beam-center-finder.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@
"p.ax.axhline(0, color='cyan')\n",
"p.ax.plot(0, 0, '+', color='k', ms=10)\n",
"dx = 0.25\n",
"style = dict(ha='center', va='center', color='w')\n",
"style = dict(ha='center', va='center', color='w') # noqa: C408\n",
"p.ax.text(dx, dx, 'North-East', **style)\n",
"p.ax.text(-dx, dx, 'North-West', **style)\n",
"p.ax.text(dx, -dx, 'South-East', **style)\n",
Expand All @@ -433,7 +433,7 @@
"metadata": {},
"outputs": [],
"source": [
"kwargs = dict(\n",
"kwargs = dict( # noqa: C408\n",
" data=masked,\n",
" norm=workflow.compute(NormWavelengthTerm[SampleRun]),\n",
" graph=workflow.compute(sans.conversions.ElasticCoordTransformGraph),\n",
Expand Down
2 changes: 1 addition & 1 deletion docs/user-guide/isis/sans2d.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@
")\n",
"parts = (CleanSummedQ[SampleRun, Numerator], CleanSummedQ[SampleRun, Denominator])\n",
"iofqs = (IofQ[SampleRun], IofQ[BackgroundRun], BackgroundSubtractedIofQ)\n",
"keys = monitors + (MaskedData[SampleRun],) + parts + iofqs\n",
"keys = (*monitors, MaskedData[SampleRun], *parts, *iofqs)\n",
"\n",
"results = workflow.compute(keys)\n",
"\n",
Expand Down
2 changes: 1 addition & 1 deletion docs/user-guide/isis/zoom.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@
")\n",
"parts = (CleanSummedQ[SampleRun, Numerator], CleanSummedQ[SampleRun, Denominator])\n",
"iofqs = (IofQ[SampleRun],)\n",
"keys = monitors + (MaskedData[SampleRun],) + parts + iofqs\n",
"keys = (*monitors, MaskedData[SampleRun], *parts, *iofqs)\n",
"\n",
"results = workflow.compute(keys)\n",
"\n",
Expand Down
4 changes: 0 additions & 4 deletions docs/user-guide/loki/loki-direct-beam.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,10 @@
},
"outputs": [],
"source": [
"import numpy as np\n",
"import scipp as sc\n",
"import sciline\n",
"import scippneutron as scn\n",
"import plopp as pp\n",
"from ess import sans\n",
"from ess import loki\n",
"from ess import isissans as isis\n",
"from ess.sans.types import *"
]
},
Expand Down
45 changes: 35 additions & 10 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,43 @@ filterwarnings = [
'ignore:\n Sentinel is not a public part of the traitlets API:DeprecationWarning',
]

[tool.bandit]
# Excluding tests because bandit doesn't like `assert`.
exclude_dirs = ["docs/conf.py", "tests"]
[tool.ruff]
line-length = 88
extend-include = ["*.ipynb"]
extend-exclude = [
".*", "__pycache__", "build", "dist", "install",
]

[tool.ruff.lint]
# See https://docs.astral.sh/ruff/rules/
select = ["B", "C4", "DTZ", "E", "F", "G", "I", "PERF", "PGH", "PT", "PYI", "RUF", "S", "T20", "UP", "W"]
ignore = [
# Conflict with ruff format, see
# https://docs.astral.sh/ruff/formatter/#conflicting-lint-rules
"COM812", "COM819", "D206", "D300", "E111", "E114", "E117", "ISC001", "ISC002", "Q000", "Q001", "Q002", "Q003", "W191",
]
fixable = ["I001", "B010"]
isort.known-first-party = ["esssans"]
pydocstyle.convention = "numpy"

[tool.black]
skip-string-normalization = true
[tool.ruff.lint.per-file-ignores]
# those files have an increased risk of relying on import order
"__init__.py" = ["I"]
"tests/*" = [
"S101", # asserts are fine in tests
"B018", # 'useless expressions' are ok because some tests just check for exceptions
]
"*.ipynb" = [
"E501", # longer lines are sometimes more readable
"F403", # *-imports used with domain types
"F405", # linter may fail to find names because of *-imports
"I", # we don't collect imports at the top
"S101", # asserts are used for demonstration and are safe in notebooks
"T201", # printing is ok for demonstration purposes
]

[tool.isort]
skip_gitignore = true
profile = "black"
known_first_party = ["esssans"]
[tool.ruff.format]
quote-style = "preserve"

[tool.mypy]
strict = true
Expand All @@ -87,7 +113,6 @@ enable_error_code = [
"redundant-expr",
"truthy-bool",
]
show_error_codes = true
warn_unreachable = true

[project.entry-points."beamlime.stateless"]
Expand Down
3 changes: 1 addition & 2 deletions requirements/make_base.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import sys
from argparse import ArgumentParser
from pathlib import Path
from typing import List

import tomli

Expand All @@ -20,7 +19,7 @@
"""


def write_dependencies(dependency_name: str, dependencies: List[str]) -> None:
def write_dependencies(dependency_name: str, dependencies: list[str]) -> None:
path = Path(f"{dependency_name}.in")
if path.exists():
sections = path.read_text().split(CUSTOM_AUTO_SEPARATOR)
Expand Down
4 changes: 0 additions & 4 deletions setup.cfg

This file was deleted.

1 change: 0 additions & 1 deletion src/ess/isissans/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# Copyright (c) 2024 Scipp contributors (https://github.com/scipp)
import sciline
import scipp as sc

from ess.sans.data import Registry
from ess.sans.types import (
BackgroundRun,
Expand Down
5 changes: 3 additions & 2 deletions src/ess/isissans/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""
Providers for the ISIS instruments.
"""

import scipp as sc

from ..sans.types import (
Expand Down Expand Up @@ -37,7 +38,7 @@
def default_parameters() -> dict:
return {
CorrectForGravity: False,
DimsToKeep: tuple(),
DimsToKeep: (),
MonitorOffset[Incident]: MonitorOffset(sc.vector([0, 0, 0], unit='m')),
MonitorOffset[Transmission]: MonitorOffset(sc.vector([0, 0, 0], unit='m')),
DetectorBankOffset: DetectorBankOffset(sc.vector([0, 0, 0], unit='m')),
Expand Down Expand Up @@ -72,7 +73,7 @@ def data_to_tof(


def monitor_to_tof(
da: ConfiguredReducibleMonitor[RunType, MonitorType]
da: ConfiguredReducibleMonitor[RunType, MonitorType],
) -> TofMonitor[RunType, MonitorType]:
"""Dummy conversion of monitor data to time-of-flight data.
The monitor data already has a time-of-flight coordinate."""
Expand Down
4 changes: 2 additions & 2 deletions src/ess/isissans/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
"""
File loading functions for ISIS data, NOT using Mantid.
"""

from typing import NewType

import scipp as sc

from ess.sans.types import MaskedDetectorIDs, PixelMaskFilename

CalibrationFilename = NewType('CalibrationFilename', str)
Expand All @@ -33,7 +33,7 @@ def read_xml_detector_masking(filename: PixelMaskFilename) -> MaskedDetectorIDs:
"""
import xml.etree.ElementTree as ET # nosec

tree = ET.parse(filename) # nosec
tree = ET.parse(filename) # noqa: S314
root = tree.getroot()

masked_detids = []
Expand Down
4 changes: 2 additions & 2 deletions src/ess/isissans/mantidio.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
"""
File loading functions for ISIS data using Mantid.
"""

from typing import NewType, NoReturn

import sciline
import scipp as sc
import scippneutron as scn
from scipp.constants import g

from ess.sans.types import DirectBeam, DirectBeamFilename, Filename, RunType, SampleRun
from scipp.constants import g

from .data import LoadedFileContents
from .io import CalibrationFilename
Expand Down
1 change: 0 additions & 1 deletion src/ess/isissans/sans2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import sciline
import scipp as sc

from ess.sans import providers as sans_providers
from ess.sans.types import MaskedData, SampleRun, ScatteringRunType, TofData

Expand Down
3 changes: 2 additions & 1 deletion src/ess/isissans/visualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""
Plotting functions for ISIS data.
"""

import warnings
from typing import Any

Expand Down Expand Up @@ -44,7 +45,7 @@ def plot_flat_detector_xy(
'Cannot plot 2-D instrument view of data array with non-constant '
'y coordinate along tubes. Use scippneutron.instrument_view instead.'
)
plot_kwargs = dict(aspect='equal')
plot_kwargs = {'aspect': 'equal'}
plot_kwargs.update(kwargs)
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=RuntimeWarning)
Expand Down
1 change: 0 additions & 1 deletion src/ess/isissans/zoom.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2024 Scipp contributors (https://github.com/scipp)
import sciline

from ess.sans import providers as sans_providers

from .data import load_tutorial_direct_beam, load_tutorial_run
Expand Down
Loading