Skip to content

Commit 953362a

Browse files
Fix startup crash on Python 3.10.0 caused by missing NodeId cache (#15342) (#15399)
On CPython 3.10.0, NodeId's cached-string attribute isn't initialized because of python/cpython#88815. Calling str() on it raises AttributeError and prevents pytest from starting. This reads the cache with getattr and a None default, then lets the existing code rebuild it. Added a regression test that simulates the missing attribute, plus a changelog entry. The relevant suites pass on Python 3.10.0 and 3.14. The regression test fails without the fix on 3.14, and a trivial suite that previously crashed now runs on 3.10.0. AI assistance: this fix, regression test and PR description were prepared with Instinct. Fixes #15337. (cherry picked from commit 50234b4) Co-authored-by: Aathithya Arasu S <142731071+Aathi-27@users.noreply.github.com>
1 parent c5a4d14 commit 953362a

4 files changed

Lines changed: 28 additions & 2 deletions

File tree

‎AUTHORS‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ merlinux GmbH, Germany, office at merlinux eu
44
Contributors include::
55

66
Aaron Coleman
7+
Aathithya Arasu S
78
Abdeali JK
89
Abdelrahman Elbehery
910
Abhijeet Kasurde

‎changelog/15337.bugfix.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed an ``INTERNALERROR`` crash when running pytest on Python 3.10.0 exactly: a slotted dataclass field was never initialized on that interpreter version (:issue:`15337`).

‎src/_pytest/nodeid.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,14 @@ def parse(cls, nodeid: str) -> NodeId:
5757
return self
5858

5959
def __str__(self) -> str:
60-
if self._str_cache is not None:
61-
return self._str_cache
60+
# Read ``_str_cache`` defensively: on Python 3.10.0, slotted dataclass
61+
# fields with ``init=False`` and a default value are never set
62+
# (https://github.com/python/cpython/issues/88815), so the attribute
63+
# may be missing entirely.
64+
# This may be cleaned up to `cache = self.__str_cache` once Python 3.10 is dropped.
65+
cache: str | None = getattr(self, "_str_cache", None)
66+
if cache is not None:
67+
return cache
6268
s = "::".join((self.path, *self.names))
6369
if self.params is not None:
6470
s += f"[{self.params}]"

‎testing/test_nodeid.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,24 @@ def test_str_with_params(self) -> None:
2323
node_id = NodeId(path="a/test_b.py", names=("test_c",), params="1-x")
2424
assert str(node_id) == "a/test_b.py::test_c[1-x]"
2525

26+
def test_str_missing_str_cache(self) -> None:
27+
"""
28+
Simulate Python 3.10.0 (https://github.com/python/cpython/issues/88815):
29+
slotted dataclass fields with init=False and a default are never set,
30+
so _str_cache may be missing entirely (#15337).
31+
32+
This test can be removed and the related code in NodeId.__str__ can be cleaned up once we
33+
drop Python 3.10 support.
34+
"""
35+
node_id = NodeId(path="a/test_b.py", names=("test_c",))
36+
# Ensure the attribute exists first so the deletion works on every
37+
# interpreter version (on 3.10.0 it is never set in the first place).
38+
object.__setattr__(node_id, "_str_cache", None)
39+
object.__delattr__(node_id, "_str_cache")
40+
assert str(node_id) == "a/test_b.py::test_c"
41+
# The cache must be repopulated by the first __str__ call.
42+
assert node_id._str_cache == "a/test_b.py::test_c"
43+
2644
# -- child() and leaf() --
2745

2846
def test_child(self) -> None:

0 commit comments

Comments
 (0)