pytest 9.2.0 regression on Python 3.10.0: NodeId.__str__ raises AttributeError
Maintainer antworten meist innerhalb von 1 Tag
Bewertung
- Schwierigkeit
- 2/5
- Geschätzter Aufwand
- 1-3 Stunden
- Anfängerfreundlichkeit
- 64/100
Rechercherichtung
Beginne mit src/_pytest/nodeid.py und untersuche NodeId.__str__() sowie das Feld _str_cache; prüfe anschließend die bestehenden Tests für NodeId und dessen Stringkonvertierung. Reproduziere den gemeldeten Fehler unter Python 3.10.0, falls verfügbar. Die Aufgabe ist abgeschlossen, wenn die Stringkonvertierung unter den unterstützten Python-Versionen funktioniert und ein Regressionstest das Verhalten abdeckt.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
Description
pytest 9.2.0 introduced the internal NodeId dataclass as part of #14758.
The class uses @dataclasses.dataclass(frozen=True, slots=True, kw_only=True) and declares _str_cache with default=None and init=False.
CPython 3.10.0 has a known bug where slotted dataclass fields with init=False and a default value are not initialized. This was fixed in CPython 3.10.1 (python/cpython#88815).
Because NodeId.__str__() accesses self._str_cache directly, an instance constructed on Python 3.10.0 can raise AttributeError during string conversion.
pytest 9.2.0 declares requires-python = ">=3.10", which includes Python 3.10.0.
Minimal reproduction
Using Python 3.10.0:
python -m pip install pytest==9.2.0
Then execute:
from _pytest.nodeid import NodeId
node = NodeId(path="test_example.py")
print(str(node))
Expected behavior
test_example.py
Predicted actual behavior on Python 3.10.0
AttributeError: 'NodeId' object has no attribute '_str_cache'
Verification status: The failure is inferred from the pytest 9.2.0 source and the CPython 3.10.0 implementation. The exact reproducer still needs to be executed on Python 3.10.0.
Root cause
In src/_pytest/nodeid.py:
_str_cache: str | None = dataclasses.field(
default=None, init=False,
repr=False, compare=False
)
In Python 3.10.0, dataclasses._field_init() does not initialize this field when init=False, even though the generated class uses slots.
CPython 3.10.1 added the missing condition:
if slots and f.default is not MISSING:
globals[default_name] = f.default
value = default_name
The new NodeId class relies on the corrected behavior.
Suggested resolution
Would the maintainers consider one of the following?
- Initialize
_str_cacheexplicitly, for example using__post_init__()andobject.__setattr__(), to preserve compatibility with Python 3.10.0. - If supporting Python 3.10.0 is not intended, clarify the minimum supported patch version and update the package metadata accordingly.
- Consider adding a small compatibility smoke test for the earliest supported CPython patch release so similar regressions can be caught in CI.
References
- pytest 9.2.0 implementation: https://github.com/pytest-dev/pytest/blob/9.2.0/src/_pytest/nodeid.py
- pytest change: #14758
- CPython bug: https://github.com/python/cpython/issues/88815
- CPython 3.10.0 implementation: https://github.com/python/cpython/blob/v3.10.0/Lib/dataclasses.py
- CPython 3.10.1 fix: https://github.com/python/cpython/blob/v3.10.1/Lib/dataclasses.py
Thank you for considering this compatibility issue.
- Vorherrschende Sprache
- Python
- Sterne
- 14.7k
- Forks
- 3.6k
- Ø Merge
- 2 T. 2 Std.
- Gemergte PRs (30 T.)
- 34
Entwicklungsumgebung
- Kein Dockerfile und keine Docker-Compose-Datei
- Hat eine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus pytest-dev/pytest
-
pytest.approx with a list expected value reports overflowing differences for numpy integer arraysEvtl. vergeben @rinopatrick hat das vor 7 Tagen übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
pytest-dev/pytest#15314 · 2 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
-
Logging capture crashes when another thread registers a loggerEvtl. vergeben @Spikhalskiy hat das vor 18 Tagen übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
pytest-dev/pytest#15237 ·
Maintainer antworten meist innerhalb von 1 Tag
-
`faulthandler_timeout` stops covering a test once setup, call or a subtest fails (`pytest_exception_interact` cancels the timer)Evtl. vergeben @mayur-dhavan hat das vor 14 Tagen übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
pytest-dev/pytest#15180 · 2 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
-
monkeypatch.setattr fails to undo on objects with a custom __setattr__ (regression from #14969)Evtl. vergeben @hardikkaurani hat das vor 45 Tagen übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
pytest-dev/pytest#15099 · 7 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
-
Allow dotted test filenames (`*.test.py`) via one-line change to `compute_module_name`Evtl. wieder frei @siddy-stack hat das vor 83 Tagen übernommen, und es ist kein Pull Request offen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 86/100
pytest-dev/pytest#14514 · 6 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
Alle Issues in pytest-dev/pytest
Ähnliche Issues
-
bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 79/100
vllm-project/vllm-omni#10051 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
Bug Testing
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
SCIInstitute/SCIRun#2931 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
Maintainer antworten meist innerhalb von 5 Tagen
-
Author Page: barom-kangOffenauthor-page correction
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 64/100
acl-org/acl-anthology#10827 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 91/100
marimo-team/marimo#11768 ·
Maintainer antworten meist innerhalb von 1 Tag