Skip to content

Commit

Permalink
Merge pull request #515 from ubermag/ruff-pre-commit
Browse files Browse the repository at this point in the history
Use ruff for pre-commits
  • Loading branch information
lang-m authored Jan 16, 2024
2 parents 30bf3c7 + efac6ee commit f1b4cda
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 49 deletions.
30 changes: 11 additions & 19 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
default_language_version:
python: python3.8

exclude: 'dev'

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
Expand All @@ -12,26 +14,16 @@ repos:
# args: [--markdown-linebreak-ext=md]
# exclude: 'discretisedfield/tests/test_sample/.*'

- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort

- repo: https://github.com/nbQA-dev/nbQA
rev: 1.7.0
hooks:
- id: nbqa-isort # isort inside Jupyter notebooks

- repo: https://github.com/PyCQA/flake8
rev: 6.1.0
hooks:
- id: flake8
additional_dependencies: [flake8-rst-docstrings] #, flake8-docstrings]

- repo: https://github.com/psf/black
rev: 23.9.1
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.11
hooks:
- id: black-jupyter
# Run the linter.
- id: ruff
types_or: [ python, pyi, jupyter ]
args: [ --fix ]
# Run the formatter.
- id: ruff-format
types_or: [ python, pyi, jupyter ]

# - repo: https://github.com/codespell-project/codespell
# rev: v2.1.0
Expand Down
17 changes: 8 additions & 9 deletions discretisedfield/__init__.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
"""Finite-difference fields."""
import importlib.metadata
import os
import pathlib

import matplotlib.pyplot as plt
import pytest

from . import tools
from .field import Field
from .field_rotator import FieldRotator
from .interact import interact
from .line import Line
from .mesh import Mesh
from .operators import integrate
from .region import Region
from . import tools # noqa: F401
from .field import Field # noqa: F401
from .field_rotator import FieldRotator # noqa: F401
from .interact import interact # noqa: F401
from .line import Line # noqa: F401
from .mesh import Mesh # noqa: F401
from .operators import integrate # noqa: F401
from .region import Region # noqa: F401

# Enable default plotting style.
plt.style.use(pathlib.Path(__file__).parent / "plotting" / "plotting-style.mplstyle")
Expand Down
1 change: 0 additions & 1 deletion discretisedfield/html/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
import re

import jinja2
Expand Down
1 change: 1 addition & 0 deletions discretisedfield/plotting/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Matplotlib based plotting."""
# ruff: noqa: F401
from discretisedfield.plotting import util
from discretisedfield.plotting.hv import Hv
from discretisedfield.plotting.k3d_field import K3dField
Expand Down
1 change: 1 addition & 0 deletions discretisedfield/region.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class Region(_RegionIO):
ValueError: ...
"""

__slots__ = ["_pmin", "_pmax", "_dims", "_units", "_tolerance_factor"]

def __init__(
Expand Down
6 changes: 3 additions & 3 deletions discretisedfield/tests/test_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ def test_field():
# else:
# return 0
def norm(points):
return np.where(
(points[..., 0] ** 2 + points[..., 1] ** 2) <= 5e-9**2, 1e5, 0
)[..., np.newaxis]
return np.where((points[..., 0] ** 2 + points[..., 1] ** 2) <= 5e-9**2, 1e5, 0)[
..., np.newaxis
]

# Values are defined in numpy for performance reasons
# We define vector fields with vx=0, vy=0, vz=+/-1 for x<0 / x>0
Expand Down
1 change: 1 addition & 0 deletions discretisedfield/tools/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Convenience tools"""
# ruff: noqa: F401
from .tools import (
count_bps,
count_large_cell_angle_regions,
Expand Down
1 change: 1 addition & 0 deletions discretisedfield/util/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# ruff: noqa: F401
from .util import array2tuple, assemble_index, bergluescher_angle, rescale_xarray
4 changes: 3 additions & 1 deletion docs/field-matplotlib-visualisation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1729,7 +1729,9 @@
"cell = (1e-9, 1e-9, 1e-9)\n",
"mesh = df.Mesh(p1=p1, p2=p2, cell=cell)\n",
"\n",
"value_fun = lambda pos: (pos[0], pos[1], pos[2])\n",
"\n",
"def value_fun(pos):\n",
" return (pos[0], pos[1], pos[2])\n",
"\n",
"\n",
"def norm_fun(pos):\n",
Expand Down
11 changes: 1 addition & 10 deletions docs/field-operations.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2533,15 +2533,6 @@
"numpy universal functions can be applied to `discretisedfield.Field` objects. Below we show a different examples. For available functions please refer to the `numpy` [documentation](https://numpy.org/doc/stable/reference/ufuncs.html#available-ufuncs)."
]
},
{
"cell_type": "code",
"execution_count": 74,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 75,
Expand Down Expand Up @@ -2697,7 +2688,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.17"
"version": "3.10.12"
},
"widgets": {
"application/vnd.jupyter.widget-state+json": {
Expand Down
11 changes: 5 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,15 @@ repository = "https://github.com/ubermag/discretisedfield"
[project.scripts]
ovf2vtk = "discretisedfield.io.ovf2vtk:ovf2vtk"

[tool.black]
experimental-string-processing = true
[tool.ruff]
ignore-init-module-imports = true # do not remove unused imports in __init__ and warn instead

[tool.ruff.per-file-ignores]
"*.ipynb" = ["F811"] # 'redefined-while-unused'; many false positives in notebooks because ipywidgets decorated functions are not recognised

[tool.coverage.run]
omit = ["discretisedfield/tests/*"]

[tool.isort]
profile = "black"
skip_gitignore = true # ignores files listed in .gitignore

[tool.setuptools.packages.find]
include = ["discretisedfield*"]

Expand Down

0 comments on commit f1b4cda

Please sign in to comment.