Skip to content

Commit

Permalink
DX: switch to Ruff formatter (#398)
Browse files Browse the repository at this point in the history
* DX: do not run notebooks in temp dirs
  • Loading branch information
redeboer committed Mar 1, 2024
1 parent 81b3909 commit dc92284
Show file tree
Hide file tree
Showing 15 changed files with 64 additions and 107 deletions.
1 change: 0 additions & 1 deletion .gitpod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ vscode:
- executablebookproject.myst-highlight
- github.vscode-github-actions
- github.vscode-pull-request-github
- ms-python.black-formatter
- ms-python.mypy-type-checker
- ms-python.python
- ms-python.vscode-pylance
Expand Down
31 changes: 10 additions & 21 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ repos:
metadata.vscode
- repo: https://github.com/ComPWA/policy
rev: 0.2.4
rev: 0.2.5
hooks:
- id: check-dev-files
args:
Expand All @@ -58,6 +58,15 @@ repos:
- --additional-packages=IPython
- --extras-require=doc,viz

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.2.1
hooks:
- id: ruff
args: [--fix]
types_or: [python, pyi, jupyter]
- id: ruff-format
types_or: [python, pyi, jupyter]

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
Expand All @@ -82,19 +91,6 @@ repos:
args: ["--django"]
- id: trailing-whitespace

- repo: https://github.com/psf/black-pre-commit-mirror
rev: 24.1.1
hooks:
- id: black
- id: black-jupyter
args: [--line-length=85]
types_or: [jupyter]

- repo: https://github.com/asottile/blacken-docs
rev: 1.16.0
hooks:
- id: blacken-docs

- repo: https://github.com/pre-commit/mirrors-prettier
rev: v4.0.0-alpha.8
hooks:
Expand Down Expand Up @@ -155,10 +151,3 @@ repos:
rev: v1.1.350
hooks:
- id: pyright

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.2.1
hooks:
- id: ruff
args: [--fix]
types_or: [python, pyi, jupyter]
2 changes: 1 addition & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"executablebookproject.myst-highlight",
"github.vscode-github-actions",
"github.vscode-pull-request-github",
"ms-python.black-formatter",
"ms-python.mypy-type-checker",
"ms-python.python",
"ms-python.vscode-pylance",
Expand All @@ -27,6 +26,7 @@
"bungcip.better-toml",
"davidanson.vscode-markdownlint",
"garaioag.garaio-vscode-unwanted-recommendations",
"ms-python.black-formatter",
"ms-python.flake8",
"ms-python.isort",
"ms-python.pylint",
Expand Down
6 changes: 4 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
"[bibtex]": {
"editor.formatOnSave": false
},
"[css]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[git-commit]": {
"editor.rulers": [72],
"rewrap.wrappingColumn": 72
Expand All @@ -19,13 +22,12 @@
"editor.codeActionsOnSave": {
"source.organizeImports": "explicit"
},
"editor.defaultFormatter": "ms-python.black-formatter",
"editor.defaultFormatter": "charliermarsh.ruff",
"editor.rulers": [88]
},
"[yaml]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"black-formatter.importStrategy": "fromEnvironment",
"coverage-gutters.coverageFileNames": ["coverage.xml"],
"coverage-gutters.coverageReportFileName": "**/htmlcov/index.html",
"coverage-gutters.showGutterCoverage": false,
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/70fc5fb0f3954a9d82d142efeff4df31)](https://www.codacy.com/gh/ComPWA/ampform)
[![Spelling checked](https://img.shields.io/badge/cspell-checked-brightgreen.svg)](https://github.com/streetsidesoftware/cspell/tree/master/packages/cspell)
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/charliermarsh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)

AmpForm is a Python library of spin formalisms and dynamics with which you can
Expand Down
4 changes: 0 additions & 4 deletions docs/_static/custom.css

This file was deleted.

23 changes: 11 additions & 12 deletions docs/usage/dynamics/k-matrix.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1488,13 +1488,11 @@
" if substitute_sqrt_rho:\n",
"\n",
" def rho_i(i):\n",
" return phsp_factor(\n",
" *sp.symbols(f\"s m_a{i} m_b{i}\", nonnegative=True)\n",
" ).doit()\n",
" return phsp_factor(*sp.symbols(f\"s m_a{i} m_b{i}\", nonnegative=True)).doit()\n",
"\n",
" expr = expr.xreplace(\n",
" {sp.sqrt(rho_i(i)): 1 for i in range(n_channels)}\n",
" ).xreplace({sp.conjugate(sp.sqrt(rho_i(i))): 1 for i in range(n_channels)})\n",
" expr = expr.xreplace({\n",
" sp.sqrt(rho_i(i)): 1 for i in range(n_channels)\n",
" }).xreplace({sp.conjugate(sp.sqrt(rho_i(i))): 1 for i in range(n_channels)})\n",
" expr = expr.xreplace({s: m**2})\n",
" expr = symplot.substitute_indexed_symbols(expr)\n",
" np_expr, sliders = symplot.prepare_sliders(expr, m)\n",
Expand Down Expand Up @@ -1581,9 +1579,7 @@
" kmatrix.NonRelativisticPVector,\n",
" kmatrix.RelativisticPVector,\n",
" }:\n",
" fig.suptitle(\n",
" f\"$P$-vector for {n_channels} channels and {n_poles} resonances\"\n",
" )\n",
" fig.suptitle(f\"$P$-vector for {n_channels} channels and {n_poles} resonances\")\n",
"\n",
" for ax in axes:\n",
" ax.set_xlim(x_min, x_max)\n",
Expand Down Expand Up @@ -1712,9 +1708,12 @@
" c=color,\n",
" rotation=+90,\n",
" )\n",
" threshold_indicators.append(\n",
" (line_thr, line_diff, text_thr, text_diff)\n",
" )\n",
" threshold_indicators.append((\n",
" line_thr,\n",
" line_diff,\n",
" text_thr,\n",
" text_diff,\n",
" ))\n",
" for i, (_, line_diff, _, text_diff) in enumerate(threshold_indicators):\n",
" m_a = kwargs[f\"m_a{i}\"]\n",
" m_b = kwargs[f\"m_b{i}\"]\n",
Expand Down
55 changes: 17 additions & 38 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ doc = [
"sphinx-togglebutton",
"sphinxcontrib-bibtex >=2",
]
format = ["black"]
jupyter = [
"black",
"isort",
"jupyterlab",
"jupyterlab-code-formatter",
Expand All @@ -97,24 +97,15 @@ jupyter = [
"python-lsp-ruff",
"python-lsp-server[rope]",
]
lint = [
"ampform[mypy]",
"ruff",
]
mypy = [
"mypy >=0.730",
"sphinx-api-relink >=0.0.3",
]
scipy = ["scipy"]
sty = [
"ampform[format]",
"ampform[lint]",
"ampform[test]", # for pytest type hints
"ampform[types]",
"mypy >=0.730",
"pre-commit >=1.4.0",
"ruff",
]
test = [
"ampform[scipy]",
"black",
"ipywidgets", # symplot
"nbmake",
"numpy",
Expand All @@ -123,6 +114,13 @@ test = [
"pytest-profiling",
"pytest-xdist",
]
types = [
"IPython",
"ipywidgets",
"numpy",
"pytest",
"sphinx-api-relink >=0.0.3",
]
viz = ["graphviz"]

[project.readme]
Expand Down Expand Up @@ -150,31 +148,6 @@ where = ["src"]
[tool.setuptools_scm]
write_to = "src/ampform/version.py"

[tool.black]
exclude = '''
/(
.*\.egg-info
| .*build
| \.eggs
| \.git
| \.pytest_cache
| \.tox
| \.venv
| \.vscode
| dist
)/
'''
include = '\.pyi?$'
preview = true
target-version = [
"py310",
"py311",
"py312",
"py37",
"py38",
"py39",
]

[tool.coverage.run]
branch = true
source = ["src"]
Expand Down Expand Up @@ -276,6 +249,7 @@ filterwarnings = [
markers = ["slow: marks tests as slow (select with '-m slow')"]
norecursedirs = [
".ipynb_checkpoints",
".virtual_documents",
"_build",
]
testpaths = [
Expand All @@ -294,6 +268,10 @@ src = [
]
target-version = "py37"

[tool.ruff.format]
docstring-code-format = true
line-ending = "lf"

[tool.ruff.lint]
extend-select = [
"A",
Expand Down Expand Up @@ -338,6 +316,7 @@ ignore = [
"D407",
"D416",
"E501",
"ISC001",
"PLR0913",
"PLW1514",
"PLW2901",
Expand Down
1 change: 1 addition & 0 deletions src/ampform/dynamics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class BlattWeisskopfSquared(sp.Expr):
See also :ref:`usage/dynamics:Form factor`.
"""

z: Any
angular_momentum: Any
_latex_repr_ = R"B_{{{angular_momentum}}}^2\left({z}\right)"
Expand Down
10 changes: 5 additions & 5 deletions src/ampform/helicity/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@
def _order_component_mapping(
mapping: Mapping[str, sp.Expr],
) -> OrderedDict[str, sp.Expr]:
return collections.OrderedDict(
[(key, mapping[key]) for key in sorted(mapping, key=natural_sorting)]
)
return collections.OrderedDict([
(key, mapping[key]) for key in sorted(mapping, key=natural_sorting)
])


def _order_symbol_mapping(
Expand Down Expand Up @@ -272,7 +272,7 @@ class ParameterValues(abc.Mapping):
dictionary.
>>> a, b, c = sp.symbols("a b c")
>>> parameters = ParameterValues({a: 0.0, b: 1+1j, c: -2})
>>> parameters = ParameterValues({a: 0.0, b: 1 + 1j, c: -2})
>>> parameters[a]
0.0
>>> parameters["b"]
Expand Down Expand Up @@ -1205,7 +1205,7 @@ def formulate_helicity_rotation(
>>> a, b, c, i = sp.symbols("a b c i")
>>> formulate_helicity_rotation(0, 0, i, a, b, c)
PoolSum(WignerD(0, 0, i, a, b, c), (i, (0,)))
>>> formulate_helicity_rotation(1/2, -1/2, i, a, b, c)
>>> formulate_helicity_rotation(1 / 2, -1 / 2, i, a, b, c)
PoolSum(WignerD(1/2, -1/2, i, a, b, c), (i, (-1/2, 1/2)))
"""
from sympy.physics.quantum.spin import Rotation as Wigner # noqa: PLC0415
Expand Down
4 changes: 1 addition & 3 deletions src/ampform/helicity/decay.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,7 @@ def is_opposite_helicity_state(topology: Topology, state_id: int) -> bool:
... sibling_id = get_sibling_state_id(topology, state_id)
... assert is_opposite_helicity_state(
... topology, state_id
... ) != is_opposite_helicity_state(
... topology, sibling_id
... )
... ) != is_opposite_helicity_state(topology, sibling_id)
The Wigner-:math:`D` function for a two-particle state treats one helicity with a
negative sign. This sign originates from Eq.(13) in
Expand Down
6 changes: 3 additions & 3 deletions src/ampform/kinematics/angles.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ def __recursive_helicity_angles(
sub_momenta_ids = determine_attached_final_state(topology, state_id)
if len(sub_momenta_ids) > 1:
# add all of these momenta together -> defines new subsystem
four_momentum = ArraySum(
*[four_momenta[i] for i in sub_momenta_ids]
)
four_momentum = ArraySum(*[
four_momenta[i] for i in sub_momenta_ids
])

# boost all of those momenta into this new subsystem
phi_expr = Phi(four_momentum)
Expand Down
10 changes: 3 additions & 7 deletions src/ampform/sympy/_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ class SymPyAssumptions(TypedDict, total=False):
def argument(*, default: T = MISSING, sympify: bool = True) -> T: ... # type: ignore[assignment]
@overload
def argument(
*, default_factory: Callable[[], T] = MISSING, sympify: bool = True # type: ignore[assignment]
*,
default_factory: Callable[[], T] = MISSING, # type: ignore[assignment]
sympify: bool = True,
) -> T: ...
def argument(
*,
Expand Down Expand Up @@ -125,7 +127,6 @@ def unevaluated(
... def evaluate(self) -> sp.Expr:
... x, y = self.args
... return x**2 + y**2
...
>>> a, b = sp.symbols("a b")
>>> expr = MyExpr(a, b**2)
>>> sp.latex(expr)
Expand All @@ -144,7 +145,6 @@ def unevaluated(
...
... def evaluate(self) -> sp.Expr:
... return sp.sqrt(self.x)
...
>>> y = sp.Symbol("y", nonnegative=True)
>>> expr = Function(x=y**2)
>>> sp.latex(expr)
Expand All @@ -161,7 +161,6 @@ def unevaluated(
>>> class Transformation:
... def __call__(self, x: sp.Basic, y: sp.Basic) -> sp.Expr: ...
...
>>> @unevaluated
... class MyExpr(sp.Expr):
... x: Any
Expand All @@ -170,7 +169,6 @@ def unevaluated(
...
... def evaluate(self) -> sp.Expr:
... return self.functor(self.x, self.y)
...
>>> expr = MyExpr(0, y=3.14, functor=Transformation)
>>> isinstance(expr.x, sp.Integer)
True
Expand Down Expand Up @@ -211,7 +209,6 @@ def _implement_new_method(cls: type[ExprClass]) -> type[ExprClass]:
... class MyExpr(sp.Expr):
... a: sp.Symbol
... b: sp.Symbol
...
>>> x, y = sp.symbols("x y")
>>> expr = MyExpr(x**2, y**2)
>>> expr.a
Expand Down Expand Up @@ -435,7 +432,6 @@ def _get_attribute_names(cls: type) -> tuple[str, ...]:
... n: ClassVar[int] = 2
...
... def print(self): ...
...
>>> _get_attribute_names(MyClass)
('a', 'b')
"""
Expand Down
Loading

0 comments on commit dc92284

Please sign in to comment.