Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/14560.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed automatic parametrization ID generation crashing when a parameter object raises an exception while accessing ``__name__``.
9 changes: 5 additions & 4 deletions src/_pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -1048,10 +1048,11 @@ def _idval_from_value(self, val: object) -> str | None:
pass
elif isinstance(val, enum.Enum):
return str(val)
elif isinstance(getattr(val, "__name__", None), str):
# Name of a class, function, module, etc.
name: str = getattr(val, "__name__")
return name
else:
name: str | None = safe_getattr(val, "__name__", None)
if isinstance(name, str):
# Name of a class, function, module, etc.
return name
return None

def _idval_from_value_required(self, val: object, idx: int) -> str:
Expand Down
12 changes: 12 additions & 0 deletions testing/python/metafunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,18 @@ def test_function():
IdMaker([], [], None, None, None, None)._idval(val, "a", 6) == expected
)

def test_idval_ignores_broken_name_attribute(self) -> None:
"""Use the fallback ID for values that reject __name__ lookup."""

class DictWrapper(dict[str, object]):
def __getattr__(self, name: str) -> object:
return self[name]

assert (
IdMaker([], [], None, None, None, None)._idval(DictWrapper(), "a", 6)
== "a6"
)

def test_notset_idval(self) -> None:
"""Test that a NOTSET value (used by an empty parameterset) generates
a proper ID.
Expand Down
Loading