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

Update pre-commits and pytest configuration #87

Merged
merged 13 commits into from
Jun 9, 2024
36 changes: 11 additions & 25 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,37 +1,23 @@
default_language_version:
python: python3.8
exclude: 'dev'

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v4.5.0
hooks:
- id: check-merge-conflict # checks for files that contain merge conflict strings
- id: check-toml # checks toml files for parseable syntax
- id: debug-statements # checks for debugger imports and py37+ `breakpoint()` calls in python source
# - id: trailing-whitespace # needs more checks
# args: [--markdown-linebreak-ext=md]
# exclude: 'micromagneticmodel/tests/test_sample/.*'

- repo: https://github.com/pycqa/isort
rev: 5.12.0
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.4.4
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
hooks:
- id: black-jupyter
# Run the linter.
- id: ruff
types_or: [python, pyi, jupyter]
args: [--fix, --exit-non-zero-on-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
2 changes: 1 addition & 1 deletion micromagneticmodel/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"""Domain-specific language for computational magnetism."""

import importlib.metadata

import pytest

import micromagneticmodel.abstract
import micromagneticmodel.consts
import micromagneticmodel.examples

from .driver import Driver, ExternalDriver
from .dynamics import Damping, Dynamics, DynamicsTerm, Precession, Slonczewski, ZhangLi
from .energy import (
Expand Down
1 change: 1 addition & 0 deletions micromagneticmodel/abstract/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Abstract classes"""

from .abstract import Abstract
from .container import Container
from .term import Term
3 changes: 1 addition & 2 deletions micromagneticmodel/abstract/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ def __iter__(self):
Demag()

"""
for term in self._terms:
yield term
yield from self._terms

def __contains__(self, item):
"""Determine whether a term of the same type as ``item`` is in the
Expand Down
1 change: 0 additions & 1 deletion micromagneticmodel/abstract/term.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import abc

import micromagneticmodel as mm

from .abstract import Abstract


Expand Down
1 change: 1 addition & 0 deletions micromagneticmodel/consts.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Physical constants."""

import math

# Fundamental constants
Expand Down
6 changes: 3 additions & 3 deletions micromagneticmodel/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,12 +282,12 @@ def schedule(

def _write_schedule_script(self, system, header, script_name, runner):
if pathlib.Path(header).exists():
with open(header, "rt", encoding="utf-8") as f:
with open(header, encoding="utf-8") as f:
header = f.read()
else:
header = header
run_commands = self._schedule_commands(system=system, runner=runner)
with open(script_name, "wt", encoding="utf-8") as f:
with open(script_name, "w", encoding="utf-8") as f:
f.write(header)
f.write("\n")
f.write("\n".join(run_commands))
Expand All @@ -303,7 +303,7 @@ def _write_info_json(self, system, **kwargs):
info["adapter"] = self.__module__.split(".")[0]
for k, v in kwargs.items():
info[k] = v
with open("info.json", "wt", encoding="utf-8") as jsonfile:
with open("info.json", "w", encoding="utf-8") as jsonfile:
jsonfile.write(json.dumps(info))

@staticmethod
Expand Down
1 change: 1 addition & 0 deletions micromagneticmodel/dynamics/damping.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class Damping(DynamicsTerm):
ValueError: ...

"""

_allowed_attributes = ["alpha"]
_reprlatex = (
r"-\frac{\gamma_{0} \alpha}{1 + \alpha^{2}} \mathbf{m} "
Expand Down
1 change: 0 additions & 1 deletion micromagneticmodel/dynamics/dynamics.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import ubermagutil as uu

import micromagneticmodel as mm

from .dynamicsterm import DynamicsTerm


Expand Down
1 change: 1 addition & 0 deletions micromagneticmodel/dynamics/precession.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class Precession(DynamicsTerm):
ValueError: ...

"""

_allowed_attributes = ["gamma0"]
_reprlatex = (
r"-\frac{\gamma_{0}}{1 + \alpha^{2}} \mathbf{m} "
Expand Down
3 changes: 2 additions & 1 deletion micromagneticmodel/energy/cubicanisotropy.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,15 @@ class CubicAnisotropy(EnergyTerm):
ValueError: ...

"""

_allowed_attributes = ["K", "u1", "u2"]

@property
def _reprlatex(self):
a1 = r"(\mathbf{m} \cdot \mathbf{u}_{1})^{2}"
a2 = r"(\mathbf{m} \cdot \mathbf{u}_{2})^{2}"
a3 = r"(\mathbf{m} \cdot \mathbf{u}_{3})^{2}"
return r"-K [{0}{1}+{1}{2}+{2}{0}]".format(a1, a2, a3)
return rf"-K [{a1}{a2}+{a2}{a3}+{a3}{a1}]"

def effective_field(self, m):
raise NotImplementedError
1 change: 1 addition & 0 deletions micromagneticmodel/energy/demag.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Demag(EnergyTerm):
>>> demag = mm.Demag()

"""

_allowed_attributes = ["asymptotic_radius"]
_reprlatex = (
r"-\frac{1}{2}\mu_{0}M_\text{s}" r"\mathbf{m} \cdot \mathbf{H}_\text{d}"
Expand Down
6 changes: 2 additions & 4 deletions micromagneticmodel/energy/dmi.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,15 @@ class DMI(EnergyTerm):
TypeError: ...

"""

_allowed_attributes = ["D", "crystalclass"]

@property
def _reprlatex(self):
if self.crystalclass in ["T", "O"]:
return r"D \mathbf{m} \cdot (\nabla \times \mathbf{m})"
elif "Cnv" in self.crystalclass:
if self.crystalclass == "Cnv":
direction = "z"
else:
direction = self.crystalclass[-1]
direction = "z" if self.crystalclass == "Cnv" else self.crystalclass[-1]
return (
r"D ( \mathbf{m} \cdot \nabla m_{" + direction + r"} "
r"- m_{" + direction + r"} \nabla \cdot \mathbf{m} )"
Expand Down
1 change: 0 additions & 1 deletion micromagneticmodel/energy/energy.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import ubermagutil as uu

import micromagneticmodel as mm

from .energyterm import EnergyTerm


Expand Down
1 change: 1 addition & 0 deletions micromagneticmodel/energy/exchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class Exchange(EnergyTerm):
TypeError: ...

"""

_allowed_attributes = ["A"]
_reprlatex = r"- A \mathbf{m} \cdot \nabla^{2} \mathbf{m}"

Expand Down
1 change: 1 addition & 0 deletions micromagneticmodel/energy/magnetoelastic.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class MagnetoElastic(EnergyTerm):
ValueError: ...

"""

_allowed_attributes = ["B1", "B2", "e_diag", "e_offdiag"]
_reprlatex = (
r"B_{1}\sum_{i} m_{i}\epsilon_{ii} + "
Expand Down
1 change: 1 addition & 0 deletions micromagneticmodel/energy/uniaxialanisotropy.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class UniaxialAnisotropy(EnergyTerm):
ValueError: ...

"""

_allowed_attributes = ["K", "K1", "K2", "u"]

@property
Expand Down
1 change: 1 addition & 0 deletions micromagneticmodel/examples/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
"""Functions for creating examples"""

from .examples import macrospin
4 changes: 2 additions & 2 deletions micromagneticmodel/tests/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def check_term(term):
term.dmdt(m=None, Heff=None)

assert term == term
assert not term != term
assert term == term
assert term != "5"

container = getattr(mm, term._container_class)()
Expand Down Expand Up @@ -84,7 +84,7 @@ def check_container(container):
assert term.name in dir(container)

assert container == container
assert not container != container
assert container == container
assert container != "5"

# neutral element for addition
Expand Down
1 change: 0 additions & 1 deletion micromagneticmodel/tests/test_cubicanisotropy.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import pytest

import micromagneticmodel as mm

from .checks import check_term


Expand Down
1 change: 0 additions & 1 deletion micromagneticmodel/tests/test_damping.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import pytest

import micromagneticmodel as mm

from .checks import check_term


Expand Down
1 change: 0 additions & 1 deletion micromagneticmodel/tests/test_demag.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import pytest

import micromagneticmodel as mm

from .checks import check_term


Expand Down
1 change: 0 additions & 1 deletion micromagneticmodel/tests/test_dmi.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import pytest

import micromagneticmodel as mm

from .checks import check_term


Expand Down
4 changes: 2 additions & 2 deletions micromagneticmodel/tests/test_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ def _check_system(self, system):
pass

def _write_input_files(self, system, **kwargs):
with open(f"{system.name}.input", "wt", encoding="utf-8") as f:
with open(f"{system.name}.input", "w", encoding="utf-8") as f:
f.write(str(-1)) # factor -1 used to invert magnetisation direction in call
self._write_info_json(system, **kwargs)

def _call(self, system, runner, **kwargs):
with open(f"{system.name}.input", "rt", encoding="utf-8") as f:
with open(f"{system.name}.input", encoding="utf-8") as f:
factor = int(f.read())
(factor * system.m).to_file("output.omf")

Expand Down
3 changes: 1 addition & 2 deletions micromagneticmodel/tests/test_dynamics.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import pytest

import micromagneticmodel as mm

from .checks import check_container


Expand Down Expand Up @@ -107,7 +106,7 @@ def test_getattr(self):
container -= self.damping
check_container(container)
with pytest.raises(AttributeError):
container.damping
container.damping # noqa: B018

def test_freestyle(self):
container = self.damping + self.zhangli # single term is not allowed
Expand Down
3 changes: 1 addition & 2 deletions micromagneticmodel/tests/test_energy.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import pytest

import micromagneticmodel as mm

from .checks import check_container


Expand Down Expand Up @@ -142,7 +141,7 @@ def test_getattr(self):
container -= self.exchange
check_container(container)
with pytest.raises(AttributeError):
container.exchange
container.exchange # noqa: B018

def test_freestyle(self):
container = self.dmi + self.zeeman # single term is not allowed
Expand Down
1 change: 0 additions & 1 deletion micromagneticmodel/tests/test_exchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import pytest

import micromagneticmodel as mm

from .checks import check_term


Expand Down
1 change: 0 additions & 1 deletion micromagneticmodel/tests/test_magnetoelastic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import pytest

import micromagneticmodel as mm

from .checks import check_term


Expand Down
1 change: 0 additions & 1 deletion micromagneticmodel/tests/test_precession.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import pytest

import micromagneticmodel as mm

from .checks import check_term


Expand Down
1 change: 0 additions & 1 deletion micromagneticmodel/tests/test_rkky.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import pytest

import micromagneticmodel as mm

from .checks import check_term


Expand Down
4 changes: 2 additions & 2 deletions micromagneticmodel/tests/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ def _call(self, argstr, need_stderr=False, dry_run=False, returncode=0, **kwargs
return sp.CompletedProcess(
argstr,
returncode=returncode,
stdout="output".encode("utf-8"),
stderr="error".encode("utf-8"),
stdout=b"output",
stderr=b"error",
)


Expand Down
5 changes: 1 addition & 4 deletions micromagneticmodel/tests/test_slonczewski.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import pytest

import micromagneticmodel as mm

from .checks import check_term


Expand Down Expand Up @@ -70,9 +69,7 @@ def time_dep(t):
assert re.search(r"^Slonczewski\(J=.+\)$", repr(term))

tcl_strings = {}
tcl_strings[
"script"
] = """proc TimeFunction { total_time } {
tcl_strings["script"] = """proc TimeFunction { total_time } {
return $total_time/10
}"""
tcl_strings["script_args"] = "total_time"
Expand Down
1 change: 0 additions & 1 deletion micromagneticmodel/tests/test_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import pytest

import micromagneticmodel as mm

from .checks import check_system


Expand Down
1 change: 0 additions & 1 deletion micromagneticmodel/tests/test_uniaxialanisotropy.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import pytest

import micromagneticmodel as mm

from .checks import check_term


Expand Down
Loading
Loading