Skip to content

Commit 685fc6e

Browse files
committed
type test functions, update commit hooks incl. mypy, setup.cfg set implicit_optional=true which defaults to false in newer mypys
github.com/python/mypy/pull/13401
1 parent c428fe5 commit 685fc6e

File tree

8 files changed

+58
-41
lines changed

8 files changed

+58
-41
lines changed

.pre-commit-config.yaml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,29 @@ default_install_hook_types: [pre-commit, commit-msg]
77

88
repos:
99
- repo: https://github.com/PyCQA/isort
10-
rev: 5.10.1
10+
rev: 5.11.4
1111
hooks:
1212
- id: isort
1313

1414
- repo: https://github.com/psf/black
15-
rev: 22.8.0
15+
rev: 22.12.0
1616
hooks:
1717
- id: black-jupyter
1818

1919
- repo: https://github.com/PyCQA/flake8
20-
rev: 5.0.4
20+
rev: 6.0.0
2121
hooks:
2222
- id: flake8
2323
additional_dependencies: [flake8-bugbear]
2424

2525
- repo: https://github.com/asottile/pyupgrade
26-
rev: v2.38.2
26+
rev: v3.3.1
2727
hooks:
2828
- id: pyupgrade
2929
args: [--py38-plus]
3030

3131
- repo: https://github.com/pre-commit/mirrors-mypy
32-
rev: v0.981
32+
rev: v0.991
3333
hooks:
3434
- id: mypy
3535
additional_dependencies: [types-requests]
@@ -40,7 +40,7 @@ repos:
4040
- id: format-ipy-cells
4141

4242
- repo: https://github.com/pre-commit/pre-commit-hooks
43-
rev: v4.3.0
43+
rev: v4.4.0
4444
hooks:
4545
- id: check-case-conflict
4646
- id: check-symlinks
@@ -52,14 +52,14 @@ repos:
5252
- id: trailing-whitespace
5353

5454
- repo: https://github.com/codespell-project/codespell
55-
rev: v2.2.1
55+
rev: v2.2.2
5656
hooks:
5757
- id: codespell
5858
stages: [commit, commit-msg]
5959
exclude_types: [csv, svg, html, yaml, jupyter]
6060

6161
- repo: https://github.com/PyCQA/autoflake
62-
rev: v1.6.1
62+
rev: v2.0.0
6363
hooks:
6464
- id: autoflake
6565

@@ -70,12 +70,12 @@ repos:
7070
exclude: tests
7171

7272
- repo: https://github.com/PyCQA/docformatter
73-
rev: v1.5.0
73+
rev: v1.5.1
7474
hooks:
7575
- id: docformatter
7676

7777
- repo: https://github.com/nbQA-dev/nbQA
78-
rev: 1.5.2
78+
rev: 1.5.3
7979
hooks:
8080
- id: nbqa-pyupgrade
8181
args: [--py38-plus]

