Skip to content
This repository was archived by the owner on Nov 10, 2025. It is now read-only.

Commit ef4c82a

Browse files
apply updated ruff rules
1 parent 246d242 commit ef4c82a

16 files changed

Lines changed: 151 additions & 132 deletions

docs/examples/comparisons/arc_fixed.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
# ruff: noqa: INP001
2+
from __future__ import annotations
23

34
from typing import TYPE_CHECKING
45

5-
import arc
66
import numpy as np
77
import scipy.integrate
88

99
if TYPE_CHECKING:
10+
import arc
11+
1012
from ryd_numerov.units import NDArray
1113

1214

1315
def radialWavefunction( # noqa: N802
1416
atom: arc.AlkaliAtom, n: int, l: int, j: float, step: float = 1e-2, use_fixed_arc: bool = False
15-
) -> tuple["NDArray", "NDArray"]:
17+
) -> tuple[NDArray, NDArray]:
1618
n, l, j = int(n), int(l), float(j)
1719
hartree_energy = 27.211 if not use_fixed_arc else 27.211_386_245_988
1820
energy = atom.getEnergy(int(n), int(l), float(j)) / hartree_energy

src/ryd_numerov/elements/base_element.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1+
from __future__ import annotations
2+
13
import inspect
24
import logging
35
import re
46
from abc import ABC
57
from fractions import Fraction
68
from functools import cache, cached_property
7-
from pathlib import Path
8-
from typing import TYPE_CHECKING, ClassVar, Optional, Union, overload
9+
from typing import TYPE_CHECKING, ClassVar, overload
910

1011
import numpy as np
1112

1213
from ryd_numerov.units import rydberg_constant, ureg
1314

1415
if TYPE_CHECKING:
16+
from pathlib import Path
17+
1518
from ryd_numerov.model.model import PotentialType
1619
from ryd_numerov.units import PintFloat
1720

@@ -39,21 +42,19 @@ class BaseElement(ABC):
3942

4043
_core_electron_configuration: ClassVar[str]
4144
"""Electron configuration of the core electrons, e.g. 4p6 for Rb or 5s for Sr."""
42-
_ionization_energy: tuple[float, Optional[float], str]
45+
_ionization_energy: tuple[float, float | None, str]
4346
"""Ionization energy with uncertainty and unit: (value, uncertainty, unit)."""
4447

4548
# Parameters for the extended Rydberg Ritz formula, see calc_n_star
46-
_quantum_defects: ClassVar[Optional[dict[tuple[int, float, float], tuple[float, float, float, float, float]]]] = (
47-
None
48-
)
49+
_quantum_defects: ClassVar[dict[tuple[int, float, float], tuple[float, float, float, float, float]] | None] = None
4950
"""Dictionary containing the quantum defects for each (l, j_tot, s_tot) combination, i.e.
5051
_quantum_defects[(l,j_tot,s_tot)] = (d0, d2, d4, d6, d8)
5152
"""
5253

53-
_corrected_rydberg_constant: tuple[float, Optional[float], str]
54+
_corrected_rydberg_constant: tuple[float, float | None, str]
5455
r"""Corrected Rydberg constant stored as (value, uncertainty, unit)"""
5556

56-
potential_type_default: Optional["PotentialType"] = None
57+
potential_type_default: PotentialType | None = None
5758
"""Default potential type to use for this element. If None, the potential type must be specified explicitly.
5859
In general, it looks like marinescu_1993 is better for alkali atoms, and fei_2009 is better for alkaline earth atoms
5960
"""
@@ -78,7 +79,7 @@ class BaseElement(ABC):
7879
defined in: Y. Fei et al., Chin. Phys. B 18, 4349 (2009), https://iopscience.iop.org/article/10.1088/1674-1056/18/10/025
7980
"""
8081

81-
_nist_energy_levels_file: Optional[Path] = None
82+
_nist_energy_levels_file: Path | None = None
8283
"""Path to the NIST energy levels file for this element.
8384
The file should be directly downloaded from https://physics.nist.gov/PhysRefData/ASD/levels_form.html
8485
in the 'Tab-delimited' format and in units of Hartree.
@@ -170,7 +171,7 @@ def _setup_nist_energy_levels(self, file: Path) -> None: # noqa: C901, PLR0912
170171

