From 881679de5094a54f122b796b2115e61ac779a145 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Thu, 3 Oct 2024 00:52:46 -0400 Subject: [PATCH] feat: modernize, Python 3.8+ (#130) Signed-off-by: Henry Schreiner --- .github/workflows/cd.yml | 9 ++++++++- .github/workflows/ci.yml | 13 ++++--------- .pre-commit-config.yaml | 13 ++++--------- README.md | 2 +- docs/conf.py | 10 ++-------- pyproject.toml | 19 +++++-------------- src/uhi/numpy_plottable.py | 4 ++-- src/uhi/typing/plottable.py | 10 +--------- tests/test_root.py | 4 ++-- tests/test_uhi.py | 9 ++------- 10 files changed, 31 insertions(+), 62 deletions(-) diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml index 567958e..7c71daa 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/cd.yml @@ -11,7 +11,7 @@ on: jobs: dist: - runs-on: ubuntu-22.04 + runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 with: @@ -29,6 +29,7 @@ jobs: url: https://pypi.org/p/uhi permissions: id-token: write + attestations: write steps: - uses: actions/download-artifact@v4 @@ -36,4 +37,10 @@ jobs: name: Packages path: dist + - uses: actions/attest-build-provenance@v1 + with: + subject-path: "dist/*" + - uses: pypa/gh-action-pypi-publish@release/v1 + with: + attestations: true diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fa8b43b..1879a0d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,16 +18,11 @@ jobs: strategy: fail-fast: false matrix: - python-version: ["3.7", "3.12"] - runs-on: [ubuntu-latest, macos-13, windows-latest] + python-version: ["3.8", "3.13"] + runs-on: [ubuntu-latest, macos-latest, windows-latest] include: - - python-version: pypy-3.9 - runs-on: ubuntu-latest - - python-version: "3.7" - runs-on: ubuntu-20.04 - exclude: - - python-version: "3.7" + - python-version: pypy-3.10 runs-on: ubuntu-latest steps: @@ -38,6 +33,7 @@ jobs: - uses: actions/setup-python@v5 with: python-version: ${{ matrix.python-version }} + allow-prereleases: true - name: Install package run: python -m pip install .[test,schema] @@ -59,7 +55,6 @@ jobs: - uses: conda-incubator/setup-miniconda@v3 with: - miniforge-variant: Mambaforge use-mamba: true environment-file: environment.yml diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 12b33c2..f1d68b5 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -2,11 +2,6 @@ ci: autoupdate_commit_msg: 'chore: update pre-commit hooks' repos: - - repo: https://github.com/psf/black-pre-commit-mirror - rev: 24.8.0 - hooks: - - id: black - - repo: https://github.com/pre-commit/pre-commit-hooks rev: v4.6.0 hooks: @@ -26,6 +21,7 @@ repos: hooks: - id: ruff args: ["--fix", "--show-fixes"] + - id: ruff-format - repo: https://github.com/pre-commit/mirrors-mypy rev: v1.11.2 @@ -34,9 +30,8 @@ repos: files: ^(src|tests) args: [] additional_dependencies: - - numpy~=1.26.0 + - numpy - pytest - - importlib_metadata - importlib_resources - repo: https://github.com/pre-commit/pygrep-hooks @@ -60,8 +55,8 @@ repos: entry: PyBind|Numpy|Cmake|CCache|Github|PyTest exclude: .pre-commit-config.yaml - - repo: https://github.com/pre-commit/mirrors-prettier - rev: "v4.0.0-alpha.8" + - repo: https://github.com/rbubley/mirrors-prettier + rev: "v3.3.3" hooks: - id: prettier types_or: [json] diff --git a/README.md b/README.md index 8a7c164..65de594 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ It is not usually a runtime dependency, but only a type checking, testing, and/or docs dependency in support of other libraries (such as [boost-histogram][] 0.13+, [hist][] 2.1+, [mplhep][] 0.2.15+, [uproot][] 4+, and [histoprint][] 2+). There are a few useful runtime usable components -(listed below). It requires Python 3.6+. [See what's +(listed below). Older versions are available for Python 3.6+. [See what's new](https://github.com/scikit-hep/uhi/releases). To assist plotting libraries in accepting Histograms from classic sources, see diff --git a/docs/conf.py b/docs/conf.py index 9b99e87..d42ef97 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -6,14 +6,8 @@ from __future__ import annotations +import importlib.metadata import os -import sys - -if sys.version_info < (3, 8): - import importlib_metadata as metadata -else: - import importlib.metadata as metadata - # -- Project information ----------------------------------------------------- @@ -22,7 +16,7 @@ author = "Henry Schreiner, Hans Dembinski, Jim Pivarski" # The full version, including alpha/beta/rc tags -version = release = metadata.version("uhi") +version = release = importlib.metadata.version("uhi") # -- General configuration --------------------------------------------------- diff --git a/pyproject.toml b/pyproject.toml index 28173b2..7f68114 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -13,12 +13,11 @@ authors = [ maintainers = [ {name="The Scikit-HEP admins", email="scikit-hep-admins@googlegroups.com"}, ] -requires-python = ">=3.7" +requires-python = ">=3.8" readme = "README.md" license = {file="LICENSE"} dependencies = [ "numpy>=1.13.3", - "typing-extensions>=3.7; python_version<'3.8'", ] classifiers = [ "Development Status :: 4 - Beta", @@ -26,12 +25,12 @@ classifiers = [ "Intended Audience :: Science/Research", "License :: OSI Approved :: BSD License", "Operating System :: OS Independent", - "Programming Language :: Python :: 3.7", "Programming Language :: Python :: 3.8", "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", "Programming Language :: Python :: 3.11", "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: 3.13", "Topic :: Scientific/Engineering", "Typing :: Typed", ] @@ -58,7 +57,6 @@ docs = [ ] test = [ "pytest>=6", - "importlib-metadata>=1.0; python_version<'3.8'", "boost-histogram>=1.0", ] @@ -70,7 +68,6 @@ build.hooks.vcs.version-file = "src/uhi/_version.py" [tool.pdm.dev-dependencies] dev = [ "pytest>=6", - "importlib-metadata>=1.0; python_version<'3.8'", "boost-histogram>=1.0", ] @@ -97,7 +94,7 @@ enable_error_code = ["ignore-without-code", "redundant-expr", "truthy-bool"] module = ["fastjsonschema"] ignore_missing_imports = true -[tool.ruff] +[tool.ruff.lint] select = [ "E", "F", "W", # flake8 "B", # flake8-bugbear @@ -128,18 +125,12 @@ extend-ignore = [ "E501", # Line too long "PT004", # Use underscore for non-returning fixture (use usefixture instead) "PT013", # Incorrect import of pytest + "ISC001", # Conflicts with formatter ] -typing-modules = ["mypackage._compat.typing"] -src = ["src"] -unfixable = [ - "T20", # Removes print statements - "F841", # Removes unused variables -] -exclude = [] flake8-unused-arguments.ignore-variadic-names = true isort.required-imports = ["from __future__ import annotations"] -[tool.ruff.per-file-ignores] +[tool.ruff.lint.per-file-ignores] "tests/**" = ["T20"] "noxfile.py" = ["T20"] "tests/test_ensure.py" = ["NPY002"] diff --git a/src/uhi/numpy_plottable.py b/src/uhi/numpy_plottable.py index 988f53a..05bcac4 100644 --- a/src/uhi/numpy_plottable.py +++ b/src/uhi/numpy_plottable.py @@ -81,7 +81,7 @@ def __len__(self) -> int: Return the number of bins (not counting flow bins, which are ignored for this Protocol currently). """ - return self.edges.shape[0] + return self.edges.shape[0] # type: ignore[no-any-return] def __eq__(self, other: Any) -> bool: """ @@ -140,7 +140,7 @@ def __init__( self.kind = kind self.axes: Sequence[PlottableAxis] = [ - _bin_helper(shape, b) for shape, b in zip(hist.shape, bins) # type: ignore[arg-type] + _bin_helper(shape, b) for shape, b in zip(hist.shape, bins) ] def __repr__(self) -> str: diff --git a/src/uhi/typing/plottable.py b/src/uhi/typing/plottable.py index 967f570..31c183a 100644 --- a/src/uhi/typing/plottable.py +++ b/src/uhi/typing/plottable.py @@ -11,20 +11,12 @@ from __future__ import annotations -import sys from collections.abc import Iterator, Sequence -from typing import Any, Tuple, TypeVar, Union +from typing import Any, Protocol, Tuple, TypeVar, Union, runtime_checkable # NumPy 1.20+ will work much, much better than previous versions when type checking import numpy as np -if sys.version_info < (3, 8): - from typing_extensions import Protocol, runtime_checkable - -else: - from typing import Protocol, runtime_checkable - - protocol_version = (1, 2) # Known kinds of histograms. A Producer can add Kinds not defined here; a diff --git a/tests/test_root.py b/tests/test_root.py index b80729c..45f2435 100644 --- a/tests/test_root.py +++ b/tests/test_root.py @@ -20,7 +20,7 @@ def test_root_th1f_convert() -> None: assert all(th.GetBinContent(i + 1) == approx(iv) for i, iv in enumerate(h.values())) assert all( th.GetBinError(i + 1) == approx(ie) - for i, ie in enumerate(np.sqrt(h.variances())) # type: ignore[arg-type] + for i, ie in enumerate(np.sqrt(h.variances())) ) @@ -36,6 +36,6 @@ def test_root_th2f_convert() -> None: ) assert all( th.GetBinError(i + 1, j + 1) == approx(ie) - for i, row in enumerate(np.sqrt(h.variances())) # type: ignore[arg-type] + for i, row in enumerate(np.sqrt(h.variances())) for j, ie in enumerate(row) ) diff --git a/tests/test_uhi.py b/tests/test_uhi.py index 67911db..361c004 100644 --- a/tests/test_uhi.py +++ b/tests/test_uhi.py @@ -1,14 +1,9 @@ from __future__ import annotations -import sys +import importlib.metadata from uhi import __version__ -if sys.version_info < (3, 8): - import importlib_metadata as metadata -else: - from importlib import metadata - def test_version() -> None: - assert __version__ == metadata.version("uhi") + assert __version__ == importlib.metadata.version("uhi")