pymatviz/histograms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ def hist_elemental_prevalence(
286286
log: bool = False,
287287
keep_top: int = None,
288288
ax: plt.Axes = None,
289-
bar_values: Literal["percent", "count", None] = "percent",
289+
bar_values: Literal["percent", "count"] | None = "percent",
290290
h_offset: int = 0,
291291
v_offset: int = 10,
292292
rotation: int = 45,

pymatviz/ptable.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def ptable_heatmap(
117117
zero_color: str = "#DDD", # light gray
118118
infty_color: str = "lightskyblue",
119119
na_color: str = "white",
120-
heat_mode: Literal["value", "fraction", "percent", None] = "value",
120+
heat_mode: Literal["value", "fraction", "percent"] | None = "value",
121121
precision: str = None,
122122
text_color: str | tuple[str, str] = "auto",
123123
exclude_elements: Sequence[str] = (),
@@ -367,7 +367,7 @@ def ptable_heatmap_plotly(
367367
count_mode: CountMode = "element_composition",
368368
colorscale: str | Sequence[str] | Sequence[tuple[float, str]] | None = None,
369369
showscale: bool = True,
370-
heat_mode: Literal["value", "fraction", "percent", None] = "value",
370+
heat_mode: Literal["value", "fraction", "percent"] | None = "value",
371371
precision: str = None,
372372
hover_props: Sequence[str] | dict[str, str] | None = None,
373373
hover_data: dict[str, str | int | float] | pd.Series | None = None,
@@ -418,7 +418,7 @@ def ptable_heatmap_plotly(
418418
hover_data (dict[str, str | int | float] | pd.Series): Map from element symbols
419419
to additional data to display in the hover tooltip. {"Fe": "this shows up in
420420
the hover tooltip on a new line below the element name"}. Defaults to None.
421-
font_colors (list[str]): One or two color strings [min_color, max_color].
421+
font_colors (list[str]): One color name or two for [min_color, max_color].
422422
min_color is applied to annotations for heatmap values
423423
< (max_val - min_val) / 2. Defaults to ["black"].
424424
gap (float): Gap in pixels between tiles of the periodic table. Defaults to 5.

readme.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ A toolkit for visualizations in materials informatics.
66

77
[![Tests](https://github.com/janosh/pymatviz/actions/workflows/test.yml/badge.svg)](https://github.com/janosh/pymatviz/actions/workflows/test.yml)
88
[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/janosh/pymatviz/main.svg)](https://results.pre-commit.ci/latest/github/janosh/pymatviz/main)
9-
[![This project supports Python 3.8+](https://img.shields.io/badge/Python-3.8+-blue.svg?logo=python)](https://python.org/downloads)
10-
[![PyPI](https://img.shields.io/pypi/v/pymatviz?logo=PyPI)](https://pypi.org/project/pymatviz)
11-
[![PyPI Downloads](https://img.shields.io/pypi/dm/pymatviz)](https://pypistats.org/packages/pymatviz)
9+
[![This project supports Python 3.8+](https://img.shields.io/badge/Python-3.8+-blue.svg?logo=python&logoColor=white)](https://python.org/downloads)
10+
[![PyPI](https://img.shields.io/pypi/v/pymatviz?logo=pypi&logoColor=white)](https://pypi.org/project/pymatviz)
11+
[![PyPI Downloads](https://img.shields.io/pypi/dm/pymatviz?logo=icloud&logoColor=white)](https://pypistats.org/packages/pymatviz)
1212

1313
</h4>
1414

setup.cfg

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,7 @@ disallow_untyped_defs = true
7575
warn_redundant_casts = true
7676
warn_unused_ignores = true
7777
show_error_codes = true
78-
79-
[mypy-tests.*]
80-
disallow_untyped_defs = false
78+
implicit_optional = true
8179

8280
[codespell]
8381
ignore-words-list = hist

tests/test_cumulative.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def test_cumulative_error(alpha: float) -> None:
1313
assert isinstance(ax, plt.Axes)
1414

1515

16-
def test_cumulative_residual():
16+
def test_cumulative_residual() -> None:
1717
ax = cumulative_residual(abs(y_true - y_pred))
1818
assert isinstance(ax, plt.Axes)
1919
assert len(ax.lines) == 3

tests/test_ptable.py

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import annotations
22

3+
from typing import TYPE_CHECKING, Literal, Sequence
4+
35
import matplotlib.pyplot as plt
46
import pandas as pd
57
import plotly.graph_objects as go
@@ -17,6 +19,10 @@
1719
from pymatviz.utils import df_ptable
1820

1921

22+
if TYPE_CHECKING:
23+
from pymatviz.ptable import CountMode
24+
25+
2026
@pytest.fixture
2127
def glass_formulas() -> list[str]:
2228
"""Output of:
@@ -26,10 +32,10 @@ def glass_formulas() -> list[str]:
2632
load_dataset("matbench_glass").composition.head(20)
2733
"""
2834
return (
29-
"Al,Al(NiB)2,Al10Co21B19,Al10Co23B17,Al10Co27B13,Al10Co29B11,Al10Co31B9,"
30-
"Al10Co33B7,Al10Cr3Si7,Al10Fe23B17,Al10Fe27B13,Al10Fe31B9,Al10Fe33B7,"
31-
"Al10Ni23B17,Al10Ni27B13,Al10Ni29B11,Al10Ni31B9,Al10Ni33B7,Al11(CrSi2)3"
32-
).split(",")
35+
"Al Al(NiB)2 Al10Co21B19 Al10Co23B17 Al10Co27B13 Al10Co29B11 Al10Co31B9 "
36+
"Al10Co33B7 Al10Cr3Si7 Al10Fe23B17 Al10Fe27B13 Al10Fe31B9 Al10Fe33B7 "
37+
"Al10Ni23B17 Al10Ni27B13 Al10Ni29B11 Al10Ni31B9 Al10Ni33B7 Al11(CrSi2)3"
38+
).split()
3339

3440

3541
@pytest.fixture
@@ -64,13 +70,13 @@ def steel_elem_counts(steel_formulas: pd.Series[Composition]) -> pd.Series[int]:
6470
("reduced_composition", {"Fe": 13, "O": 27, "P": 3}),
6571
],
6672
)
67-
def test_count_elements(count_mode, counts):
73+
def test_count_elements(count_mode: CountMode, counts: dict[str, float]) -> None:
6874
series = count_elements(["Fe2 O3"] * 5 + ["Fe4 P4 O16"] * 3, count_mode=count_mode)
6975
expected = pd.Series(counts, index=df_ptable.index, name="count").fillna(0)
7076
assert series.equals(expected)
7177

7278

73-
def test_count_elements_by_atomic_nums():
79+
def test_count_elements_by_atomic_nums() -> None:
7480
series_in = pd.Series(1, index=range(1, 119))
7581
el_cts = count_elements(series_in)
7682
expected = pd.Series(1, index=df_ptable.index, name="count")
@@ -79,7 +85,7 @@ def test_count_elements_by_atomic_nums():
7985

8086

8187
@pytest.mark.parametrize("range_limits", [(-1, 10), (100, 200)])
82-
def test_count_elements_bad_atomic_nums(range_limits):
88+
def test_count_elements_bad_atomic_nums(range_limits: tuple[int, int]) -> None:
8389
with pytest.raises(ValueError, match="assumed to represent atomic numbers"):
8490
count_elements({idx: 0 for idx in range(*range_limits)})
8591

@@ -88,7 +94,7 @@ def test_count_elements_bad_atomic_nums(range_limits):
8894
count_elements({str(idx): 0 for idx in range(*range_limits)})
8995

9096

91-
def test_hist_elemental_prevalence(glass_formulas):
97+
def test_hist_elemental_prevalence(glass_formulas: list[str]) -> None:
9298
ax = hist_elemental_prevalence(glass_formulas)
9399
assert isinstance(ax, plt.Axes)
94100

@@ -99,7 +105,9 @@ def test_hist_elemental_prevalence(glass_formulas):
99105
hist_elemental_prevalence(glass_formulas, keep_top=10, bar_values="count")
100106

101107

102-
def test_ptable_heatmap(glass_formulas, glass_elem_counts):
108+
def test_ptable_heatmap(
109+
glass_formulas: list[str], glass_elem_counts: pd.Series[int]
110+
) -> None:
103111
ax = ptable_heatmap(glass_formulas)
104112
assert isinstance(ax, plt.Axes)
105113

@@ -139,8 +147,11 @@ def test_ptable_heatmap(glass_formulas, glass_elem_counts):
139147

140148

141149
def test_ptable_heatmap_ratio(
142-
steel_formulas, glass_formulas, steel_elem_counts, glass_elem_counts
143-
):
150+
steel_formulas: list[str],
151+
glass_formulas: list[str],
152+
steel_elem_counts: pd.Series[int],
153+
glass_elem_counts: pd.Series[int],
154+
) -> None:
144155
# composition strings
145156
ax = ptable_heatmap_ratio(glass_formulas, steel_formulas)
146157
assert isinstance(ax, plt.Axes)
@@ -153,7 +164,7 @@ def test_ptable_heatmap_ratio(
153164
ptable_heatmap_ratio(glass_elem_counts, steel_formulas)
154165

155166

156-
def test_ptable_heatmap_plotly(glass_formulas):
167+
def test_ptable_heatmap_plotly(glass_formulas: list[str]) -> None:
157168
fig = ptable_heatmap_plotly(glass_formulas)
158169
assert isinstance(fig, go.Figure)
159170
assert (
@@ -194,10 +205,16 @@ def test_ptable_heatmap_plotly(glass_formulas):
194205
)
195206
@pytest.mark.parametrize("showscale", [False, True])
196207
@pytest.mark.parametrize("font_size", [None, 14])
197-
@pytest.mark.parametrize("font_colors", [None, ("black", "white")])
208+
@pytest.mark.parametrize("font_colors", [["red"], ("black", "white")])
198209
def test_ptable_heatmap_plotly_kwarg_combos(
199-
glass_formulas, exclude_elements, heat_mode, showscale, font_size, font_colors, log
200-
):
210+
glass_formulas: list[str],
211+
exclude_elements: Sequence[str],
212+
heat_mode: Literal["value", "fraction", "percent"] | None,
213+
showscale: bool,
214+
font_size: int,
215+
font_colors: tuple[str] | tuple[str, str],
216+
log: bool,
217+
) -> None:
201218
fig = ptable_heatmap_plotly(
202219
glass_formulas,
203220
exclude_elements=exclude_elements,
@@ -211,8 +228,10 @@ def test_ptable_heatmap_plotly_kwarg_combos(
211228

212229

213230
@pytest.mark.parametrize(
214-
"clr_scl", ["YlGn", ["blue", "red"], [(0, "blue"), (1, "red")]]
231+
"colorscale", ["YlGn", ["blue", "red"], [(0, "blue"), (1, "red")]]
215232
)
216-
def test_ptable_heatmap_plotly_colorscale(glass_formulas, clr_scl):
217-
fig = ptable_heatmap_plotly(glass_formulas, colorscale=clr_scl)
233+
def test_ptable_heatmap_plotly_colorscale(
234+
glass_formulas: list[str], colorscale: str | list[tuple[float, str]] | list[str]
235+
) -> None:
236+
fig = ptable_heatmap_plotly(glass_formulas, colorscale=colorscale)
218237
assert isinstance(fig, go.Figure)

tests/test_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from tests.conftest import y_pred, y_true
1616

1717

18-
def test_add_mae_r2_box():
18+
def test_add_mae_r2_box() -> None:
1919
text_box = add_mae_r2_box(y_pred, y_true)
2020

2121
assert isinstance(text_box, AnchoredText)
@@ -65,7 +65,7 @@ def test_add_identity_line(
6565
assert line["line"]["color"] == line_kwds["color"] if line_kwds else "gray"
6666

6767

68-
def test_df_to_arrays():
68+
def test_df_to_arrays() -> None:
6969
df = pd.DataFrame([y_true, y_pred]).T
7070
x1, y1 = df_to_arrays(None, y_true, y_pred)
7171
x_col, y_col = df.columns[:2]

0 commit comments

Comments
 (0)