171172
@classmethod
172173
@cache
173-
def from_species(cls, species: str, use_nist_data: bool = True) -> "BaseElement":
174+
def from_species(cls, species: str, use_nist_data: bool = True) -> BaseElement:
174175
"""Create an instance of the element class from the species string.
175176
176177
This method searches through all subclasses of BaseElement until it finds one with a matching species attribute.
@@ -196,7 +197,7 @@ def from_species(cls, species: str, use_nist_data: bool = True) -> "BaseElement"
196197
)
197198

198199
@classmethod
199-
def _get_concrete_subclasses(cls) -> list[type["BaseElement"]]:
200+
def _get_concrete_subclasses(cls) -> list[type[BaseElement]]:
200201
subclasses = []
201202
for subclass in cls.__subclasses__():
202203
if not inspect.isabstract(subclass) and hasattr(subclass, "species"):
@@ -239,12 +240,12 @@ def is_allowed_shell(self, n: int, l: int, s_tot: float) -> bool:
239240
return (n, l) in self._additional_allowed_shells
240241

241242
@overload
242-
def get_ionization_energy(self, unit: None = None) -> "PintFloat": ...
243+
def get_ionization_energy(self, unit: None = None) -> PintFloat: ...
243244

244245
@overload
245246
def get_ionization_energy(self, unit: str) -> float: ...
246247

247-
def get_ionization_energy(self, unit: Optional[str] = "hartree") -> Union["PintFloat", float]:
248+
def get_ionization_energy(self, unit: str | None = "hartree") -> PintFloat | float:
248249
"""Return the ionization energy in the desired unit.
249250
250251
Args:
@@ -263,12 +264,12 @@ def get_ionization_energy(self, unit: Optional[str] = "hartree") -> Union["PintF
263264
return ionization_energy.to(unit, "spectroscopy").magnitude
264265

265266
@overload
266-
def get_corrected_rydberg_constant(self, unit: None = None) -> "PintFloat": ...
267+
def get_corrected_rydberg_constant(self, unit: None = None) -> PintFloat: ...
267268

268269
@overload
269270
def get_corrected_rydberg_constant(self, unit: str) -> float: ...
270271

271-
def get_corrected_rydberg_constant(self, unit: Optional[str] = "hartree") -> Union["PintFloat", float]:
272+
def get_corrected_rydberg_constant(self, unit: str | None = "hartree") -> PintFloat | float:
272273
r"""Return the corrected Rydberg constant in the desired unit.
273274
274275
The corrected Rydberg constant is defined as
@@ -317,14 +318,14 @@ def reduced_mass_factor(self) -> float:
317318
)
318319

319320
@overload
320-
def calc_energy(self, n: int, l: int, j_tot: float, s_tot: float, unit: None = None) -> "PintFloat": ...
321+
def calc_energy(self, n: int, l: int, j_tot: float, s_tot: float, unit: None = None) -> PintFloat: ...
321322

322323
@overload
323324
def calc_energy(self, n: int, l: int, j_tot: float, s_tot: float, unit: str) -> float: ...
324325

325326
def calc_energy(
326-
self, n: int, l: int, j_tot: float, s_tot: float, unit: Optional[str] = "hartree"
327-
) -> Union["PintFloat", float]:
327+
self, n: int, l: int, j_tot: float, s_tot: float, unit: str | None = "hartree"
328+
) -> PintFloat | float:
328329
r"""Calculate the energy of a Rydberg state with for the given n, l, j_tot and s_tot.
329330
330331
I.e. either look up the energy for low lying states in the nist data,
@@ -355,7 +356,7 @@ def calc_energy(
355356
if j_tot % 1 != (l + s_tot) % 1:
356357
raise ValueError(f"Invalid quantum numbers: ({l=}, {j_tot=}, {s_tot=})")
357358

358-
energy_au: Optional[float] = None
359+
energy_au: float | None = None
359360
if n <= self._nist_n_max and self.use_nist_data:
360361
if (n, l, j_tot, s_tot) in self._nist_energy_levels:
361362
energy_au = self._nist_energy_levels[(n, l, j_tot, s_tot)]

src/ryd_numerov/elements/strontium.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
from __future__ import annotations
2+
13
from pathlib import Path
2-
from typing import ClassVar, Optional
4+
from typing import ClassVar
35

46
from ryd_numerov.elements.base_element import BaseElement
57
from ryd_numerov.units import electron_mass, rydberg_constant
@@ -15,7 +17,7 @@ class _StrontiumAbstract(BaseElement):
1517
_nist_energy_levels_file = Path(__file__).parent / "nist_energy_levels" / "strontium.txt"
1618

1719
# https://webbook.nist.gov/cgi/inchi?ID=C7440246&Mask=20
18-
_ionization_energy: tuple[float, Optional[float], str] = (5.694_84, 0.000_02, "eV")
20+
_ionization_energy: tuple[float, float | None, str] = (5.694_84, 0.000_02, "eV")
1921

2022
potential_type_default = "model_potential_fei_2009"
2123

src/ryd_numerov/model/model.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
from __future__ import annotations
2+
13
import logging
2-
from typing import TYPE_CHECKING, Literal, Optional, TypeVar, get_args
4+
from typing import TYPE_CHECKING, Literal, TypeVar, get_args
35

46
import numpy as np
57

@@ -20,9 +22,9 @@ class Model:
2022

2123
def __init__(
2224
self,
23-
element: "BaseElement",
25+
element: BaseElement,
2426
l: int,
25-
potential_type: Optional[PotentialType] = None,
27+
potential_type: PotentialType | None = None,
2628
) -> None:
2729
r"""Initialize the model.
2830
@@ -43,7 +45,7 @@ def __init__(
4345
raise ValueError(f"Invalid potential type {potential_type}. Must be one of {get_args(PotentialType)}.")
4446
self.potential_type = potential_type
4547

46-
def calc_potential_coulomb(self, x: "XType") -> "XType":
48+
def calc_potential_coulomb(self, x: XType) -> XType:
4749
r"""Calculate the Coulomb potential V_Col(x) in atomic units.
4850
4951
The Coulomb potential is given as
@@ -62,7 +64,7 @@ def calc_potential_coulomb(self, x: "XType") -> "XType":
6264
"""
6365
return -1 / x
6466

65-
def calc_model_potential_marinescu_1993(self, x: "XType") -> "XType":
67+
def calc_model_potential_marinescu_1993(self, x: XType) -> XType:
6668
r"""Calculate the model potential by Marinescu et al. (1994) in atomic units.
6769
6870
The model potential from
@@ -114,7 +116,7 @@ def calc_model_potential_marinescu_1993(self, x: "XType") -> "XType":
114116

115117
return v_c + v_p
116118

117-
def calc_model_potential_fei_2009(self, x: "XType") -> "XType":
119+
def calc_model_potential_fei_2009(self, x: XType) -> XType:
118120
r"""Calculate the model potential by Fei et al. (2009) in atomic units.
119121
120122
The four parameter potential from Y. Fei et al., Chin. Phys. B 18, 4349 (2009), https://iopscience.iop.org/article/10.1088/1674-1056/18/10/025
@@ -138,7 +140,7 @@ def calc_model_potential_fei_2009(self, x: "XType") -> "XType":
138140
denom: XType = 1 - alpha + alpha * np.exp(beta * x**delta + gamma * x ** (2.0 * delta))
139141
return -1 / x - (self.element.Z - 1) / (x * denom)
140142

141-
def calc_effective_potential_centrifugal(self, x: "XType") -> "XType":
143+
def calc_effective_potential_centrifugal(self, x: XType) -> XType:
142144
r"""Calculate the effective centrifugal potential V_l(x) in atomic units.
143145
144146
The effective centrifugal potential is given as
@@ -158,7 +160,7 @@ def calc_effective_potential_centrifugal(self, x: "XType") -> "XType":
158160
x2 = x * x
159161
return (1 / self.element.reduced_mass_factor) * self.l * (self.l + 1) / (2 * x2)
160162

161-
def calc_effective_potential_sqrt(self, x: "XType") -> "XType":
163+
def calc_effective_potential_sqrt(self, x: XType) -> XType:
162164
r"""Calculate the effective potential V_sqrt(x) from the sqrt transformation in atomic units.
163165
164166
The sqrt transformation potential arises from the transformation from the wavefunction u(x) to w(z),
@@ -179,7 +181,7 @@ def calc_effective_potential_sqrt(self, x: "XType") -> "XType":
179181
x2 = x * x
180182
return (1 / self.element.reduced_mass_factor) * (3 / 32) / x2
181183

182-
def calc_total_effective_potential(self, x: "XType") -> "XType":
184+
def calc_total_effective_potential(self, x: XType) -> XType:
183185
r"""Calculate the total effective potential V_eff(x) in atomic units.
184186
185187
The total effective potential includes all physical and effective potentials:

src/ryd_numerov/radial/grid.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
from typing import TYPE_CHECKING, Optional
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING
24

35
import numpy as np
46

@@ -65,7 +67,7 @@ def z_max(self) -> float:
6567
return self.z_list[-1] # type: ignore [no-any-return] # FIXME: numpy indexing
6668

6769
@property
68-
def z_list(self) -> "NDArray":
70+
def z_list(self) -> NDArray:
6971
"""The grid in the scaled dimensionless coordinate z = sqrt{x}.
7072
7173
In this coordinate the grid points are chosen equidistant,
@@ -84,11 +86,11 @@ def x_max(self) -> float:
8486
return self.z_max**2
8587

8688
@property
87-
def x_list(self) -> "NDArray":
89+
def x_list(self) -> NDArray:
8890
"""The grid in the dimensionless coordinate x = r/a_0."""
8991
return self.z_list**2
9092

91-
def set_grid_range(self, step_start: Optional[int] = None, step_stop: Optional[int] = None) -> None:
93+
def set_grid_range(self, step_start: int | None = None, step_stop: int | None = None) -> None:
9294
"""Restrict the grid to the range [step_start, step_stop]."""
9395
if step_start is None:
9496
step_start = 0

src/ryd_numerov/radial/numerov.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
from collections.abc import Sequence
2-
from typing import TYPE_CHECKING, Callable, Union
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING, Callable
34

45
from numba import njit
56

67
if TYPE_CHECKING:
8+
from collections.abc import Sequence
9+
710
from ryd_numerov.units import NDArray
811

912

@@ -13,7 +16,7 @@ def _run_numerov_integration_python(
1316
dx: float,
1417
y0: float,
1518
y1: float,
16-
g_list: Union[Sequence[float], "NDArray"],
19+
g_list: Sequence[float] | NDArray,
1720
x_min: float,
1821
verbose: bool = False,
1922
) -> list[float]:
@@ -58,7 +61,7 @@ def run_numerov_integration(
5861
dx: float,
5962
y0: float,
6063
y1: float,
61-
g_list: Union[Sequence[float], "NDArray"],
64+
g_list: Sequence[float] | NDArray,
6265
x_min: float,
6366
verbose: bool = False,
6467
) -> list[float]:

src/ryd_numerov/radial/radial_matrix_element.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import logging
24
from typing import TYPE_CHECKING, Literal
35

@@ -14,8 +16,8 @@
1416

1517

1618
def calc_radial_matrix_element(
17-
state1: "RydbergStateBase",
18-
state2: "RydbergStateBase",
19+
state1: RydbergStateBase,
20+
state2: RydbergStateBase,
1921
k_radial: int = 0,
2022
integration_method: INTEGRATION_METHODS = "sum",
2123
) -> float:
@@ -52,10 +54,10 @@ def calc_radial_matrix_element(
5254

5355

5456
def _calc_radial_matrix_element_from_w_z(
55-
z1: "NDArray",
56-
w1: "NDArray",
57-
z2: "NDArray",
58-
w2: "NDArray",
57+
z1: NDArray,
58+
w1: NDArray,
59+
z2: NDArray,
60+
w2: NDArray,
5961
k_radial: int = 0,
6062
integration_method: INTEGRATION_METHODS = "sum",
6163
) -> float:
@@ -120,7 +122,7 @@ def _calc_radial_matrix_element_from_w_z(
120122
return _integrate(integrand, dz, integration_method)
121123

122124

123-
def _multiply_by_powers(result: "NDArray", base: "NDArray", exponent: int) -> "NDArray":
125+
def _multiply_by_powers(result: NDArray, base: NDArray, exponent: int) -> NDArray:
124126
"""Calculate result * base**(exponent) in an optimized way."""
125127
base_powers = {0: base}
126128
for i in range(exponent):
@@ -133,7 +135,7 @@ def _multiply_by_powers(result: "NDArray", base: "NDArray", exponent: int) -> "N
133135
return result
134136

135137

136-
def _sanity_check_integration(z1: "NDArray", z2: "NDArray") -> None:
138+
def _sanity_check_integration(z1: NDArray, z2: NDArray) -> None:
137139
tol = 1e-10
138140
assert len(z1) == len(z2), f"Length mismatch: {len(z1)=} != {len(z2)=}"
139141
assert z1[0] - z2[0] < tol, f"First point mismatch: {z1[0]=} != {z2[0]=}"
@@ -142,7 +144,7 @@ def _sanity_check_integration(z1: "NDArray", z2: "NDArray") -> None:
142144
assert z1[-1] - z2[-1] < tol, f"Last point mismatch: {z1[-1]=} != {z2[-1]=}"
143145

144146

145-
def _integrate(integrand: "NDArray", dz: float, method: INTEGRATION_METHODS) -> float:
147+
def _integrate(integrand: NDArray, dz: float, method: INTEGRATION_METHODS) -> float:
146148
"""Integrate the given integrand using the specified method."""
147149
if method == "sum":
148150
value = np.sum(integrand) * dz

0 commit comments

Comments
 (0)