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

DX: switch to Ruff formatter #398

Merged
merged 4 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
8 changes: 4 additions & 4 deletions src/ampform/helicity/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,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 @@ -229,7 +229,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
2 changes: 1 addition & 1 deletion src/ampform/helicity/align/axisangle.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,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
8 changes: 3 additions & 5 deletions src/ampform/helicity/align/dpd.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,9 @@ def _(obj: ReactionInfo) -> ReactionInfo: # type: ignore[misc]

@relabel_edge_ids.register(StateTransitionCollection)
def _(obj: StateTransitionCollection) -> StateTransitionCollection: # type: ignore[misc]
return StateTransitionCollection(
[ # no attrs.evolve() for __attrs_post_init__()
relabel_edge_ids(transition) for transition in obj.transitions
]
)
return StateTransitionCollection([ # no attrs.evolve() for __attrs_post_init__()
relabel_edge_ids(transition) for transition in obj.transitions
])


@relabel_edge_ids.register(StateTransition)
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 @@ -129,9 +129,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
Loading
Loading