Skip to content

Commit 0e5985d

Browse files
committed
add types
1 parent 5b997f7 commit 0e5985d

File tree

2 files changed

+35
-20
lines changed

2 files changed

+35
-20
lines changed

src/pymatgen/core/periodic_table.py

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,20 @@
2626
from pymatgen.util.string import Stringify, formula_double_format
2727

2828
if TYPE_CHECKING:
29-
from collections.abc import Callable
29+
from collections.abc import Callable, Sequence
3030
from typing import Any, Literal
3131

3232
from typing_extensions import Self
3333

3434
from pymatgen.util.typing import SpeciesLike
3535

36-
# Load element data from JSON file
36+
# Load element data (periodic table) from JSON file
3737
with open(Path(__file__).absolute().parent / "periodic_table.json", encoding="utf-8") as ptable_json:
38-
_pt_data = json.load(ptable_json)
38+
_pt_data: dict = json.load(ptable_json)
3939

40-
_pt_row_sizes = (2, 8, 8, 18, 18, 32, 32)
40+
_pt_row_sizes: tuple[int, ...] = (2, 8, 8, 18, 18, 32, 32)
4141

42-
_madelung = [
42+
_madelung: list[tuple[int, str]] = [
4343
(1, "s"),
4444
(2, "s"),
4545
(2, "p"),
@@ -144,8 +144,8 @@ def __init__(self, symbol: SpeciesLike) -> None:
144144

145145
self._is_named_isotope = data.get("Is named isotope", False)
146146
if self._is_named_isotope:
147-
for sym in _pt_data:
148-
if _pt_data[sym]["Atomic no"] == self.Z and not _pt_data[sym].get("Is named isotope", False):
147+
for sym, info in _pt_data.items():
148+
if info["Atomic no"] == self.Z and not info.get("Is named isotope", False):
149149
self.symbol = sym
150150
break
151151
# For specified/named isotopes, treat the same as named element
@@ -452,32 +452,47 @@ def icsd_oxidation_states(self) -> tuple[int, ...]:
452452

453453
@property
454454
def full_electronic_structure(self) -> list[tuple[int, str, int]]:
455-
"""Full electronic structure as list of tuples, in order of increasing
455+
"""Full electronic structure in order of increasing
456456
energy level (according to the Madelung rule). Therefore, the final
457457
element in the list gives the electronic structure of the valence shell.
458458
459-
For example, the electronic structure for Fe is represented as:
460-
[(1, "s", 2), (2, "s", 2), (2, "p", 6), (3, "s", 2), (3, "p", 6),
461-
(4, "s", 2), (3, "d", 6)].
459+
For example, the full electronic structure for Fe is:
460+
[(1, "s", 2), (2, "s", 2), (2, "p", 6), (3, "s", 2), (3, "p", 6),
461+
(4, "s", 2), (3, "d", 6)].
462462
463463
References:
464464
Kramida, A., Ralchenko, Yu., Reader, J., and NIST ASD Team (2023). NIST
465465
Atomic Spectra Database (ver. 5.11). https://physics.nist.gov/asd [2024,
466466
June 3]. National Institute of Standards and Technology, Gaithersburg,
467467
MD. DOI: https://doi.org/10.18434/T4W30F
468+
469+
Returns:
470+
list[tuple[int, str, int]]: A list of tuples representing each subshell,
471+
where each tuple contains:
472+
- `n` (int): Principal quantum number.
473+
- `orbital_type` (str): Orbital type (e.g., "s", "p", "d", "f").
474+
- `electron_count` (int): Number of electrons in the subshell.
468475
"""
469-
e_str = self.electronic_structure
476+
e_str: str = self.electronic_structure
470477

471-
def parse_orbital(orb_str):
478+
def parse_orbital(orb_str: str) -> str | tuple[int, str, int]:
479+
"""Parse orbital information from split electron configuration string."""
480+
# Parse valence subshell notation (e.g., "3d6" -> (3, "d", 6))
472481
if match := re.match(r"(\d+)([spdfg]+)(\d+)", orb_str):
473482
return int(match[1]), match[2], int(match[3])
483+
484+
# Return core-electron configuration as-is (e.g. "[Ar]")
474485
return orb_str
475486

476-
data = [parse_orbital(s) for s in e_str.split(".")]
477-
if data[0][0] == "[":
478-
sym = data[0].replace("[", "").replace("]", "")
487+
# Split e_str (e.g. for Fe "[Ar].3d6.4s2" into ["[Ar]", "3d6", "4s2"])
488+
data: Sequence[str | tuple[int, str, int]] = [parse_orbital(s) for s in e_str.split(".")]
489+
490+
# Fully expand core-electron configuration (replace noble gas notation string)
491+
if isinstance(data[0], str):
492+
sym: str = data[0].replace("[", "").replace("]", "")
479493
data = list(Element(sym).full_electronic_structure) + data[1:]
480-
# sort the final electronic structure by increasing energy level
494+
495+
# Sort the final electronic structure by increasing energy level
481496
return sorted(data, key=lambda x: _madelung.index((x[0], x[1])))
482497

483498
@property

src/pymatgen/io/vasp/inputs.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2136,9 +2136,9 @@ def electron_configuration(self) -> list[tuple[int, str, int]] | None:
21362136
)
21372137
return None
21382138

2139-
el = Element.from_Z(self.atomic_no)
2140-
full_config = el.full_electronic_structure
2141-
nelect = self.nelectrons
2139+
el: Element = Element.from_Z(self.atomic_no)
2140+
full_config: list[tuple[int, str, int]] = el.full_electronic_structure
2141+
nelect: float = self.nelectrons
21422142
config = []
21432143
while nelect > 0:
21442144
e = full_config.pop(-1)

0 commit comments

Comments
 (0)