Skip to content

Commit

Permalink
feat(python): Improved error message on invalid Python Enum init (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-beedie authored Nov 29, 2024
1 parent 4a46723 commit ccaf682
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 7 deletions.
2 changes: 1 addition & 1 deletion py-polars/polars/convert/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@ def _from_series_repr(m: re.Match[str]) -> Series:
else:
string_values = [
v.strip()
for v in re.findall(r"[\s>#]*(?:\t|\s{4,})([^\n]*)\n", m.groups()[-1])
for v in re.findall(r"[\s>#]*(?:\t|\s{2,})([^\n]*)\n", m.groups()[-1])
]
if string_values == ["[", "]"]:
string_values = []
Expand Down
6 changes: 3 additions & 3 deletions py-polars/polars/datatypes/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,10 +622,10 @@ def __init__(self, categories: Series | Iterable[str] | type[enum.Enum]) -> None
)

if isclass(categories) and issubclass(categories, enum.Enum):
for enum_subclass in (enum.IntFlag, enum.Flag, enum.IntEnum):
for enum_subclass in (enum.Flag, enum.IntEnum):
if issubclass(categories, enum_subclass):
enum_type_name = enum_subclass.__name__
msg = f"Enum categories must be strings; Python `enum.{enum_type_name}` values are integers"
enum_type_name = categories.__name__
msg = f"Enum categories must be strings; `{enum_type_name}` values are integers"
raise TypeError(msg)

enum_values = [
Expand Down
4 changes: 1 addition & 3 deletions py-polars/tests/unit/datatypes/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,9 @@ class Color(EnumBase): # type: ignore[no-redef,misc,valid-type]
GREEN = enum.auto()
BLUE = enum.auto()

base_name = EnumBase.__name__

with pytest.raises(
TypeError,
match=f"Enum categories must be strings; Python `enum.{base_name}` values are integers",
match="Enum categories must be strings; `Color` values are integers",
):
pl.Enum(Color)

Expand Down

0 comments on commit ccaf682

Please sign in to comment.