Skip to content

Commit

Permalink
chore: update pre-commit hooks (#434)
Browse files Browse the repository at this point in the history
* chore: update pre-commit hooks

updates:
- [github.com/astral-sh/ruff-pre-commit: v0.4.1 → v0.4.2](astral-sh/ruff-pre-commit@v0.4.1...v0.4.2)
- [github.com/pre-commit/mirrors-mypy: v1.9.0 → v1.10.0](pre-commit/mirrors-mypy@v1.9.0...v1.10.0)

* fix: improve typing

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

---------

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Henry Schreiner <henryschreineriii@gmail.com>
  • Loading branch information
pre-commit-ci[bot] and henryiii committed Apr 30, 2024
1 parent 721896c commit cb0a861
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 23 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ repos:
- id: end-of-file-fixer

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: "v0.4.1"
rev: "v0.4.2"
hooks:
- id: ruff
args: ["--fix", "--show-fixes"]
Expand All @@ -28,7 +28,7 @@ repos:
types_or: [python, pyi, jupyter]

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.9.0
rev: v1.10.0
hooks:
- id: mypy
files: '^src/decaylanguage/(decay|dec|utils)/'
Expand Down
3 changes: 1 addition & 2 deletions notebooks/ExampleDecFileParsingWithLark.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,7 @@
" if tree.data == \"decay\":\n",
" if tree.children[0].children[0].value in decays:\n",
" print(\n",
" \"Decays of particle %s are redefined! Please check your .dec file.\"\n",
" % tree.children[0].children[0].value\n",
" f\"Decays of particle {tree.children[0].children[0].value} are redefined! Please check your .dec file.\"\n",
" )\n",
" decays[tree.children[0].children[0].value] = []\n",
" for decay_mode in tree.find_data(\"decayline\"):\n",
Expand Down
2 changes: 1 addition & 1 deletion src/decaylanguage/dec/dec.py
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,7 @@ def _find_decay_modes(self, mother: str) -> tuple[Any, ...]:
if get_decay_mother_name(decay_Tree) == mother:
return tuple(decay_Tree.find_data("decayline"))

raise DecayNotFound("Decays of particle '%s' not found in .dec file!" % mother)
raise DecayNotFound(f"Decays of particle '{mother}' not found in .dec file!")

def list_decay_modes(self, mother: str, pdg_name: bool = False) -> list[list[str]]:
"""
Expand Down
42 changes: 24 additions & 18 deletions src/decaylanguage/decay/decay.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

from __future__ import annotations

import typing
from collections import Counter
from collections.abc import Sequence
from collections.abc import Collection, Sequence
from copy import deepcopy
from itertools import product
from typing import TYPE_CHECKING, Any, Dict, Iterable, Iterator, List, TypedDict
Expand Down Expand Up @@ -55,7 +56,7 @@ class DaughtersDict(CounterStr):

def __init__(
self,
iterable: dict[str, int] | list[str] | tuple[str] | str | None = None,
iterable: dict[str, int] | Collection[str] | str | None = None,
**kwds: Any,
) -> None:
"""
Expand Down Expand Up @@ -569,6 +570,23 @@ def _build_decay_modes(
decay_modes[mother] = DecayMode.from_dict(d)


T = typing.TypeVar("T")


def _get_modes(decay_chain: dict[str, T]) -> T:
# The list of decay modes is the first (and only) value of the dict
assert len(decay_chain.values()) == 1
modes = list(decay_chain.values())
return modes[0]


def _get_fs(decay: DecayModeDict) -> list[Any]:
fs = decay["fs"]
if isinstance(fs, list):
return fs
raise TypeError(f"Expected list, not {type(fs)}")


def _expand_decay_modes(
decay_chain: DecayChainDict,
*,
Expand Down Expand Up @@ -680,18 +698,6 @@ def _expand_decay_modes(
}
"""

def _get_modes(decay_chain: DecayChainDict) -> list[DecayModeDict]:
# The list of decay modes is the first (and only) value of the dict
assert len(decay_chain.values()) == 1
modes = list(decay_chain.values())
return modes[0]

def _get_fs(decay: DecayModeDict) -> list[Any]:
fs = decay["fs"]
if isinstance(fs, list):
return fs
raise TypeError(f"Expected list, not {type(fs)}")

# The mother particle is the first (and only) key of the dict
assert len(decay_chain.keys()) == 1
orig_mother = next(iter(decay_chain.keys()))
Expand All @@ -705,16 +711,16 @@ def _get_fs(decay: DecayModeDict) -> list[Any]:
# Replace dicts with strings (decay descriptors)
expanded_modes = []
for mode in _get_modes(decay_chain):
fsp_options = []
fsp_options: list[list[str]] = []
for fsp in _get_fs(mode):
if isinstance(fsp, dict):
fsp_options += [_get_modes(fsp)]
fsp_options.append(_get_modes(fsp))
elif isinstance(fsp, str):
fsp_options += [[fsp]] # type: ignore[list-item]
fsp_options.append([fsp])
for expanded_mode in product(*fsp_options):
# TODO: delegate descriptor-building to another function
# allow for different conventions?
final_state = DaughtersDict(list(expanded_mode)).to_string()
final_state = DaughtersDict(expanded_mode).to_string()
descriptor = DescriptorFormat.format_descriptor(mother, final_state, top)
expanded_modes += [descriptor]

Expand Down

0 comments on commit cb0a861

Please sign in to comment.