Skip to content

Commit 6c9ddef

Browse files
authored
Hide all type-hint-only imports behind if TYPE_CHECKING (#2992)
* remove unnecessary decimal points (.0) * fix mypy * ruff enable TCH (flake8-type-checking) * hide all type-hint-only imports behind if TYPE_CHECKING remove numpy.typing.ArrayLike re-export from pymatgen/util/typing.py * shorten import pymatgen.core.(structure.)Structure * fix mypy
1 parent 0b76613 commit 6c9ddef

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+435
-281
lines changed

dev_scripts/chemenv/plane_multiplicity.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@
3232
equiv_plane.extend(opposite_edge)
3333
equiv_plane.sort()
3434
all_plane_points.append(tuple(equiv_plane))
35-
all_plane_points = list(set(all_plane_points))
36-
all_plane_points = [list(equiv_plane) for equiv_plane in all_plane_points]
35+
all_plane_points = [tuple(equiv_plane) for equiv_plane in set(all_plane_points)]
3736

3837
print(f"All plane points ({len(all_plane_points):d}) for {cg_symbol} : ")
3938
print(all_plane_points)

dev_scripts/chemenv/test_algos.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
algos_results.append(min(results[0]))
8888

8989
if not np.isclose(min(results[0]), 0):
90-
print("Following is not 0.0 ...")
90+
print("Following is not 0 ...")
9191
input(results)
9292
print(" => ", algos_results)
9393
idx_perm += 1

dev_scripts/chemenv/test_algos_all_geoms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@
118118
algos_results.append(min(results[0]))
119119

120120
if not min(results[0]) < 1.5:
121-
print("Following is not close to 0.0 ...")
121+
print("Following is not close to 0 ...")
122122
input(results)
123123
print(" => ", algos_results)
124124
i_perm += 1

docs_rst/conf-normal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@
204204

205205
# -- Options for LaTeX output ------------------------------------------------
206206

207-
latex_elements = {
207+
latex_elements: dict[str, str] = {
208208
# The paper size ('letterpaper' or 'a4paper').
209209
# 'papersize': 'letterpaper',
210210
# The font size ('10pt', '11pt' or '12pt').

pymatgen/alchemy/filters.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@
66

77
import abc
88
from collections import defaultdict
9+
from typing import TYPE_CHECKING
910

1011
from monty.json import MSONable
1112

1213
from pymatgen.analysis.structure_matcher import ElementComparator, StructureMatcher
1314
from pymatgen.core.periodic_table import get_el_sp
14-
from pymatgen.core.structure import Structure
1515
from pymatgen.symmetry.analyzer import SpacegroupAnalyzer
1616

17+
if TYPE_CHECKING:
18+
from pymatgen.core import Structure
19+
1720

1821
class AbstractStructureFilter(MSONable, metaclass=abc.ABCMeta):
1922
"""

pymatgen/alchemy/materials.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,21 @@
1010
import json
1111
import os
1212
import re
13-
from typing import Any
13+
from typing import TYPE_CHECKING, Any
1414
from warnings import warn
1515

1616
from monty.json import MSONable, jsanitize
1717

18-
from pymatgen.alchemy.filters import AbstractStructureFilter
1918
from pymatgen.core.structure import Structure
2019
from pymatgen.io.cif import CifParser
2120
from pymatgen.io.vasp.inputs import Poscar
2221
from pymatgen.io.vasp.sets import MPRelaxSet, VaspInputSet
23-
from pymatgen.transformations.transformation_abc import AbstractTransformation
2422
from pymatgen.util.provenance import StructureNL
2523

24+
if TYPE_CHECKING:
25+
from pymatgen.alchemy.filters import AbstractStructureFilter
26+
from pymatgen.transformations.transformation_abc import AbstractTransformation
27+
2628

2729
class TransformedStructure(MSONable):
2830
"""

pymatgen/analysis/bond_valence.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def _calc_site_probabilities(self, site, nn):
181181
try:
182182
prob = {k: v / sum(prob.values()) for k, v in prob.items()}
183183
except ZeroDivisionError:
184-
prob = {k: 0.0 for k in prob}
184+
prob = {key: 0 for key in prob}
185185
return prob
186186

187187
def _calc_site_probabilities_unordered(self, site, nn):
@@ -202,7 +202,7 @@ def _calc_site_probabilities_unordered(self, site, nn):
202202
try:
203203
prob[el] = {k: v / sum(prob[el].values()) for k, v in prob[el].items()}
204204
except ZeroDivisionError:
205-
prob[el] = {k: 0.0 for k in prob[el]}
205+
prob[el] = {key: 0 for key in prob[el]}
206206
return prob
207207

208208
def get_valences(self, structure):

pymatgen/analysis/chemenv/utils/defs_utils.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@
44

55
from __future__ import annotations
66

7+
from typing import TYPE_CHECKING
8+
79
from pymatgen.analysis.chemenv.utils.coordination_geometry_utils import (
810
is_anion_cation_bond,
911
)
10-
from pymatgen.core.structure import Structure
12+
13+
if TYPE_CHECKING:
14+
from pymatgen.core import Structure
15+
1116

1217
__author__ = "David Waroquiers"
1318
__copyright__ = "Copyright 2012, The Materials Project"

pymatgen/analysis/chempot_diagram.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import warnings
2828
from functools import lru_cache
2929
from itertools import groupby
30+
from typing import TYPE_CHECKING
3031

3132
import numpy as np
3233
import plotly.express as px
@@ -36,10 +37,12 @@
3637

3738
from pymatgen.analysis.phase_diagram import PDEntry, PhaseDiagram
3839
from pymatgen.core.composition import Composition, Element
39-
from pymatgen.entries.computed_entries import ComputedEntry
4040
from pymatgen.util.coord import Simplex
4141
from pymatgen.util.string import htmlify
4242

43+
if TYPE_CHECKING:
44+
from pymatgen.entries.computed_entries import ComputedEntry
45+
4346
with open(os.path.join(os.path.dirname(__file__), "..", "util", "plotly_chempot_layouts.json")) as f:
4447
plotly_layouts = json.load(f)
4548

pymatgen/analysis/diffraction/core.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66

77
import abc
88
import collections
9+
from typing import TYPE_CHECKING
910

1011
import numpy as np
1112

1213
from pymatgen.core.spectrum import Spectrum
13-
from pymatgen.core.structure import Structure
1414
from pymatgen.util.plotting import add_fig_kwargs
1515

16+
if TYPE_CHECKING:
17+
from pymatgen.core import Structure
18+
1619

1720
class DiffractionPattern(Spectrum):
1821
"""

pymatgen/analysis/diffraction/neutron.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import json
88
import os
99
from math import asin, cos, degrees, pi, radians, sin
10+
from typing import TYPE_CHECKING
1011

1112
import numpy as np
1213

@@ -15,9 +16,11 @@
1516
DiffractionPattern,
1617
get_unique_families,
1718
)
18-
from pymatgen.core.structure import Structure
1919
from pymatgen.symmetry.analyzer import SpacegroupAnalyzer
2020

21+
if TYPE_CHECKING:
22+
from pymatgen.core import Structure
23+
2124
__author__ = "Yuta Suzuki"
2225
__copyright__ = "Copyright 2018, The Materials Project"
2326
__version__ = "0.1"

pymatgen/analysis/diffraction/tem.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,20 @@
1111
from collections import namedtuple
1212
from fractions import Fraction
1313
from functools import lru_cache
14-
from typing import List, Tuple, cast
14+
from typing import TYPE_CHECKING, List, Tuple, cast
1515

1616
import numpy as np
1717
import pandas as pd
1818
import plotly.graph_objs as go
1919
import scipy.constants as sc
2020

2121
from pymatgen.analysis.diffraction.core import AbstractDiffractionPatternCalculator
22-
from pymatgen.core.structure import Structure
2322
from pymatgen.symmetry.analyzer import SpacegroupAnalyzer
2423
from pymatgen.util.string import latexify_spacegroup, unicodeify_spacegroup
2524

25+
if TYPE_CHECKING:
26+
from pymatgen.core import Structure
27+
2628
with open(os.path.join(os.path.dirname(__file__), "atomic_scattering_params.json")) as f:
2729
ATOMIC_SCATTERING_PARAMS = json.load(f)
2830

pymatgen/analysis/diffraction/xrd.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import json
88
import os
99
from math import asin, cos, degrees, pi, radians, sin
10+
from typing import TYPE_CHECKING
1011

1112
import numpy as np
1213

@@ -15,9 +16,11 @@
1516
DiffractionPattern,
1617
get_unique_families,
1718
)
18-
from pymatgen.core.structure import Structure
1919
from pymatgen.symmetry.analyzer import SpacegroupAnalyzer
2020

21+
if TYPE_CHECKING:
22+
from pymatgen.core import Structure
23+
2124
# XRD wavelengths in angstroms
2225
WAVELENGTHS = {
2326
"CuKa": 1.54184,

pymatgen/analysis/disorder.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66

77
import collections
88
import itertools
9+
from typing import TYPE_CHECKING
910

10-
from pymatgen.core.structure import Structure
11+
if TYPE_CHECKING:
12+
from pymatgen.core import Structure
1113

1214

1315
def get_warren_cowley_parameters(structure: Structure, r: float, dr: float) -> dict[tuple, float]:

pymatgen/analysis/elasticity/elastic.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import itertools
1010
import warnings
11+
from typing import TYPE_CHECKING
1112

1213
import numpy as np
1314
import sympy as sp
@@ -17,7 +18,6 @@
1718

1819
from pymatgen.analysis.elasticity.strain import Strain
1920
from pymatgen.analysis.elasticity.stress import Stress
20-
from pymatgen.core.structure import Structure
2121
from pymatgen.core.tensors import (
2222
DEFAULT_QUAD,
2323
SquareTensor,
@@ -27,6 +27,10 @@
2727
)
2828
from pymatgen.core.units import Unit
2929

30+
if TYPE_CHECKING:
31+
from pymatgen.core import Structure
32+
33+
3034
__author__ = "Joseph Montoya"
3135
__copyright__ = "Copyright 2012, The Materials Project"
3236
__credits__ = "Maarten de Jong, Ian Winter, Shyam Dwaraknath, Mark Asta, Anubhav Jain"
@@ -308,7 +312,7 @@ def snyder_ac(self, structure):
308312
0.38483
309313
* avg_mass
310314
* ((self.long_v(structure) + 2.0 * self.trans_v(structure)) / 3.0) ** 3.0
311-
/ (300.0 * num_density ** (-2.0 / 3.0) * nsites ** (1.0 / 3.0))
315+
/ (300.0 * num_density ** (-2.0 / 3.0) * nsites ** (1 / 3))
312316
)
313317

314318
@raise_error_if_unphysical
@@ -329,7 +333,7 @@ def snyder_opt(self, structure):
329333
* (self.long_v(structure) + 2.0 * self.trans_v(structure))
330334
/ 3.0
331335
/ num_density ** (-2.0 / 3.0)
332-
* (1 - nsites ** (-1.0 / 3.0))
336+
* (1 - nsites ** (-1 / 3))
333337
)
334338

335339
@raise_error_if_unphysical
@@ -391,8 +395,8 @@ def debye_temperature(self, structure):
391395
"""
392396
v0 = structure.volume * 1e-30 / structure.num_sites
393397
vl, vt = self.long_v(structure), self.trans_v(structure)
394-
vm = 3 ** (1.0 / 3.0) * (1 / vl**3 + 2 / vt**3) ** (-1.0 / 3.0)
395-
td = 1.05457e-34 / 1.38065e-23 * vm * (6 * np.pi**2 / v0) ** (1.0 / 3.0)
398+
vm = 3 ** (1 / 3) * (1 / vl**3 + 2 / vt**3) ** (-1 / 3)
399+
td = 1.05457e-34 / 1.38065e-23 * vm * (6 * np.pi**2 / v0) ** (1 / 3)
396400
return td
397401

398402
@property

pymatgen/analysis/elasticity/strain.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@
88

99
import collections
1010
import itertools
11-
from typing import Literal
11+
from typing import TYPE_CHECKING, Literal
1212

1313
import numpy as np
1414
import scipy
1515

1616
from pymatgen.core.lattice import Lattice
17-
from pymatgen.core.structure import Structure
1817
from pymatgen.core.tensors import SquareTensor, symmetry_reduce
19-
from pymatgen.util.typing import ArrayLike
18+
19+
if TYPE_CHECKING:
20+
from numpy.typing import ArrayLike
21+
22+
from pymatgen.core.structure import Structure
2023

2124
__author__ = "Joseph Montoya"
2225
__copyright__ = "Copyright 2012, The Materials Project"

pymatgen/analysis/elasticity/stress.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def dev_principal_invariants(self):
5555
@property
5656
def von_mises(self):
5757
"""
58-
Returns the von mises stress
58+
Returns the von Mises stress
5959
"""
6060
if not self.is_symmetric():
6161
raise ValueError(
@@ -68,7 +68,7 @@ def mean_stress(self):
6868
"""
6969
Returns the mean stress
7070
"""
71-
return 1.0 / 3.0 * self.trace()
71+
return 1 / 3 * self.trace()
7272

7373
@property
7474
def deviator_stress(self):

pymatgen/analysis/elasticity/tests/test_stress.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def setUp(self):
1919
def test_properties(self):
2020
# mean_stress
2121
assert self.rand_stress.mean_stress == pytest.approx(
22-
1.0 / 3.0 * (self.rand_stress[0, 0] + self.rand_stress[1, 1] + self.rand_stress[2, 2])
22+
(self.rand_stress[0, 0] + self.rand_stress[1, 1] + self.rand_stress[2, 2]) / 3
2323
)
2424
assert self.symm_stress.mean_stress == pytest.approx(3.66)
2525
# deviator_stress
@@ -29,11 +29,7 @@ def test_properties(self):
2929
)
3030
self.assertArrayAlmostEqual(
3131
self.non_symm.deviator_stress,
32-
[
33-
[-0.2666666667, 0.2, 0.3],
34-
[0.4, 0.133333333, 0.6],
35-
[0.2, 0.5, 0.133333333],
36-
],
32+
[[-0.2666666667, 0.2, 0.3], [0.4, 0.133333333, 0.6], [0.2, 0.5, 0.133333333]],
3733
)
3834
# deviator_principal_invariants
3935
self.assertArrayAlmostEqual(self.symm_stress.dev_principal_invariants, [0, 44.2563, 111.953628])

0 commit comments

Comments
 (0)