Skip to content

Commit 1d5c5e9

Browse files
authored
Remove unused type ignores (#328)
* remove unused type ignores * some more * fix circular import * bot comments * fix valid EmbeddingMethod check
1 parent f338804 commit 1d5c5e9

40 files changed

+256
-196
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ default_install_hook_types: [pre-commit, commit-msg]
88

99
repos:
1010
- repo: https://github.com/astral-sh/ruff-pre-commit
11-
rev: v0.14.3
11+
rev: v0.14.4
1212
hooks:
1313
- id: ruff-check
1414
args: [--fix]
@@ -80,7 +80,7 @@ repos:
8080
- --fix
8181

8282
- repo: https://github.com/pre-commit/mirrors-eslint
83-
rev: v9.39.0
83+
rev: v9.39.1
8484
hooks:
8585
- id: eslint
8686
types: [file]

assets/scripts/cluster/composition/cluster_compositions_matbench.py

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@
2525

2626
import pymatviz as pmv
2727
import pymatviz.cluster.composition as pcc
28+
from pymatviz.cluster.composition import EmbeddingMethod as Embed
29+
from pymatviz.cluster.composition import ProjectionMethod as Project
2830
from pymatviz.enums import Key
2931

3032

3133
if TYPE_CHECKING:
3234
import plotly.graph_objects as go
3335

34-
from pymatviz.cluster.composition import ProjectionMethod
35-
3636

3737
pmv.set_plotly_template("pymatviz_white")
3838
module_dir = os.path.dirname(__file__)
@@ -61,7 +61,7 @@ def process_dataset(
6161
target_label: str,
6262
target_symbol: str,
6363
embed_method: pcc.EmbeddingMethod,
64-
projection: ProjectionMethod,
64+
projection: pcc.ProjectionMethod,
6565
n_components: int,
6666
**kwargs: Any,
6767
) -> go.Figure:
@@ -110,7 +110,7 @@ def process_dataset(
110110
# Create embeddings
111111
if embed_method == "one-hot":
112112
embeddings = pcc.one_hot_encode(compositions)
113-
elif embed_method in ["magpie", "matscholar_el"]:
113+
elif embed_method in (Embed.magpie, Embed.matscholar_el):
114114
embeddings = pcc.matminer_featurize(compositions, preset=embed_method)
115115
else:
116116
raise ValueError(f"Unknown {embed_method=}")
@@ -144,14 +144,15 @@ def annotate_top_points(row: pd.Series) -> dict[str, Any] | None:
144144
text = f"{comp_str}<br>{prop_val}"
145145
return dict(text=text, font_size=11, bgcolor="rgba(240, 240, 240, 0.5)")
146146

147-
if "embeddings" not in df_plot:
148-
df_plot["embeddings"] = [embeddings_dict.get(comp) for comp in compositions]
147+
embed_col = "embeddings"
148+
if embed_col not in df_plot:
149+
df_plot[embed_col] = [embeddings_dict.get(comp) for comp in compositions]
149150

150151
fig = pmv.cluster_compositions(
151152
df_in=df_plot,
152153
composition_col="composition",
153154
prop_name=target_label,
154-
embedding_method="embeddings",
155+
embedding_method=embed_col,
155156
projection=projection,
156157
n_components=n_components,
157158
marker_size=8,
@@ -185,68 +186,72 @@ def annotate_top_points(row: pd.Series) -> dict[str, Any] | None:
185186
return fig
186187

187188

188-
mb_jdft2d = (
189+
mb_jdft2d: tuple[str, str, str, str] = (
189190
"matbench_jdft2d",
190191
"exfoliation_en",
191192
"Exfoliation Energy (meV/atom)",
192193
"E<sub>ex</sub>",
193194
)
194-
mb_steels = (
195+
mb_steels: tuple[str, str, str, str] = (
195196
"matbench_steels",
196197
"yield strength",
197198
"Yield Strength (MPa)",
198199
"σ",
199200
)
200-
mb_dielectric = (
201+
mb_dielectric: tuple[str, str, str, str] = (
201202
"matbench_dielectric",
202203
"n",
203204
"Refractive index",
204205
"n",
205206
)
206-
mb_perovskites = (
207+
mb_perovskites: tuple[str, str, str, str] = (
207208
"matbench_perovskites",
208209
"e_form",
209210
"Formation energy (eV/atom)",
210211
"E<sub>f</sub>",
211212
)
212-
mb_phonons = (
213+
mb_phonons: tuple[str, str, str, str] = (
213214
"matbench_phonons",
214215
"last phdos peak",
215216
"Max Phonon Peak (cm⁻¹)",
216217
"ν<sub>max</sub>",
217218
)
218-
mb_bulk_modulus = (
219+
mb_bulk_modulus: tuple[str, str, str, str] = (
219220
"matbench_log_kvrh",
220221
"log10(K_VRH)",
221222
"Bulk Modulus (GPa)",
222223
"K<sub>VRH</sub>",
223224
)
224225
plot_combinations: list[ # type: ignore[invalid-assignment]
225-
tuple[
226-
str, str, str, str, pcc.EmbeddingMethod, ProjectionMethod, int, dict[str, Any]
227-
]
226+
tuple[str, str, str, str, Embed, Project, int, dict[str, Any]]
228227
] = [
229228
# 1. Steels with PCA (2D) - shows clear linear trends
230-
(*mb_steels, "magpie", "pca", 2, dict(x=0.01, xanchor="left")),
229+
(*mb_steels, Embed.magpie, Project.pca, 2, dict(x=0.01, xanchor="left")),
231230
# 2. Steels with t-SNE (2D) - shows non-linear clustering
232-
(*mb_steels, "magpie", "tsne", 2, dict(x=0.01, xanchor="left")),
231+
(*mb_steels, Embed.magpie, Project.tsne, 2, dict(x=0.01, xanchor="left")),
233232
# TODO umap-learn seemingly not installed by uv run in CI, fix later
234233
# 3. JDFT2D with UMAP (2D) - shows modern non-linear projection
235-
# (*mb_jdft2d, "magpie", "umap", 2, dict(x=0.01, xanchor="left")),
234+
# (*mb_jdft2d, Embed.magpie, Project.umap, 2, dict(x=0.01, xanchor="left")),
236235
# 4. JDFT2D with one-hot encoding and PCA (3D) - shows raw element relationships
237-
(*mb_jdft2d, "one-hot", "pca", 3, dict()),
236+
(*mb_jdft2d, Embed.one_hot, Project.pca, 3, dict()),
238237
# 5. Steels with Matscholar embedding and t-SNE (3D) - shows advanced embedding
239-
(*mb_steels, "matscholar_el", "tsne", 3, dict(x=0.5, y=0.8)),
238+
(*mb_steels, Embed.matscholar_el, Project.tsne, 3, dict(x=0.5, y=0.8)),
240239
# 6. Dielectric with PCA (2D) - shows clear linear trends
241-
(*mb_dielectric, "magpie", "pca", 2, dict(x=0.01, xanchor="left")),
240+
(*mb_dielectric, Embed.magpie, Project.pca, 2, dict(x=0.01, xanchor="left")),
242241
# 7. Perovskites with PCA (2D) - shows clear linear trends
243-
(*mb_perovskites, "magpie", "pca", 2, dict(x=0.01, xanchor="left")),
242+
(*mb_perovskites, Embed.magpie, Project.pca, 2, dict(x=0.01, xanchor="left")),
244243
# 8. Phonons with PCA (2D) - shows clear linear trends
245-
(*mb_phonons, "magpie", "pca", 2, dict(x=0.01, xanchor="left")),
244+
(*mb_phonons, Embed.magpie, Project.pca, 2, dict(x=0.01, xanchor="left")),
246245
# 9. Bulk Modulus with PCA (2D) - shows clear linear trends
247-
(*mb_bulk_modulus, "magpie", "pca", 2, dict(x=0.99, y=0.96, yanchor="top")),
246+
(
247+
*mb_bulk_modulus,
248+
Embed.magpie,
249+
Project.pca,
250+
2,
251+
dict(x=0.99, y=0.96, yanchor="top"),
252+
),
248253
# 10. Perovskites with t-SNE (3D) - shows raw element relationships
249-
(*mb_perovskites, "magpie", "tsne", 3, dict()),
254+
(*mb_perovskites, Embed.magpie, Project.tsne, 3, dict()),
250255
]
251256

252257
for (

assets/scripts/ptable/ptable_heatmap_splits_plotly.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
cbar_title = f"Periodic Table Heatmap with {n_splits}-fold split"
3232
fig = pmv.ptable_heatmap_splits_plotly(
3333
data=data_dict,
34-
orientation=orientation, # type: ignore[arg-type]
34+
orientation=orientation,
3535
colorscale="RdYlBu", # Single colorscale will be used for all splits
3636
colorbar=dict(title=cbar_title),
3737
)
@@ -44,7 +44,7 @@
4444
]
4545
fig = pmv.ptable_heatmap_splits_plotly(
4646
data=data_dict,
47-
orientation=orientation, # type: ignore[arg-type]
47+
orientation=orientation,
4848
colorscale=colorscales,
4949
colorbar=colorbars,
5050
)
@@ -63,7 +63,7 @@
6363
]
6464
fig = pmv.ptable_heatmap_splits_plotly(
6565
data=data_dict,
66-
orientation=orientation, # type: ignore[arg-type]
66+
orientation=orientation,
6767
colorscale=sequential_colors,
6868
colorbar=colorbars,
6969
)

assets/scripts/ptable/ptable_scatter_plotly.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@
7070
),
7171
]:
7272
fig = pmv.ptable_scatter_plotly(
73-
elem_data_dict, # type: ignore[arg-type]
73+
elem_data_dict,
7474
mode=mode, # type: ignore[arg-type]
75-
line_kwargs=line_kwargs, # type: ignore[arg-type]
76-
color_elem_strategy=color_strategy, # type: ignore[arg-type]
75+
line_kwargs=line_kwargs,
76+
color_elem_strategy=color_strategy,
7777
scale=1.2,
7878
marker_kwargs=marker_kwargs,
7979
symbol_kwargs=symbol_kwargs,

assets/scripts/track_pymatviz_citations.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,10 @@ def update_readme(
238238
"""Update readme with papers sorted by citations and year."""
239239
sorted_papers = sorted(
240240
papers,
241-
key=lambda p: (-p["citations"], -p.get("year") or float("inf")), # type: ignore[operator]
241+
key=lambda p: (
242+
-p["citations"],
243+
float("inf") if p.get("year") is None else -p["year"],
244+
),
242245
)
243246

244247
with open(readme_path, encoding="utf-8") as file:

assets/scripts/treemap/py_pkg_treemap.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
}.items():
5555
fig = pmv.py_pkg_treemap(
5656
"pymatviz",
57-
group_by=group_by, # type: ignore[arg-type]
57+
group_by=group_by,
5858
show_counts="value",
5959
color_discrete_sequence=clr_scheme,
6060
# Only include files with at least 20 lines
@@ -124,7 +124,7 @@ def custom_module_formatter(module: str, count: int, _total: int) -> str:
124124
coverage_data_file = "https://github.com/user-attachments/files/21545087/2025-07-31-pymatgen-coverage.json"
125125
# coverage_data_file = f"{pmv.ROOT}/tmp/2025-07-31-pymatgen-coverage.json"
126126

127-
fig_coverage_range = pmv.py_pkg_treemap(
127+
fig_cvrg_range = pmv.py_pkg_treemap(
128128
pymatgen,
129129
color_by="coverage",
130130
coverage_data_file=coverage_data_file, # Use existing coverage data
@@ -133,7 +133,8 @@ def custom_module_formatter(module: str, count: int, _total: int) -> str:
133133
show_counts="value",
134134
color_continuous_scale="RdYlGn", # Red-Yellow-Green scale for coverage
135135
)
136-
title_range = "pymatgen: Coverage Heatmap with Manual Range (0-100%)"
137-
fig_coverage_range.layout.title.update(text=title_range, x=0.5, y=0.97, font_size=18)
138-
fig_coverage_range.show()
139-
pmv.io.save_and_compress_svg(fig_coverage_range, "py-pkg-treemap-pymatgen-coverage")
136+
# title_range = "pymatgen: Coverage Heatmap with Manual Range (0-100%)"
137+
# fig_cvrg_range.layout.title.update(text=title_range, x=0.5, y=0.97, font_size=18)
138+
fig_cvrg_range.layout.margin = dict(l=0, r=0, b=0, t=0)
139+
fig_cvrg_range.show()
140+
pmv.io.save_and_compress_svg(fig_cvrg_range, "py-pkg-treemap-pymatgen-coverage")

pymatviz/brillouin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,9 @@ def brillouin_zone_3d(
300300
if subplot_title is not False:
301301
title_func = subplot_title or get_subplot_title
302302
if title_func is get_subplot_title:
303-
anno = title_func(structure, struct_key, idx, subplot_title) # type: ignore[call-arg]
303+
anno = title_func(structure, struct_key, idx, subplot_title)
304304
else:
305-
anno = title_func(structure, struct_key) # type: ignore[call-arg]
305+
anno = title_func(structure, struct_key)
306306
if not isinstance(anno, (str, dict)):
307307
raise TypeError("Subplot title must be a string or dict")
308308
if isinstance(anno, str):

pymatviz/classify/confusion_matrix.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
def confusion_matrix(
1111
conf_mat: Sequence[Sequence[int]] | np.ndarray | None = None,
1212
*,
13-
y_true: Sequence[str | int] | None = None,
14-
y_pred: Sequence[str | int] | None = None,
13+
y_true: Sequence[str | int] | np.ndarray | None = None,
14+
y_pred: Sequence[str | int] | np.ndarray | None = None,
1515
x_labels: tuple[str, ...] | None = None,
1616
y_labels: tuple[str, ...] | None = None,
1717
annotations: Sequence[Sequence[str]]
@@ -227,9 +227,9 @@ def confusion_matrix(
227227
font_size=22,
228228
) | (metrics_kwargs or {})
229229
fig.add_annotation(**metrics_defaults)
230-
if metrics_defaults.get("y", 0) >= 1: # type: ignore[operator]
230+
if metrics_defaults.get("y", 0) >= 1:
231231
fig.layout.margin.t = 60
232-
if metrics_defaults.get("y", 0) <= 0: # type: ignore[operator]
232+
if metrics_defaults.get("y", 0) <= 0:
233233
fig.layout.margin.b = 60
234234

235235
# Update axes formatting

pymatviz/classify/curves.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,13 @@ def _standardize_input(
4444
f"when passing a DataFrame, targets must be a column name, got "
4545
f"{type(targets).__name__}"
4646
)
47-
targets, probs_positive = df_to_arrays(df, targets, probs_positive) # type: ignore[arg-type]
47+
if isinstance(probs_positive, dict):
48+
raise TypeError(
49+
f"when passing a DataFrame, probs_positive must be a column name "
50+
f"(str) or array, not dict. Got {type(probs_positive).__name__}. "
51+
f"Pass df=None to use dict of predictions."
52+
)
53+
targets, probs_positive = df_to_arrays(df, targets, probs_positive)
4854

4955
if isinstance(probs_positive, dict):
5056
# Convert array values to dicts if needed

pymatviz/cluster/composition/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from pymatviz.cluster.composition.embed import matminer_featurize, one_hot_encode
88
from pymatviz.cluster.composition.plot import (
99
EmbeddingMethod,
10+
ProjectionCallable,
1011
ProjectionMethod,
1112
cluster_compositions,
1213
)

0 commit comments

Comments
 (0)