Skip to content

Commit ff94faa

Browse files
authored
Fix ruff FURB192 (#3785)
* tests/io/cp2k/test_sets.py rename TestSet->TestDftSet * f-strings * rename m->match and use walrus op * tlist -> temperatures * variable renames * ruff check . --select FURB192 --preview --fix * bump ruff, mypy, pyright pre-commit hooks * collections.defaultdict -> defaultdict everywhere
1 parent f52c921 commit ff94faa

Some content is hidden

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

71 files changed

+582
-593
lines changed

.pre-commit-config.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ ci:
88

99
repos:
1010
- repo: https://github.com/astral-sh/ruff-pre-commit
11-
rev: v0.4.1
11+
rev: v0.4.2
1212
hooks:
1313
- id: ruff
1414
args: [--fix, --unsafe-fixes]
@@ -22,7 +22,7 @@ repos:
2222
- id: trailing-whitespace
2323

2424
- repo: https://github.com/pre-commit/mirrors-mypy
25-
rev: v1.9.0
25+
rev: v1.10.0
2626
hooks:
2727
- id: mypy
2828

@@ -64,6 +64,6 @@ repos:
6464
args: [--drop-empty-cells, --keep-output]
6565

6666
- repo: https://github.com/RobertCraigie/pyright-python
67-
rev: v1.1.359
67+
rev: v1.1.360
6868
hooks:
6969
- id: pyright

dev_scripts/update_pt_data.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
from __future__ import annotations
77

8-
import collections
98
import json
109
import re
10+
from collections import defaultdict
1111
from itertools import product
1212

1313
import requests
@@ -154,7 +154,7 @@ def parse_shannon_radii():
154154
sheet = wb["Sheet1"]
155155
i = 2
156156
el = charge = cn = None
157-
radii = collections.defaultdict(dict)
157+
radii = defaultdict(dict)
158158
while sheet[f"E{i}"].value:
159159
if sheet[f"A{i}"].value:
160160
el = sheet[f"A{i}"].value
@@ -277,7 +277,7 @@ def add_ionization_energies():
277277
for table in soup.find_all("table"):
278278
if "Hydrogen" in table.text:
279279
break
280-
data = collections.defaultdict(list)
280+
data = defaultdict(list)
281281
for row in table.find_all("tr"):
282282
row = [td.get_text().strip() for td in row.find_all("td")]
283283
if row:

pymatgen/analysis/bond_valence.py

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

33
from __future__ import annotations
44

5-
import collections
65
import functools
76
import operator
87
import os
8+
from collections import defaultdict
99
from math import exp, sqrt
1010
from typing import TYPE_CHECKING
1111

@@ -279,7 +279,7 @@ def get_valences(self, structure: Structure):
279279
self._best_vset = None
280280

281281
def evaluate_assignment(v_set):
282-
el_oxi = collections.defaultdict(list)
282+
el_oxi = defaultdict(list)
283283
for idx, sites in enumerate(equi_sites):
284284
el_oxi[sites[0].specie.symbol].append(v_set[idx])
285285
max_diff = max(max(v) - min(v) for v in el_oxi.values())
@@ -346,7 +346,7 @@ def _recurse(assigned=None):
346346
self._best_vset = None
347347

348348
def evaluate_assignment(v_set):
349-
el_oxi = collections.defaultdict(list)
349+
el_oxi = defaultdict(list)
350350
jj = 0
351351
for sites in equi_sites:
352352
for specie, _ in get_z_ordered_elmap(sites[0].species):
@@ -474,7 +474,7 @@ def add_oxidation_state_by_site_fraction(structure, oxidation_states):
474474
"""
475475
try:
476476
for idx, site in enumerate(structure):
477-
new_sp = collections.defaultdict(float)
477+
new_sp = defaultdict(float)
478478
for j, (el, occu) in enumerate(get_z_ordered_elmap(site.species)):
479479
specie = Species(el.symbol, oxidation_states[idx][j])
480480
new_sp[specie] += occu

pymatgen/analysis/diffraction/core.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from __future__ import annotations
44

55
import abc
6-
import collections
6+
from collections import defaultdict
77
from typing import TYPE_CHECKING
88

99
import matplotlib.pyplot as plt
@@ -215,9 +215,9 @@ def get_unique_families(hkls):
215215
def is_perm(hkl1, hkl2) -> bool:
216216
h1 = np.abs(hkl1)
217217
h2 = np.abs(hkl2)
218-
return all(i == j for i, j in zip(sorted(h1), sorted(h2)))
218+
return np.all(np.sort(h1) == np.sort(h2))
219219

220-
unique = collections.defaultdict(list)
220+
unique = defaultdict(list)
221221
for hkl1 in hkls:
222222
found = False
223223
for hkl2, v2 in unique.items():
@@ -229,7 +229,7 @@ def is_perm(hkl1, hkl2) -> bool:
229229
unique[hkl1].append(hkl1)
230230

231231
pretty_unique = {}
232-
for v in unique.values():
233-
pretty_unique[sorted(v)[-1]] = len(v)
232+
for val in unique.values():
233+
pretty_unique[max(val)] = len(val)
234234

235235
return pretty_unique

pymatgen/analysis/diffraction/neutron.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ def get_pattern(self, structure: Structure, scaled=True, two_theta_range=(0, 90)
9898
)
9999

100100
# Obtain crystallographic reciprocal lattice points within range
101-
recip_latt = lattice.reciprocal_lattice_crystallographic
102-
recip_pts = recip_latt.get_points_in_sphere([[0, 0, 0]], [0, 0, 0], max_r)
101+
recip_lattice = lattice.reciprocal_lattice_crystallographic
102+
recip_pts = recip_lattice.get_points_in_sphere([[0, 0, 0]], [0, 0, 0], max_r)
103103
if min_r:
104104
recip_pts = [pt for pt in recip_pts if pt[1] >= min_r]
105105

@@ -129,7 +129,7 @@ def get_pattern(self, structure: Structure, scaled=True, two_theta_range=(0, 90)
129129
coeffs = np.array(_coeffs)
130130
frac_coords = np.array(_frac_coords)
131131
occus = np.array(_occus)
132-
dwfactors = np.array(_dwfactors)
132+
dw_factors = np.array(_dwfactors)
133133
peaks: dict[float, list[float | list[tuple[int, ...]]]] = {}
134134
two_thetas: list[float] = []
135135

@@ -147,7 +147,7 @@ def get_pattern(self, structure: Structure, scaled=True, two_theta_range=(0, 90)
147147
s = g_hkl / 2
148148

149149
# Calculate Debye-Waller factor
150-
dw_correction = np.exp(-dwfactors * (s**2))
150+
dw_correction = np.exp(-dw_factors * (s**2))
151151

152152
# Vectorized computation of g.r for all fractional coords and
153153
# hkl.

pymatgen/analysis/diffraction/xrd.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ def get_pattern(self, structure: Structure, scaled=True, two_theta_range=(0, 90)
159159
)
160160

161161
# Obtain crystallographic reciprocal lattice points within range
162-
recip_latt = lattice.reciprocal_lattice_crystallographic
163-
recip_pts = recip_latt.get_points_in_sphere([[0, 0, 0]], [0, 0, 0], max_r)
162+
recip_lattice = lattice.reciprocal_lattice_crystallographic
163+
recip_pts = recip_lattice.get_points_in_sphere([[0, 0, 0]], [0, 0, 0], max_r)
164164
if min_r:
165165
recip_pts = [pt for pt in recip_pts if pt[1] >= min_r]
166166

@@ -172,7 +172,7 @@ def get_pattern(self, structure: Structure, scaled=True, two_theta_range=(0, 90)
172172
_coeffs = []
173173
_frac_coords = []
174174
_occus = []
175-
_dwfactors = []
175+
_dw_factors = []
176176

177177
for site in structure:
178178
for sp, occu in site.species.items():
@@ -184,15 +184,15 @@ def get_pattern(self, structure: Structure, scaled=True, two_theta_range=(0, 90)
184184
f"Unable to calculate XRD pattern as there is no scattering coefficients for {sp.symbol}."
185185
)
186186
_coeffs.append(c)
187-
_dwfactors.append(self.debye_waller_factors.get(sp.symbol, 0))
187+
_dw_factors.append(self.debye_waller_factors.get(sp.symbol, 0))
188188
_frac_coords.append(site.frac_coords)
189189
_occus.append(occu)
190190

191191
zs = np.array(_zs)
192192
coeffs = np.array(_coeffs)
193193
frac_coords = np.array(_frac_coords)
194194
occus = np.array(_occus)
195-
dwfactors = np.array(_dwfactors)
195+
dw_factors = np.array(_dw_factors)
196196
peaks: dict[float, list[float | list[tuple[int, ...]]]] = {}
197197
two_thetas: list[float] = []
198198

@@ -227,7 +227,7 @@ def get_pattern(self, structure: Structure, scaled=True, two_theta_range=(0, 90)
227227
axis=1, # type: ignore
228228
)
229229

230-
dw_correction = np.exp(-dwfactors * s2)
230+
dw_correction = np.exp(-dw_factors * s2)
231231

232232
# Structure factor = sum of atomic scattering factors (with
233233
# position factor exp(2j * pi * g.r and occupancies).

pymatgen/analysis/dimensionality.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ def get_dimensionality_cheon(
347347
else:
348348
dim = np.log2(float(max3) / max1) / np.log2(3)
349349
if dim == int(dim):
350-
dim = str(int(dim)) + "D"
350+
dim = f"{int(dim)}D"
351351
else:
352352
return None
353353
else:
@@ -361,7 +361,7 @@ def get_dimensionality_cheon(
361361
else:
362362
dim = np.log2(float(max2) / max1)
363363
if dim == int(dim):
364-
dim = str(int(dim)) + "D"
364+
dim = f"{int(dim)}D"
365365
else:
366366
structure = copy.copy(structure_save)
367367
structure.make_supercell(np.eye(3) * 3)
@@ -372,7 +372,7 @@ def get_dimensionality_cheon(
372372
else:
373373
dim = np.log2(float(max3) / max1) / np.log2(3)
374374
if dim == int(dim):
375-
dim = str(int(dim)) + "D"
375+
dim = f"{int(dim)}D"
376376
else:
377377
return None
378378
return dim

pymatgen/analysis/disorder.py

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

33
from __future__ import annotations
44

5-
import collections
65
import itertools
6+
from collections import defaultdict
77
from typing import TYPE_CHECKING
88

99
if TYPE_CHECKING:
@@ -24,8 +24,8 @@ def get_warren_cowley_parameters(structure: Structure, r: float, dr: float) -> d
2424
"""
2525
comp = structure.composition
2626

27-
n_ij = collections.defaultdict(int) # type: ignore
28-
n_neighbors = collections.defaultdict(int) # type: ignore
27+
n_ij = defaultdict(int) # type: ignore
28+
n_neighbors = defaultdict(int) # type: ignore
2929
for site in structure:
3030
for nn in structure.get_neighbors_in_shell(site.coords, r, dr):
3131
n_ij[(site.specie, nn.specie)] += 1

pymatgen/analysis/nmr.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ def principal_axis_system(self):
165165
def V_xx(self):
166166
"""Returns: First diagonal element."""
167167
diags = np.diag(self.principal_axis_system)
168-
return sorted(diags, key=np.abs)[0]
168+
return min(diags, key=np.abs)
169169

170170
@property
171171
def V_yy(self):

pymatgen/analysis/phase_diagram.py

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

33
from __future__ import annotations
44

5-
import collections
65
import itertools
76
import json
87
import logging
98
import math
109
import os
1110
import re
1211
import warnings
12+
from collections import defaultdict
1313
from functools import lru_cache
1414
from typing import TYPE_CHECKING, Any, Literal, no_type_check
1515

@@ -1181,7 +1181,7 @@ def get_chempot_range_map(
11811181
else:
11821182
el_energies = dict.fromkeys(elements, 0)
11831183

1184-
chempot_ranges = collections.defaultdict(list)
1184+
chempot_ranges = defaultdict(list)
11851185
vertices = [list(range(len(self.elements)))]
11861186

11871187
if len(all_chempots) > len(self.elements):
@@ -2631,7 +2631,7 @@ def _create_plotly_figure_layout(self, label_stable=True):
26312631
if hasattr(el_ref, "original_entry"): # for grand potential PDs, etc.
26322632
clean_formula = htmlify(el_ref.original_entry.reduced_formula)
26332633

2634-
layout["ternary"][axis + "axis"]["title"] = {
2634+
layout["ternary"][f"{axis}axis"]["title"] = {
26352635
"text": clean_formula,
26362636
"font": {"size": 24},
26372637
}

0 commit comments

Comments
 (0)