From b61488ceac66caae83bb94908ec27fac65228dc2 Mon Sep 17 00:00:00 2001 From: Nicholas Devenish Date: Tue, 1 Oct 2024 13:14:01 +0100 Subject: [PATCH] Update pre-commit, replace with ruff. --- .github/workflows/ci.yml | 4 +- .github/workflows/publish-to-pypi.yml | 110 +++++++++++++------------- .pre-commit-config.yaml | 38 +++------ pyproject.toml | 15 ++-- src/nxmx/__init__.py | 11 +-- tests/conftest.py | 12 +-- tests/test_nxmx.py | 24 +++--- 7 files changed, 96 insertions(+), 118 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d872330..24b26e1 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -22,9 +22,9 @@ jobs: - name: Run pytest run: pytest -vs post_build: - name: "CI Successful" + name: CI Successful needs: [build] runs-on: ubuntu-latest steps: - name: Nothing - run: "true" + run: 'true' diff --git a/.github/workflows/publish-to-pypi.yml b/.github/workflows/publish-to-pypi.yml index 0e7258b..758fa09 100644 --- a/.github/workflows/publish-to-pypi.yml +++ b/.github/workflows/publish-to-pypi.yml @@ -6,67 +6,67 @@ jobs: build: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 - - name: Set up Python - uses: actions/setup-python@v4 - with: - python-version: 3.11 - - name: Install Build - run: | - python -mpip install --upgrade pip - pip install build - - name: Build distribution - run: | - python -mbuild - pip install dist/*.whl - - uses: actions/upload-artifact@v3 - with: - path: ./dist/* + - uses: actions/checkout@v4 + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: 3.11 + - name: Install Build + run: | + python -mpip install --upgrade pip + pip install build + - name: Build distribution + run: | + python -mbuild + pip install dist/*.whl + - uses: actions/upload-artifact@v3 + with: + path: ./dist/* test: needs: build runs-on: ubuntu-latest strategy: matrix: - python-version: ["3.9", "3.10", "3.11", "3.12"] + python-version: ['3.9', '3.10', '3.11', '3.12'] steps: - - uses: actions/checkout@v4 - - uses: actions/download-artifact@v3 - with: - name: artifact - path: dist + - uses: actions/checkout@v4 + - uses: actions/download-artifact@v3 + with: + name: artifact + path: dist - - name: Set up Python ${{ matrix.python-version }} - uses: actions/setup-python@v4 - with: - python-version: ${{ matrix.python-version }} - - name: Install dependencies - run: | - python -m pip install --upgrade pip - pip install pytest dist/*.whl -r requirements_dev.txt pytest-md pytest-emoji - - name: Run pytest - uses: pavelzw/pytest-action@510c5e90c360a185039bea56ce8b3e7e51a16507 # v2.2.0 - with: - click-to-expand: false - emoji: true - job-summary: true - verbose: false - custom-arguments: "-v -ra" - custom-pytest: "PYTHONDEVMODE=1 pytest" - report-title: "Test Report" + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v4 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install pytest dist/*.whl -r requirements_dev.txt pytest-md pytest-emoji + - name: Run pytest + uses: pavelzw/pytest-action@510c5e90c360a185039bea56ce8b3e7e51a16507 # v2.2.0 + with: + click-to-expand: false + emoji: true + job-summary: true + verbose: false + custom-arguments: -v -ra + custom-pytest: PYTHONDEVMODE=1 pytest + report-title: Test Report pypi-publish: - name: Upload release to PyPI - if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags') - runs-on: ubuntu-latest - environment: - name: Release - url: https://pypi.org/p/nxmx - permissions: - id-token: write - steps: - - uses: actions/download-artifact@v3 - with: - name: artifact - path: dist - - name: Publish package distributions to PyPI - uses: pypa/gh-action-pypi-publish@release/v1 \ No newline at end of file + name: Upload release to PyPI + if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags') + runs-on: ubuntu-latest + environment: + name: Release + url: https://pypi.org/p/nxmx + permissions: + id-token: write + steps: + - uses: actions/download-artifact@v3 + with: + name: artifact + path: dist + - name: Publish package distributions to PyPI + uses: pypa/gh-action-pypi-publish@release/v1 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index b85e871..e814ce2 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,35 +1,19 @@ -# Run 'libtbx.precommit install' to enable repository pre-commits. -repos: - -# Bring Python code up to date with pyupgrade -- repo: https://github.com/asottile/pyupgrade - rev: v3.3.1 - hooks: - - id: pyupgrade +ci: + autoupdate_schedule: quarterly + skip: [poetry-lock] -# Automatically sort imports with isort -- repo: https://github.com/pycqa/isort - rev: 5.12.0 - hooks: - - id: isort - -# Automatic source code formatting with Black -- repo: https://github.com/psf/black - rev: 22.12.0 - hooks: - - id: black - args: [--safe, --quiet] +repos: -# Enforce style with Flake8 -- repo: https://github.com/PyCQA/flake8 - rev: 6.0.0 +- repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.6.2 hooks: - - id: flake8 - args: [--max-line-length=88, '--select=E401,E711,E712,E713,E714,E721,E722,E901,F401,F402,F403,F405,F631,F632,F633,F811,F812,F821,F822,F841,F901,W191,W291,W292,W293,W602,W603,W604,W605,W606'] + - id: ruff + args: [--fix, --show-fixes, --exit-non-zero-on-fix] + - id: ruff-format # Format YAML & TOML files prettily - repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks - rev: v2.6.0 + rev: v2.14.0 hooks: - id: pretty-format-yaml args: [--autofix, --indent, '2'] @@ -38,7 +22,7 @@ repos: # Syntax check with pre-commit out-of-the-box hooks - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.0.1 + rev: v4.6.0 hooks: - id: check-ast - id: check-json diff --git a/pyproject.toml b/pyproject.toml index 034370e..7027356 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -3,15 +3,8 @@ build-backend = "setuptools.build_meta" requires = ["setuptools>=61", "setuptools-scm"] [project] -classifiers = [ - "Programming Language :: Python :: 3" -] -dependencies = [ - "h5py", - "pint", - "python-dateutil", - "scipy" -] +classifiers = ["Programming Language :: Python :: 3"] +dependencies = ["h5py", "pint", "python-dateutil", "scipy"] description = "Read HDF5 data conforming to the NXmx application definition of the NeXus format" dynamic = ["version"] keywords = ["NeXus", "NXmx"] @@ -20,6 +13,10 @@ name = "nxmx" readme = "README.md" requires-python = ">=3.9" +[tool.ruff.lint] +ignore = ["E501"] +select = ["E", "F", "UP", "W", "I", "C"] + [tool.setuptools.package-data] nxmx = ["py.typed"] diff --git a/src/nxmx/__init__.py b/src/nxmx/__init__.py index 0228903..3fbf560 100644 --- a/src/nxmx/__init__.py +++ b/src/nxmx/__init__.py @@ -5,8 +5,9 @@ import logging import operator from collections import abc, namedtuple +from collections.abc import Iterable, Iterator, Sequence from functools import cached_property, reduce -from typing import Iterable, Iterator, Sequence, Union, overload +from typing import Union, overload import dateutil.parser import h5py @@ -266,7 +267,6 @@ def data_scale_factor(self) -> str | None: if "data_scale_factor" in self._handle: return self._handle["data_scale_factor"][()] - @cached_property def data_offset(self) -> str | None: """ @@ -278,7 +278,6 @@ def data_offset(self) -> str | None: return self._handle["data_offset"][()] - class NXtransformations(H5Mapping): """Collection of axis-based translations and rotations to describe a geometry. @@ -1162,12 +1161,10 @@ def __iter__(self) -> Iterator[NXtransformationsAxis]: return iter(self.transformations) @overload - def __getitem__(self, idx: int) -> NXtransformationsAxis: - ... + def __getitem__(self, idx: int) -> NXtransformationsAxis: ... @overload - def __getitem__(self, idx: slice) -> Sequence[NXtransformationsAxis]: - ... + def __getitem__(self, idx: slice) -> Sequence[NXtransformationsAxis]: ... def __getitem__(self, idx): return self.transformations[idx] diff --git a/tests/conftest.py b/tests/conftest.py index f3421bd..e14cdee 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -79,9 +79,9 @@ def nxmx_example(): "fast_pixel_direction", data=7.5e-5 ) fast_pixel_direction.attrs["transformation_type"] = "translation" - fast_pixel_direction.attrs[ - "depends_on" - ] = "/entry/instrument/detector/module/module_offset" + fast_pixel_direction.attrs["depends_on"] = ( + "/entry/instrument/detector/module/module_offset" + ) fast_pixel_direction.attrs["vector"] = np.array([-1.0, 0.0, 0.0]) fast_pixel_direction.attrs["offset"] = np.array([0.0, 0.0, 0.0]) fast_pixel_direction.attrs["offset_units"] = b"m" @@ -91,9 +91,9 @@ def nxmx_example(): "slow_pixel_direction", data=7.5e-5 ) slow_pixel_direction.attrs["transformation_type"] = "translation" - slow_pixel_direction.attrs[ - "depends_on" - ] = "/entry/instrument/detector/module/module_offset" + slow_pixel_direction.attrs["depends_on"] = ( + "/entry/instrument/detector/module/module_offset" + ) slow_pixel_direction.attrs["vector"] = np.array([0.0, -1.0, 0.0]) slow_pixel_direction.attrs["offset"] = np.array([0.0, 0.0, 0.0]) slow_pixel_direction.attrs["offset_units"] = b"m" diff --git a/tests/test_nxmx.py b/tests/test_nxmx.py index ac1cd47..288d79f 100644 --- a/tests/test_nxmx.py +++ b/tests/test_nxmx.py @@ -263,9 +263,9 @@ def detector_depends_on_example(request): "fast_pixel_direction", data=7.5e-5 ) fast_pixel_direction.attrs["transformation_type"] = "translation" - fast_pixel_direction.attrs[ - "depends_on" - ] = "/entry/instrument/detector/module/module_offset" + fast_pixel_direction.attrs["depends_on"] = ( + "/entry/instrument/detector/module/module_offset" + ) fast_pixel_direction.attrs["vector"] = np.array([-1.0, 0.0, 0.0]) fast_pixel_direction.attrs["offset"] = np.array([0.0, 0.0, 0.0]) fast_pixel_direction.attrs["offset_units"] = "m" @@ -274,13 +274,13 @@ def detector_depends_on_example(request): module_offset = module.create_dataset("module_offset", data=0) module_offset.attrs["transformation_type"] = "translation" if request.param: - module_offset.attrs[ - "depends_on" - ] = "/entry/instrument/detector/transformations/det_z_tune" + module_offset.attrs["depends_on"] = ( + "/entry/instrument/detector/transformations/det_z_tune" + ) else: - module_offset.attrs[ - "depends_on" - ] = "/entry/instrument/detector/transformations/det_z" + module_offset.attrs["depends_on"] = ( + "/entry/instrument/detector/transformations/det_z" + ) module_offset.attrs["vector"] = np.array([1.0, 0.0, 0.0]) module_offset.attrs["offset"] = np.array([0.155985, 0.166904, -0]) module_offset.attrs["offset_units"] = "m" @@ -291,9 +291,9 @@ def detector_depends_on_example(request): det_z_tune = transformations.create_dataset( "det_z_tune", data=np.array([-0.5]) ) - det_z_tune.attrs[ - "depends_on" - ] = b"/entry/instrument/detector/transformations/det_z" + det_z_tune.attrs["depends_on"] = ( + b"/entry/instrument/detector/transformations/det_z" + ) det_z_tune.attrs["transformation_type"] = b"translation" det_z_tune.attrs["units"] = b"mm" det_z_tune.attrs["vector"] = np.array([0.0, 0.0, 1.0])