From 27ca5b0b12aa22a8f938bf34d115d8c8256013c2 Mon Sep 17 00:00:00 2001 From: Stuart Mumford Date: Mon, 11 Nov 2024 19:02:34 +0000 Subject: [PATCH] Updates from package template (#180) --- .cruft.json | 5 +- .github/workflows/label_sync.yml | 23 +++++++ .github/workflows/sub_package_update.yml | 48 ++++++++----- .pre-commit-config.yaml | 2 +- .ruff.toml | 72 ++++++++++++++++---- benchmarks/benchmark.py | 1 - benchmarks/benchmark_plot.py | 3 +- docs/conf.py | 4 +- pyproject.toml | 2 +- python/streamtracer/streamline.py | 3 +- python/streamtracer/tests/__init__.py | 0 python/streamtracer/tests/test_streamline.py | 14 ++-- 12 files changed, 130 insertions(+), 47 deletions(-) create mode 100644 .github/workflows/label_sync.yml create mode 100644 python/streamtracer/tests/__init__.py diff --git a/.cruft.json b/.cruft.json index 6994b04..c75f422 100644 --- a/.cruft.json +++ b/.cruft.json @@ -1,6 +1,6 @@ { "template": "https://github.com/sunpy/package-template", - "commit": "aec53b81aed2e7e534045e59303d82712fe82fb1", + "commit": "75f84c4adf1753af67967930c3335bc73bca9bf5", "checkout": null, "context": { "cookiecutter": { @@ -16,7 +16,8 @@ "enable_dynamic_dev_versions": "n", "include_example_code": "n", "include_cruft_update_github_workflow": "y", - "_sphinx_theme": "alabaster", + "use_extended_ruff_linting": "y", + "_sphinx_theme": "sunpy", "_parent_project": "", "_install_requires": "", "_copy_without_render": [ diff --git a/.github/workflows/label_sync.yml b/.github/workflows/label_sync.yml new file mode 100644 index 0000000..7f21775 --- /dev/null +++ b/.github/workflows/label_sync.yml @@ -0,0 +1,23 @@ +name: Label Sync +on: + workflow_dispatch: + schedule: + # ┌───────── minute (0 - 59) + # │ ┌───────── hour (0 - 23) + # │ │ ┌───────── day of the month (1 - 31) + # │ │ │ ┌───────── month (1 - 12 or JAN-DEC) + # │ │ │ │ ┌───────── day of the week (0 - 6 or SUN-SAT) + - cron: '0 0 * * *' # run every day at midnight UTC + +# Give permissions to write issue labels +permissions: + issues: write + +jobs: + label_sync: + runs-on: ubuntu-latest + name: Label Sync + steps: + - uses: srealmoreno/label-sync-action@850ba5cef2b25e56c6c420c4feed0319294682fd + with: + config-file: https://raw.githubusercontent.com/sunpy/.github/main/labels.yml diff --git a/.github/workflows/sub_package_update.yml b/.github/workflows/sub_package_update.yml index 7455847..0b657f2 100644 --- a/.github/workflows/sub_package_update.yml +++ b/.github/workflows/sub_package_update.yml @@ -21,14 +21,6 @@ jobs: runs-on: ubuntu-latest strategy: fail-fast: true - matrix: - include: - - add-paths: . - body: apply the changes to this repo. - branch: cruft/update - commit-message: "Automatic package template update" - title: Updates from the package template - steps: - uses: actions/checkout@v4 @@ -55,25 +47,47 @@ jobs: echo "has_changes=$CHANGES" >> "$GITHUB_OUTPUT" - name: Run update if available + id: cruft_update if: steps.check.outputs.has_changes == '1' run: | git config --global user.email "${{ github.actor }}@users.noreply.github.com" git config --global user.name "${{ github.actor }}" - cruft update --skip-apply-ask --refresh-private-variables + cruft_output=$(cruft update --skip-apply-ask --refresh-private-variables) + echo $cruft_output git restore --staged . - - name: Create pull request + if [[ "$cruft_output" == *"Failed to cleanly apply the update, there may be merge conflicts."* ]]; then + echo merge_conflicts=1 >> $GITHUB_OUTPUT + else + echo merge_conflicts=0 >> $GITHUB_OUTPUT + fi + + - name: Check if only .cruft.json is modified + id: cruft_json if: steps.check.outputs.has_changes == '1' + run: | + git status --porcelain=1 + if [[ "$(git status --porcelain=1)" == " M .cruft.json" ]]; then + echo "Only .cruft.json is modified. Exiting workflow early." + echo "has_changes=0" >> "$GITHUB_OUTPUT" + else + echo "has_changes=1" >> "$GITHUB_OUTPUT" + fi + + - name: Create pull request + if: steps.cruft_json.outputs.has_changes == '1' uses: peter-evans/create-pull-request@v7 with: token: ${{ secrets.GITHUB_TOKEN }} - add-paths: ${{ matrix.add-paths }} - commit-message: ${{ matrix.commit-message }} - branch: ${{ matrix.branch }} + add-paths: "." + commit-message: "Automatic package template update" + branch: "cruft/update" delete-branch: true - branch-suffix: timestamp - title: ${{ matrix.title }} + draft: ${{ steps.cruft_update.outputs.merge_conflicts == '1' }} + title: "Updates from the package template" body: | - This is an autogenerated PR, which will ${{ matrix.body }}. - [Cruft](https://cruft.github.io/cruft/) has detected updates from the Package Template + This is an autogenerated PR, which will applies the latest changes from the [SunPy Package Template](https://github.com/sunpy/package-template). + If this pull request has been opened as a draft there are conflicts which need fixing. + + **To run the CI on this pull request you will need to close it and reopen it.** diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 805a74f..524879e 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,7 +1,7 @@ repos: # This should be before any formatting hooks like isort - repo: https://github.com/astral-sh/ruff-pre-commit - rev: "v0.6.9" + rev: "v0.7.2" hooks: - id: ruff args: ["--fix"] diff --git a/.ruff.toml b/.ruff.toml index 38476e5..13adf41 100644 --- a/.ruff.toml +++ b/.ruff.toml @@ -1,5 +1,5 @@ target-version = "py310" -line-length = 110 +line-length = 120 exclude = [ ".git,", "__pycache__", @@ -8,30 +8,78 @@ exclude = [ ] [lint] -select = ["E", "F", "W", "UP", "PT"] +select = [ + "E", + "F", + "W", + "UP", + "PT", + "BLE", + "A", + "C4", + "INP", + "PIE", + "T20", + "RET", + "TID", + "PTH", + "PD", + "PLC", + "PLE", + "FLY", + "NPY", + "PERF", + "RUF", +] extend-ignore = [ # pycodestyle (E, W) - "E501", # LineTooLong # TODO! fix + "E501", # ignore line length will use a formatter instead + # pyupgrade (UP) + "UP038", # Use | in isinstance - not compatible with models and is slower # pytest (PT) "PT001", # Always use pytest.fixture() "PT004", # Fixtures which don't return anything should have leading _ - "PT007", # Parametrize should be lists of tuples # TODO! fix - "PT011", # Too broad exception assert # TODO! fix "PT023", # Always use () on pytest decorators + # flake8-pie (PIE) + "PIE808", # Disallow passing 0 as the first argument to range + # flake8-use-pathlib (PTH) + "PTH123", # open() should be replaced by Path.open() + # Ruff (RUF) + "RUF003", # Ignore ambiguous quote marks, doesn't allow ' in comments + "RUF012", # Mutable class attributes should be annotated with `typing.ClassVar` + "RUF013", # PEP 484 prohibits implicit `Optional` + "RUF015", # Prefer `next(iter(...))` over single element slice ] [lint.per-file-ignores] -# Part of configuration, not a package. -"setup.py" = ["INP001"] -"conftest.py" = ["INP001"] +"setup.py" = [ + "INP001", # File is part of an implicit namespace package. +] +"conftest.py" = [ + "INP001", # File is part of an implicit namespace package. +] "docs/conf.py" = [ - "E402" # Module imports not at top of file + "E402" # Module imports not at top of file ] "docs/*.py" = [ - "INP001", # Implicit-namespace-package. The examples are not a package. + "INP001", # File is part of an implicit namespace package. +] +"examples/**.py" = [ + "T201", # allow use of print in examples + "INP001", # File is part of an implicit namespace package. +] +"__init__.py" = [ + "E402", # Module level import not at top of cell + "F401", # Unused import + "F403", # from {name} import * used; unable to detect undefined names + "F405", # {name} may be undefined, or defined from star imports +] +"test_*.py" = [ + "E402", # Module level import not at top of cell +] +"benchmarks/*.py" = [ + "NPY002", # TODO: Generator API ] -"__init__.py" = ["E402", "F401", "F403"] -"test_*.py" = ["B011", "D", "E402", "PGH001", "S101"] [lint.pydocstyle] convention = "numpy" diff --git a/benchmarks/benchmark.py b/benchmarks/benchmark.py index de4b7b3..f605ea4 100644 --- a/benchmarks/benchmark.py +++ b/benchmarks/benchmark.py @@ -30,7 +30,6 @@ dts.append(time.time() - t) assert len(tracer.xs) == nseeds times += [np.mean(dts)] - print(nseeds, times[-1] / nseeds, times[-1]) pd.DataFrame({"nseeds": seedlist, "time": times}).to_csv( diff --git a/benchmarks/benchmark_plot.py b/benchmarks/benchmark_plot.py index a6eebd4..ae41dbf 100644 --- a/benchmarks/benchmark_plot.py +++ b/benchmarks/benchmark_plot.py @@ -11,12 +11,11 @@ "210dev0": "v2.1 (Rust Parallel)", } -files = Path(".").glob("v*.csv") +files = Path().glob("v*.csv") for file in files: label = version_names.get(file.stem[1:], file.stem) data = pd.read_csv(file) - print(data) ax.plot(data["nseeds"], data["time"], label=label, marker="o") diff --git a/docs/conf.py b/docs/conf.py index 2c46576..7e44288 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -44,7 +44,7 @@ ] # Add any paths that contain templates here, relative to this directory. -# templates_path = ["_templates"] # NOQA: ERA001 +# templates_path = ["_templates"] # List of patterns, relative to source directory, that match files and # directories to ignore when looking for source files. @@ -87,7 +87,7 @@ # Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, # so a file named "default.css" will overwrite the builtin "default.css". -# html_static_path = ["_static"] # NOQA: ERA001 +# html_static_path = ["_static"] # By default, when rendering docstrings for classes, sphinx.ext.autodoc will # make docs with the class-level docstring and the class-method docstrings, diff --git a/pyproject.toml b/pyproject.toml index 106a998..22c7006 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -45,9 +45,9 @@ tests = [ docs = [ "sphinx", "sphinx-automodapi", + "sunpy-sphinx-theme", "packaging", "jupyter-sphinx", - "sunpy-sphinx-theme", "sphinx-changelog", ] diff --git a/python/streamtracer/streamline.py b/python/streamtracer/streamline.py index fea6255..ddac445 100755 --- a/python/streamtracer/streamline.py +++ b/python/streamtracer/streamline.py @@ -172,8 +172,7 @@ def _get_coords(self, i): self.grid_spacing[i] * np.arange(self.vectors.shape[i]) + self.origin_coord[i] ) - else: - return self.coords[i] + return self.coords[i] @property def xcoords(self): diff --git a/python/streamtracer/tests/__init__.py b/python/streamtracer/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/python/streamtracer/tests/test_streamline.py b/python/streamtracer/tests/test_streamline.py index 41b0dff..28abb4f 100644 --- a/python/streamtracer/tests/test_streamline.py +++ b/python/streamtracer/tests/test_streamline.py @@ -22,9 +22,9 @@ def uniform_x_field(): @pytest.mark.parametrize( ("direction", "ROTs"), [ - [1, np.array([2, 2, 2])], - [-1, np.array([2, 2, 2])], - [0, np.array([[2, 2], [2, 2], [2, 2]])], + (1, np.array([2, 2, 2])), + (-1, np.array([2, 2, 2])), + (0, np.array([[2, 2], [2, 2], [2, 2]])), ], ) def test_rot(tracer, uniform_x_field, direction, ROTs): @@ -217,18 +217,18 @@ def test_invalid_max_steps(val, errstr): # Paramatrize to make sure behaviour is same in x,y,z directions -@pytest.mark.parametrize("dir", [0, 1, 2]) -def test_bounds(dir): +@pytest.mark.parametrize("dirs", [0, 1, 2]) +def test_bounds(dirs): v = np.zeros((3, 3, 3, 3)) # Make all vectors point along the specified dimension - v[:, :, :, dir] = 1 + v[:, :, :, dirs] = 1 spacing = [1, 1, 1] grid = VectorGrid(v, spacing) seed = np.array([[0.5, 0.5, 0.5]]) tracer = StreamTracer(max_steps=10, step_size=1.0) tracer.trace(seed, grid) - expected = np.roll(np.array([1.5, 0.5, 0.5]), dir) + expected = np.roll(np.array([1.5, 0.5, 0.5]), dirs) assert (tracer.xs[0][-1, :] == expected).all() expected = np.array([0.5, 0.5, 0.5]) assert (tracer.xs[0][0, :] == expected).all()