Skip to content

Commit a9da16e

Browse files
DanielYang59janosh
andauthored
Re-enable some useful ruff rules (materialsproject#3813)
* remove COM812 * remove ISC001 * fix `RUF012` * enable `C408` * enable `PLC1901` * enable `PYI024` to fix partially * revert change for abiobjects * finish `PYI024` * remove `PERF203` no change to code * Revert "enable `C408`" This reverts commit 38a5aa7. * address materialsproject#3813 (comment) --------- Co-authored-by: Janosh Riebesell <janosh.riebesell@gmail.com>
1 parent d9e9ef6 commit a9da16e

33 files changed

+329
-241
lines changed

pymatgen/analysis/chemenv/coordination_environments/chemenv_strategies.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ class AbstractChemenvStrategy(MSONable, abc.ABC):
201201
"""
202202

203203
AC = AdditionalConditions()
204-
STRATEGY_OPTIONS: ClassVar[dict[str, dict]] = dict()
204+
STRATEGY_OPTIONS: ClassVar[dict[str, dict]] = {}
205205
STRATEGY_DESCRIPTION: str | None = None
206206
STRATEGY_INFO_FIELDS: ClassVar[list] = []
207207
DEFAULT_SYMMETRY_MEASURE_TYPE = "csm_wcs_ctwcc"
@@ -1640,11 +1640,11 @@ class SelfCSMNbSetWeight(NbSetWeight):
16401640

16411641
SHORT_NAME = "SelfCSMWeight"
16421642

1643-
DEFAULT_EFFECTIVE_CSM_ESTIMATOR = dict(
1643+
DEFAULT_EFFECTIVE_CSM_ESTIMATOR: ClassVar = dict(
16441644
function="power2_inverse_decreasing",
16451645
options={"max_csm": 8.0},
16461646
)
1647-
DEFAULT_WEIGHT_ESTIMATOR = dict(
1647+
DEFAULT_WEIGHT_ESTIMATOR: ClassVar = dict(
16481648
function="power2_decreasing_exp",
16491649
options={"max_csm": 8.0, "alpha": 1},
16501650
)
@@ -1743,12 +1743,12 @@ class DeltaCSMNbSetWeight(NbSetWeight):
17431743

17441744
SHORT_NAME = "DeltaCSMWeight"
17451745

1746-
DEFAULT_EFFECTIVE_CSM_ESTIMATOR = dict(
1746+
DEFAULT_EFFECTIVE_CSM_ESTIMATOR: ClassVar = dict(
17471747
function="power2_inverse_decreasing",
17481748
options={"max_csm": 8.0},
17491749
)
17501750
DEFAULT_SYMMETRY_MEASURE_TYPE = "csm_wcs_ctwcc"
1751-
DEFAULT_WEIGHT_ESTIMATOR = dict(
1751+
DEFAULT_WEIGHT_ESTIMATOR: ClassVar = dict(
17521752
function="smootherstep",
17531753
options={"delta_csm_min": 0.5, "delta_csm_max": 3.0},
17541754
)
@@ -2121,7 +2121,7 @@ class DistanceAngleAreaNbSetWeight(NbSetWeight):
21212121
SHORT_NAME = "DistAngleAreaWeight"
21222122

21232123
AC = AdditionalConditions()
2124-
DEFAULT_SURFACE_DEFINITION = dict(
2124+
DEFAULT_SURFACE_DEFINITION: ClassVar = dict(
21252125
type="standard_elliptic",
21262126
distance_bounds={"lower": 1.2, "upper": 1.8},
21272127
angle_bounds={"lower": 0.1, "upper": 0.8},
@@ -2644,7 +2644,7 @@ class WeightedNbSetChemenvStrategy(AbstractChemenvStrategy):
26442644
"""WeightedNbSetChemenvStrategy."""
26452645

26462646
STRATEGY_DESCRIPTION = " WeightedNbSetChemenvStrategy"
2647-
DEFAULT_CE_ESTIMATOR = dict(
2647+
DEFAULT_CE_ESTIMATOR: ClassVar = dict(
26482648
function="power2_inverse_power2_decreasing",
26492649
options={"max_csm": 8.0},
26502650
)
@@ -2948,7 +2948,7 @@ class MultiWeightsChemenvStrategy(WeightedNbSetChemenvStrategy):
29482948
# 'cn_map_delta_csm', 'cn_map_delta_csms_cn_map2', 'cn_map_delta_csm_weight',
29492949
# 'cn_map_cn_weight',
29502950
# 'cn_map_fraction', 'cn_map_ce_fraction', 'ce_fraction']
2951-
DEFAULT_CE_ESTIMATOR = dict(
2951+
DEFAULT_CE_ESTIMATOR: ClassVar = dict(
29522952
function="power2_inverse_power2_decreasing",
29532953
options={"max_csm": 8.0},
29542954
)

pymatgen/analysis/chemenv/coordination_environments/coordination_geometry_finder.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
from pymatgen.util.due import Doi, due
5050

5151
if TYPE_CHECKING:
52+
from typing import ClassVar
53+
5254
from typing_extensions import Self
5355

5456
__author__ = "David Waroquiers"
@@ -362,19 +364,19 @@ class LocalGeometryFinder:
362364
"""Main class used to find the local environments in a structure."""
363365

364366
DEFAULT_BVA_DISTANCE_SCALE_FACTOR = 1.0
365-
BVA_DISTANCE_SCALE_FACTORS = dict(
367+
BVA_DISTANCE_SCALE_FACTORS: ClassVar = dict(
366368
experimental=1.0,
367369
GGA_relaxed=1.015,
368370
LDA_relaxed=0.995,
369371
)
370-
DEFAULT_SPG_ANALYZER_OPTIONS = dict(symprec=1e-3, angle_tolerance=5)
372+
DEFAULT_SPG_ANALYZER_OPTIONS: ClassVar = dict(symprec=1e-3, angle_tolerance=5)
371373
STRUCTURE_REFINEMENT_NONE = "none"
372374
STRUCTURE_REFINEMENT_REFINED = "refined"
373375
STRUCTURE_REFINEMENT_SYMMETRIZED = "symmetrized"
374376

375377
DEFAULT_STRATEGY = MultiWeightsChemenvStrategy.stats_article_weights_parameters()
376378

377-
PRESETS = {
379+
PRESETS: ClassVar = {
378380
"DEFAULT": {
379381
"maximum_distance_factor": 2.0,
380382
"minimum_angle_factor": 0.05,

pymatgen/analysis/chemenv/utils/chemenv_config.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@
55
import json
66
from os import makedirs
77
from os.path import exists, expanduser
8+
from typing import TYPE_CHECKING
89

910
from pymatgen.analysis.chemenv.utils.scripts_utils import strategies_class_lookup
1011
from pymatgen.core import SETTINGS
1112

13+
if TYPE_CHECKING:
14+
from typing import ClassVar
15+
1216
__author__ = "David Waroquiers"
1317
__copyright__ = "Copyright 2012, The Materials Project"
1418
__credits__ = "Geoffroy Hautier"
@@ -25,7 +29,7 @@ class ChemEnvConfig:
2529
- Default options (strategies, ...).
2630
"""
2731

28-
DEFAULT_PACKAGE_OPTIONS = dict(
32+
DEFAULT_PACKAGE_OPTIONS: ClassVar = dict(
2933
default_strategy={
3034
"strategy": "SimplestChemenvStrategy",
3135
"strategy_options": {

pymatgen/analysis/chemenv/utils/defs_utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
from pymatgen.util.due import Doi, due
99

1010
if TYPE_CHECKING:
11+
from typing import ClassVar
12+
1113
from pymatgen.core import Structure
1214

1315

@@ -57,7 +59,7 @@ class AdditionalConditions:
5759
ONLY_ACB_AND_NO_E2SEB = ONLY_ANION_CATION_BONDS_AND_NO_ELEMENT_TO_SAME_ELEMENT_BONDS
5860
ONLY_E2OB = ONLY_ELEMENT_TO_OXYGEN_BONDS
5961
# Dictionary mapping of integer for the condition and its "description"
60-
CONDITION_DESCRIPTION = {
62+
CONDITION_DESCRIPTION: ClassVar = {
6163
NO_ADDITIONAL_CONDITION: "No additional condition",
6264
ONLY_ANION_CATION_BONDS: "Only anion-cation bonds",
6365
NO_ELEMENT_TO_SAME_ELEMENT_BONDS: "No element-element bonds (same elements)",

pymatgen/analysis/chemenv/utils/func_utils.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from __future__ import annotations
44

5-
from typing import TYPE_CHECKING, ClassVar
5+
from typing import TYPE_CHECKING
66

77
import numpy as np
88

@@ -15,6 +15,8 @@
1515
)
1616

1717
if TYPE_CHECKING:
18+
from typing import ClassVar
19+
1820
from typing_extensions import Self
1921

2022
__author__ = "David Waroquiers"
@@ -114,7 +116,7 @@ def from_dict(cls, dct: dict) -> Self:
114116
class RatioFunction(AbstractRatioFunction):
115117
"""Concrete implementation of a series of ratio functions."""
116118

117-
ALLOWED_FUNCTIONS = dict(
119+
ALLOWED_FUNCTIONS: ClassVar = dict(
118120
power2_decreasing_exp=["max", "alpha"],
119121
smoothstep=["lower", "upper"],
120122
smootherstep=["lower", "upper"],
@@ -227,7 +229,7 @@ class CSMFiniteRatioFunction(AbstractRatioFunction):
227229
D. Waroquiers et al., Acta Cryst. B 76, 683 (2020).
228230
"""
229231

230-
ALLOWED_FUNCTIONS = dict(
232+
ALLOWED_FUNCTIONS: ClassVar = dict(
231233
power2_decreasing_exp=["max_csm", "alpha"],
232234
smoothstep=["lower_csm", "upper_csm"],
233235
smootherstep=["lower_csm", "upper_csm"],
@@ -328,7 +330,7 @@ class CSMInfiniteRatioFunction(AbstractRatioFunction):
328330
D. Waroquiers et al., Acta Cryst. B 76, 683 (2020).
329331
"""
330332

331-
ALLOWED_FUNCTIONS = dict(
333+
ALLOWED_FUNCTIONS: ClassVar = dict(
332334
power2_inverse_decreasing=["max_csm"],
333335
power2_inverse_power2_decreasing=["max_csm"],
334336
)
@@ -419,7 +421,7 @@ class DeltaCSMRatioFunction(AbstractRatioFunction):
419421
D. Waroquiers et al., Acta Cryst. B 76, 683 (2020).
420422
"""
421423

422-
ALLOWED_FUNCTIONS = dict(smootherstep=["delta_csm_min", "delta_csm_max"])
424+
ALLOWED_FUNCTIONS: ClassVar = dict(smootherstep=["delta_csm_min", "delta_csm_max"])
423425

424426
def smootherstep(self, vals):
425427
"""Get the evaluation of the smootherstep ratio function: f(x)=6*x^5-15*x^4+10*x^3.

pymatgen/analysis/diffraction/tem.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
"""This module implements a TEM pattern calculator."""
1+
"""TEM pattern calculator."""
22

33
from __future__ import annotations
44

55
import json
66
import os
7-
from collections import namedtuple
87
from fractions import Fraction
9-
from typing import TYPE_CHECKING, cast
8+
from typing import TYPE_CHECKING, NamedTuple, cast
109

1110
import numpy as np
1211
import pandas as pd
@@ -18,6 +17,8 @@
1817
from pymatgen.util.string import latexify_spacegroup, unicodeify_spacegroup
1918

2019
if TYPE_CHECKING:
20+
from numpy.typing import NDArray
21+
2122
from pymatgen.core import Structure
2223

2324
__author__ = "Frank Wan, Jason Liang"
@@ -29,13 +30,13 @@
2930

3031

3132
module_dir = os.path.dirname(__file__)
32-
with open(f"{module_dir}/atomic_scattering_params.json") as file:
33+
with open(f"{module_dir}/atomic_scattering_params.json", encoding="utf-8") as file:
3334
ATOMIC_SCATTERING_PARAMS = json.load(file)
3435

3536

3637
class TEMCalculator(AbstractDiffractionPatternCalculator):
3738
"""
38-
Computes the TEM pattern of a crystal structure for multiple Laue zones.
39+
Compute the TEM pattern of a crystal structure for multiple Laue zones.
3940
Code partially inspired from XRD calculation implementation. X-ray factor to electron factor
4041
conversion based on the International Table of Crystallography.
4142
#TODO: Could add "number of iterations", "magnification", "critical value of beam",
@@ -520,13 +521,20 @@ def tem_dots(self, structure: Structure, points) -> list:
520521
Returns:
521522
list of TEM_dots
522523
"""
524+
525+
class dot(NamedTuple):
526+
position: NDArray
527+
hkl: tuple[int, int, int]
528+
intensity: float
529+
film_radius: float
530+
d_spacing: float
531+
523532
dots = []
524533
interplanar_spacings = self.get_interplanar_spacings(structure, points)
525534
bragg_angles = self.bragg_angles(interplanar_spacings)
526535
cell_intensity = self.normalized_cell_intensity(structure, bragg_angles)
527536
positions = self.get_positions(structure, points)
528537
for hkl, intensity in cell_intensity.items():
529-
dot = namedtuple("dot", ["position", "hkl", "intensity", "film_radius", "d_spacing"])
530538
position = positions[hkl]
531539
film_radius = 0.91 * (10**-3 * self.cs * self.wavelength_rel() ** 3) ** Fraction("1/4")
532540
d_spacing = interplanar_spacings[hkl]

pymatgen/analysis/eos.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
from pymatgen.util.plotting import add_fig_kwargs, get_ax_fig, pretty_plot
2020

2121
if TYPE_CHECKING:
22+
from typing import ClassVar
23+
2224
import matplotlib.pyplot as plt
2325

2426
__author__ = "Kiran Mathew, gmatteo"
@@ -532,7 +534,7 @@ class EOS:
532534
eos_fit.plot()
533535
"""
534536

535-
MODELS = dict(
537+
MODELS: ClassVar = dict(
536538
murnaghan=Murnaghan,
537539
birch=Birch,
538540
birch_murnaghan=BirchMurnaghan,

pymatgen/analysis/graphs.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
import os.path
88
import subprocess
99
import warnings
10-
from collections import defaultdict, namedtuple
10+
from collections import defaultdict
1111
from itertools import combinations
1212
from operator import itemgetter
1313
from shutil import which
14-
from typing import TYPE_CHECKING, cast
14+
from typing import TYPE_CHECKING, NamedTuple, cast
1515

1616
import networkx as nx
1717
import networkx.algorithms.isomorphism as iso
@@ -55,7 +55,13 @@
5555
__status__ = "Production"
5656
__date__ = "August 2017"
5757

58-
ConnectedSite = namedtuple("ConnectedSite", "site, jimage, index, weight, dist")
58+
59+
class ConnectedSite(NamedTuple):
60+
site: PeriodicSite
61+
jimage: tuple[int, int, int]
62+
index: Any # TODO: use more specific type
63+
weight: float
64+
dist: float
5965

6066

6167
def _compare(g1, g2, i1, i2) -> bool:

0 commit comments

Comments
 (0)