Skip to content

Commit 331e9d9

Browse files
Fix crash on bytes field names in functional Enum/namedtuple
names.value.replace(",", " ") raises TypeError, not AttributeError, when the field names argument is a bytes constant. Only AttributeError was caught, so Enum("", b"") crashed inference instead of falling back to the default. Closes #3189
1 parent a68e42c commit 331e9d9

3 files changed

Lines changed: 23 additions & 1 deletion

File tree

‎ChangeLog‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ Release date: TBA
2121
Closes #2852
2222
Closes pylint-dev/pylint#10807
2323

24+
* Fix a crash when the field names of a functional ``Enum`` or ``namedtuple``
25+
call are bytes, as in ``Enum("", b"")``. Such a definition is invalid, so
26+
inference now falls back to its default instead of raising ``TypeError``.
27+
28+
Closes #3189
29+
2430
* The documentation build now fails on any Sphinx warning, so a cross-reference
2531
that does not resolve is caught by CI instead of quietly rendering as plain
2632
text. ``Uninferable``, ``Position`` and the ``BadOperationMessage`` classes

‎astroid/brain/brain_namedtuple_enum.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,11 @@ def infer_func_form(
9292
name, names = _find_func_form_arguments(node, context)
9393
try:
9494
attributes: list[str] = names.value.replace(",", " ").split()
95-
except AttributeError as exc:
95+
except (AttributeError, TypeError) as exc:
96+
# ``names`` is not a whitespace-separated string: it is either a
97+
# container node, which has no ``value`` (AttributeError), or a
98+
# constant of another type such as bytes (TypeError).
99+
96100
# Handle attributes of NamedTuples
97101
if not enum:
98102
attributes = []

‎tests/brain/test_enum.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,18 @@ def test_enum_func_form_non_string_attribute_no_crash(self) -> None:
195195
# Inference must not raise ``TypeError``; it falls back to default.
196196
assert next(node.infer()) is util.Uninferable
197197

198+
def test_enum_func_form_bytes_field_names_no_crash(self) -> None:
199+
"""A functional Enum whose field names are bytes is invalid.
200+
201+
Regression test for https://github.com/pylint-dev/astroid/issues/3189
202+
"""
203+
node = builder.extract_node("""
204+
from enum import Enum
205+
Enum("", b"") #@
206+
""")
207+
# Inference must not raise ``TypeError``; it falls back to default.
208+
assert next(node.infer()) is util.Uninferable
209+
198210
def test_infer_enum_value_as_the_right_type(self) -> None:
199211
string_value, int_value = builder.extract_node("""
200212
from enum import Enum

0 commit comments

Comments
 (0)