Skip to content

Commit 4a237b4

Browse files
committed
Cleanup all unnecessary type definitions. There really isn't a need to
define things like Tuple3Ints, etc. when it is easier to just read the primitive types. We only introduce type definitions for things that are not easily covered under typical primitives or NDArray.
1 parent 74dd716 commit 4a237b4

30 files changed

+241
-252
lines changed

src/pymatgen/analysis/diffraction/tem.py

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
from numpy.typing import NDArray
2121

2222
from pymatgen.core import Structure
23-
from pymatgen.util.typing import Tuple3Ints
2423

2524
__author__ = "Frank Wan, Jason Liang"
2625
__copyright__ = "Copyright 2020, The Materials Project"
@@ -48,7 +47,7 @@ def __init__(
4847
self,
4948
symprec: float | None = None,
5049
voltage: float = 200,
51-
beam_direction: Tuple3Ints = (0, 0, 1),
50+
beam_direction: tuple[int, int, int] = (0, 0, 1),
5251
camera_length: int = 160,
5352
debye_waller_factors: dict[str, float] | None = None,
5453
cs: float = 1,
@@ -105,7 +104,9 @@ def generate_points(coord_left: int = -10, coord_right: int = 10) -> np.ndarray:
105104
points_matrix = (np.ravel(points[i]) for i in range(3))
106105
return np.vstack(list(points_matrix)).transpose()
107106

108-
def zone_axis_filter(self, points: list[Tuple3Ints] | np.ndarray, laue_zone: int = 0) -> list[Tuple3Ints]:
107+
def zone_axis_filter(
108+
self, points: list[tuple[int, int, int]] | np.ndarray, laue_zone: int = 0
109+
) -> list[tuple[int, int, int]]:
109110
"""Filter out all points that exist within the specified Laue zone according to the zone axis rule.
110111
111112
Args:
@@ -121,11 +122,11 @@ def zone_axis_filter(self, points: list[Tuple3Ints] | np.ndarray, laue_zone: int
121122
return []
122123
filtered = np.where(np.dot(np.array(self.beam_direction), np.transpose(points)) == laue_zone)
123124
result = points[filtered]
124-
return cast("list[Tuple3Ints]", [tuple(x) for x in result.tolist()])
125+
return cast("list[tuple[int, int, int]]", [tuple(x) for x in result.tolist()])
125126

126127
def get_interplanar_spacings(
127-
self, structure: Structure, points: list[Tuple3Ints] | np.ndarray
128-
) -> dict[Tuple3Ints, float]:
128+
self, structure: Structure, points: list[tuple[int, int, int]] | np.ndarray
129+
) -> dict[tuple[int, int, int], float]:
129130
"""
130131
Args:
131132
structure (Structure): the input structure.
@@ -141,7 +142,9 @@ def get_interplanar_spacings(
141142
interplanar_spacings_val = np.array([structure.lattice.d_hkl(x) for x in points_filtered])
142143
return dict(zip(points_filtered, interplanar_spacings_val, strict=True))
143144

144-
def bragg_angles(self, interplanar_spacings: dict[Tuple3Ints, float]) -> dict[Tuple3Ints, float]:
145+
def bragg_angles(
146+
self, interplanar_spacings: dict[tuple[int, int, int], float]
147+
) -> dict[tuple[int, int, int], float]:
145148
"""Get the Bragg angles for every hkl point passed in (where n = 1).
146149
147150
Args:
@@ -155,7 +158,7 @@ def bragg_angles(self, interplanar_spacings: dict[Tuple3Ints, float]) -> dict[Tu
155158
bragg_angles_val = np.arcsin(self.wavelength_rel() / (2 * interplanar_spacings_val))
156159
return dict(zip(plane, bragg_angles_val, strict=True))
157160

158-
def get_s2(self, bragg_angles: dict[Tuple3Ints, float]) -> dict[Tuple3Ints, float]:
161+
def get_s2(self, bragg_angles: dict[tuple[int, int, int], float]) -> dict[tuple[int, int, int], float]:
159162
"""
160163
Calculates the s squared parameter (= square of sin theta over lambda) for each hkl plane.
161164
@@ -172,8 +175,8 @@ def get_s2(self, bragg_angles: dict[Tuple3Ints, float]) -> dict[Tuple3Ints, floa
172175
return dict(zip(plane, s2_val, strict=True))
173176

174177
def x_ray_factors(
175-
self, structure: Structure, bragg_angles: dict[Tuple3Ints, float]
176-
) -> dict[str, dict[Tuple3Ints, float]]:
178+
self, structure: Structure, bragg_angles: dict[tuple[int, int, int], float]
179+
) -> dict[str, dict[tuple[int, int, int], float]]:
177180
"""
178181
Calculates x-ray factors, which are required to calculate atomic scattering factors. Method partially inspired
179182
by the equivalent process in the xrd module.
@@ -202,8 +205,8 @@ def x_ray_factors(
202205
return x_ray_factors
203206

204207
def electron_scattering_factors(
205-
self, structure: Structure, bragg_angles: dict[Tuple3Ints, float]
206-
) -> dict[str, dict[Tuple3Ints, float]]:
208+
self, structure: Structure, bragg_angles: dict[tuple[int, int, int], float]
209+
) -> dict[str, dict[tuple[int, int, int], float]]:
207210
"""
208211
Calculates atomic scattering factors for electrons using the Mott-Bethe formula (1st order Born approximation).
209212
@@ -229,8 +232,8 @@ def electron_scattering_factors(
229232
return electron_scattering_factors
230233

231234
def cell_scattering_factors(
232-
self, structure: Structure, bragg_angles: dict[Tuple3Ints, float]
233-
) -> dict[Tuple3Ints, int]:
235+
self, structure: Structure, bragg_angles: dict[tuple[int, int, int], float]
236+
) -> dict[tuple[int, int, int], int]:
234237
"""
235238
Calculates the scattering factor for the whole cell.
236239
@@ -255,7 +258,9 @@ def cell_scattering_factors(
255258
scattering_factor_curr = 0
256259
return cell_scattering_factors
257260

258-
def cell_intensity(self, structure: Structure, bragg_angles: dict[Tuple3Ints, float]) -> dict[Tuple3Ints, float]:
261+
def cell_intensity(
262+
self, structure: Structure, bragg_angles: dict[tuple[int, int, int], float]
263+
) -> dict[tuple[int, int, int], float]:
259264
"""
260265
Calculates cell intensity for each hkl plane. For simplicity's sake, take I = |F|**2.
261266
@@ -312,8 +317,8 @@ def get_pattern(
312317
return pd.DataFrame(rows, columns=field_names)
313318

314319
def normalized_cell_intensity(
315-
self, structure: Structure, bragg_angles: dict[Tuple3Ints, float]
316-
) -> dict[Tuple3Ints, float]:
320+
self, structure: Structure, bragg_angles: dict[tuple[int, int, int], float]
321+
) -> dict[tuple[int, int, int], float]:
317322
"""
318323
Normalizes the cell_intensity dict to 1, for use in plotting.
319324
@@ -335,8 +340,8 @@ def normalized_cell_intensity(
335340
def is_parallel(
336341
self,
337342
structure: Structure,
338-
plane: Tuple3Ints,
339-
other_plane: Tuple3Ints,
343+
plane: tuple[int, int, int],
344+
other_plane: tuple[int, int, int],
340345
) -> bool:
341346
"""Checks if two hkl planes are parallel in reciprocal space.
342347
@@ -351,7 +356,7 @@ def is_parallel(
351356
phi = self.get_interplanar_angle(structure, plane, other_plane)
352357
return phi in (180, 0) or np.isnan(phi)
353358

354-
def get_first_point(self, structure: Structure, points: list) -> dict[Tuple3Ints, float]:
359+
def get_first_point(self, structure: Structure, points: list) -> dict[tuple[int, int, int], float]:
355360
"""Get the first point to be plotted in the 2D DP, corresponding to maximum d/minimum R.
356361
357362
Args:
@@ -372,7 +377,7 @@ def get_first_point(self, structure: Structure, points: list) -> dict[Tuple3Ints
372377
return {max_d_plane: max_d}
373378

374379
@staticmethod
375-
def get_interplanar_angle(structure: Structure, p1: Tuple3Ints, p2: Tuple3Ints) -> float:
380+
def get_interplanar_angle(structure: Structure, p1: tuple[int, int, int], p2: tuple[int, int, int]) -> float:
376381
"""Get the interplanar angle (in degrees) between the normal of two crystal planes.
377382
Formulas from International Tables for Crystallography Volume C pp. 2-9.
378383
@@ -426,9 +431,9 @@ def get_interplanar_angle(structure: Structure, p1: Tuple3Ints, p2: Tuple3Ints)
426431

427432
@staticmethod
428433
def get_plot_coeffs(
429-
p1: Tuple3Ints,
430-
p2: Tuple3Ints,
431-
p3: Tuple3Ints,
434+
p1: tuple[int, int, int],
435+
p2: tuple[int, int, int],
436+
p3: tuple[int, int, int],
432437
) -> np.ndarray:
433438
"""
434439
Calculates coefficients of the vector addition required to generate positions for each DP point
@@ -448,7 +453,7 @@ def get_plot_coeffs(
448453
x = np.dot(a_pinv, b)
449454
return np.ravel(x)
450455

451-
def get_positions(self, structure: Structure, points: list) -> dict[Tuple3Ints, np.ndarray]:
456+
def get_positions(self, structure: Structure, points: list) -> dict[tuple[int, int, int], np.ndarray]:
452457
"""
453458
Calculates all the positions of each hkl point in the 2D diffraction pattern by vector addition.
454459
Distance in centimeters.
@@ -518,7 +523,7 @@ def tem_dots(self, structure: Structure, points) -> list:
518523

519524
class dot(NamedTuple):
520525
position: NDArray
521-
hkl: Tuple3Ints
526+
hkl: tuple[int, int, int]
522527
intensity: float
523528
film_radius: float
524529
d_spacing: float

src/pymatgen/analysis/graphs.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343

4444
from pymatgen.analysis.local_env import NearNeighbors
4545
from pymatgen.core import Species
46-
from pymatgen.util.typing import Tuple3Ints
4746

4847

4948
logger = logging.getLogger(__name__)
@@ -59,7 +58,7 @@
5958

6059
class ConnectedSite(NamedTuple):
6160
site: PeriodicSite
62-
jimage: Tuple3Ints
61+
jimage: tuple[int, int, int]
6362
index: Any # TODO: use more specific type
6463
weight: float
6564
dist: float
@@ -334,8 +333,8 @@ def add_edge(
334333
self,
335334
from_index: int,
336335
to_index: int,
337-
from_jimage: Tuple3Ints = (0, 0, 0),
338-
to_jimage: Tuple3Ints | None = None,
336+
from_jimage: tuple[int, int, int] = (0, 0, 0),
337+
to_jimage: tuple[int, int, int] | None = None,
339338
weight: float | None = None,
340339
warn_duplicates: bool = True,
341340
edge_properties: dict | None = None,
@@ -755,7 +754,7 @@ def map_indices(grp: Molecule) -> dict[int, int]:
755754
warn_duplicates=False,
756755
)
757756

758-
def get_connected_sites(self, n: int, jimage: Tuple3Ints = (0, 0, 0)) -> list[ConnectedSite]:
757+
def get_connected_sites(self, n: int, jimage: tuple[int, int, int] = (0, 0, 0)) -> list[ConnectedSite]:
759758
"""Get a named tuple of neighbors of site n:
760759
periodic_site, jimage, index, weight.
761760
Index is the index of the corresponding site

src/pymatgen/analysis/interfaces/coherent_interfaces.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
from collections.abc import Iterator, Sequence
1818

1919
from pymatgen.core import Structure
20-
from pymatgen.util.typing import Tuple3Ints
2120

2221

2322
class CoherentInterfaceBuilder:
@@ -30,8 +29,8 @@ def __init__(
3029
self,
3130
substrate_structure: Structure,
3231
film_structure: Structure,
33-
film_miller: Tuple3Ints,
34-
substrate_miller: Tuple3Ints,
32+
film_miller: tuple[int, int, int],
33+
substrate_miller: tuple[int, int, int],
3534
zslgen: ZSLGenerator | None = None,
3635
termination_ftol: float = 0.25,
3736
label_index: bool = False, # necessary to add index to termination

src/pymatgen/analysis/interfaces/substrate_analyzer.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from typing_extensions import Self
1515

1616
from pymatgen.core import Structure
17-
from pymatgen.util.typing import Tuple3Ints
1817

1918

2019
@dataclass
@@ -25,8 +24,8 @@ class SubstrateMatch(ZSLMatch):
2524
energy if provided, and the elastic energy.
2625
"""
2726

28-
film_miller: Tuple3Ints
29-
substrate_miller: Tuple3Ints
27+
film_miller: tuple[int, int, int]
28+
substrate_miller: tuple[int, int, int]
3029
strain: Strain
3130
von_mises_strain: float
3231
ground_state_energy: float

src/pymatgen/analysis/local_env.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040

4141
from pymatgen.analysis.graphs import MoleculeGraph
4242
from pymatgen.core.composition import SpeciesLike
43-
from pymatgen.util.typing import Tuple3Ints
4443

4544

4645
__author__ = "Shyue Ping Ong, Geoffroy Hautier, Sai Jayaraman, "
@@ -563,7 +562,7 @@ def _get_nn_shell_info(
563562
return list(all_sites.values())
564563

565564
@staticmethod
566-
def _get_image(structure: Structure, site: Site) -> Tuple3Ints:
565+
def _get_image(structure: Structure, site: Site) -> tuple[int, int, int]:
567566
"""Private convenience method for get_nn_info,
568567
gives lattice image from provided PeriodicSite and Structure.
569568

src/pymatgen/analysis/surface_analysis.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
if TYPE_CHECKING:
5858
from typing_extensions import Self
5959

60-
from pymatgen.util.typing import Tuple3Ints
6160

6261
EV_PER_ANG2_TO_JOULES_PER_M2 = 16.0217656
6362

@@ -573,7 +572,7 @@ def area_frac_vs_chempot_plot(
573572
all_chempots = np.linspace(min(chempot_range), max(chempot_range), increments)
574573

575574
# initialize a dictionary of lists of fractional areas for each hkl
576-
hkl_area_dict: dict[Tuple3Ints, list[float]] = {}
575+
hkl_area_dict: dict[tuple[int, int, int], list[float]] = {}
577576
for hkl in self.all_slab_entries:
578577
hkl_area_dict[hkl] = []
579578

0 commit comments

Comments
 (0)