Skip to content

Commit a6825ff

Browse files
committed
force LabelEnums to have labels, gives .label type str causing less mypy headache
- update LabelEnum to require label and make description default to empty str for better usability - update tests to cover new LabelEnum behavior
1 parent 45d5c69 commit a6825ff

File tree

6 files changed

+13
-27
lines changed

6 files changed

+13
-27
lines changed

pymatviz/coordination/helpers.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
class CnSplitMode(LabelEnum):
2222
"""How to split the coordination number histogram into subplots."""
2323

24-
none = "none"
25-
by_element = "by element"
26-
by_structure = "by structure"
27-
by_structure_and_element = "by structure and element"
24+
none = "none", "None"
25+
by_element = "by element", "By element"
26+
by_structure = "by structure", "By structure"
27+
by_structure_and_element = "by structure and element", "By structure and element"
2828

2929

3030
def create_hover_text(

pymatviz/enums.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,7 @@ class LabelEnum(StrEnum):
8585
Simply add label and description as a tuple starting with the key's value.
8686
"""
8787

88-
def __new__(
89-
cls,
90-
val: str,
91-
label: str | None = None,
92-
desc: str | None = None,
93-
) -> Self:
88+
def __new__(cls, val: str, label: str, desc: str = "") -> Self:
9489
"""Create a new class from a value, label, and description. Label and
9590
description are optional.
9691
"""
@@ -111,14 +106,14 @@ def __reduce_ex__(self, proto: object) -> tuple[type, tuple[str]]:
111106
return str, (self.value,)
112107

113108
@property
114-
def label(self) -> str | None:
109+
def label(self) -> str:
115110
"""Make label read-only."""
116-
return self.__dict__.get("label")
111+
return self.__dict__["label"]
117112

118113
@property
119-
def description(self) -> str | None:
114+
def description(self) -> str:
120115
"""Make description read-only."""
121-
return self.__dict__.get("desc")
116+
return self.__dict__["desc"]
122117

123118

124119
eV_per_atom = html_tag("(eV/atom)", style="small") # noqa: N816

pymatviz/process_data.py

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

55
import itertools
6-
import warnings
76
from typing import TYPE_CHECKING
87

98
import pandas as pd
@@ -62,12 +61,6 @@ def count_elements(
6261
Returns:
6362
pd.Series: Map element symbols to heatmap values.
6463
"""
65-
# TODO: remove warning after 2025-01-01
66-
if fill_value is None:
67-
warnings.warn(
68-
"default value of fill_value changed from zero to None.", stacklevel=2
69-
)
70-
7164
valid_count_modes = set(ElemCountMode)
7265
if count_mode not in valid_count_modes:
7366
raise ValueError(f"Invalid {count_mode=} must be one of {valid_count_modes}")

pymatviz/scatter.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@ def density_scatter_plotly(
182182
y (str): y-values dataframe column name.
183183
df (pd.DataFrame): DataFrame with x and y columns.
184184
density ('kde' | 'interpolate' | 'empirical'): Determines the method for
185-
calculating and displaying density.
185+
calculating and displaying density. Default is 'empirical' when n_bins
186+
is provided, else 'kde' for kernel density estimation.
186187
log_density (bool | None): Whether to apply logarithmic scaling to density.
187188
If None, automatically set based on density range.
188189
identity_line (bool | dict[str, Any], optional): Whether to add a parity line

tests/test_enums.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
class DummyEnum(LabelEnum):
1515
TEST1 = "test1", "Test Label 1", "Test Description 1"
16-
TEST2 = "test2"
16+
TEST2 = "test2", "Test Label 2"
1717

1818

1919
def test_str_enum() -> None:
@@ -74,8 +74,6 @@ def test_label_enum_new() -> None:
7474

7575
def test_label_enum_repr() -> None:
7676
assert repr(DummyEnum.TEST1) == DummyEnum.TEST1.label == "Test Label 1"
77-
assert repr(DummyEnum.TEST2) == "DummyEnum.TEST2"
78-
assert DummyEnum.TEST2.label is None
7977

8078

8179
@pytest.mark.parametrize(

tests/test_process_data.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,7 @@ def test_count_elements_exclude_invalid_elements() -> None:
123123

124124
def test_count_elements_fill_value() -> None:
125125
compositions = [Composition("Fe2O3")] * 5 + [Composition("Fe4P4O16")] * 3
126-
with pytest.warns(UserWarning, match="default value of fill_value changed"):
127-
series = pmv.count_elements(compositions, count_mode=ElemCountMode.composition)
126+
series = pmv.count_elements(compositions, count_mode=ElemCountMode.composition)
128127
expected = pd.Series(
129128
{"Fe": 22, "O": 63, "P": 12}, index=pmv.df_ptable.index, name="count"
130129
)

0 commit comments

Comments
 (0)