Skip to content
Open
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 AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ Nicolas Delaby
Nicolas Simonds
Nico Vidal
Nikesh Chavhan
Nikita Zamuldinov
Nikolay Kondratyev
Nipunn Koorapati
Oleg Pidsadnyi
Expand Down
1 change: 1 addition & 0 deletions changelog/14560.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed a crash when parametrized tests used custom classes with a ``__getattr__`` that raises ``KeyError`` instead of ``AttributeError`` during test id generation.
22 changes: 22 additions & 0 deletions repro_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from __future__ import annotations

import sys


sys.path.insert(0, "/root/.openclaw/workspace-fork/pytest-work/src")

from _pytest.python import IdMaker


class CustomDict:
def __init__(self, data):
self._data = data

def __getattr__(self, item):
return self._data[item]


val = CustomDict({"a": 1})
result = IdMaker([], [], None, None, None, None)._idval(val, "a", 6)
assert result == "a6", f"Expected 'a6', got {result!r}"
print("Test passed!")
7 changes: 5 additions & 2 deletions src/_pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -1048,9 +1048,12 @@ 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):
try:
name = getattr(val, "__name__", None)
except (AttributeError, KeyError):
name = None
if isinstance(name, str):
# Name of a class, function, module, etc.
name: str = getattr(val, "__name__")
return name
return None

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

def test_idval_custom_class_getattr_keyerror(self) -> None:
"""Regression test for #14560.

Custom classes with a ``__getattr__`` that raises ``KeyError``
(instead of ``AttributeError``) should not crash id generation.
"""

class CustomDict:
def __init__(self, data: dict) -> None:
self._data = data

def __getattr__(self, item: str) -> object:
return self._data[item]

val = CustomDict({"a": 1})
result = IdMaker([], [], None, None, None, None)._idval(val, "a", 6)
assert result == "a6"

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