pytest 9.2.0 regression on Python 3.10.0: NodeId.__str__ raises AttributeError

Offen Anfängerfreundlich
#15,337 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

@Aathi-27 arbeitet bereits daran.

Seit 05.11.2026.

  • #15342 von @Aathi-27 — offen

Bewertung

Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Anfängerfreundlichkeit
64/100
Issue-Typ
Bug
Klarheit
Größtenteils klar
Aktivitätsstatus
Aktiv
Tech-Stack
python
Bereich
testing

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?

  1. Initialize _str_cache explicitly, for example using __post_init__() and object.__setattr__(), to preserve compatibility with Python 3.10.0.
  2. If supporting Python 3.10.0 is not intended, clarify the minimum supported patch version and update the package metadata accordingly.
  3. Consider adding a small compatibility smoke test for the earliest supported CPython patch release so similar regressions can be caught in CI.
References

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

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus pytest-dev/pytest

Alle Issues in pytest-dev/pytest

Ähnliche Issues

Weitere Issues zu Python

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.