Skip to content

Commit 8577346

Browse files
kdelayPierre-Sassoulas
authored andcommitted
Infer the value of a data descriptor as unknown
raw_building modelled a data descriptor of a C-implemented class as a class named after the attribute, so reading such an attribute inferred that class rather than a value: exc.__traceback__.tb_frame inferred a tb_frame class and gen.gi_frame a gi_frame class. Attributes of the frame those descriptors return were then reported as missing, e.g. E1101 Class 'tb_frame' has no 'f_locals' member. Build an unknown value instead, the same node the fallback branch of InspectBuilder.object_build already uses for members astroid cannot model. The attribute itself stays resolvable; only its value is now Uninferable. Closes pylint-dev/pylint#11218
1 parent 8666418 commit 8577346

5 files changed

Lines changed: 53 additions & 12 deletions

File tree

‎ChangeLog‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,15 @@ Refs #3108
357357

358358
Refs #3115
359359

360+
* Infer the value of a data descriptor of a C-implemented class as unknown
361+
instead of as a class named after the attribute. Reading such an attribute,
362+
for instance ``exc.__traceback__.tb_frame`` or ``gen.gi_frame``, inferred a
363+
class, so the attributes of the value the descriptor returns were reported as
364+
missing. ``raw_building.object_build_datadescriptor()`` now returns an
365+
``EmptyNode``.
366+
367+
Closes pylint-dev/pylint#11218
368+
360369
What's New in astroid 4.1.2?
361370
============================
362371
Release date: 2026-03-22

‎astroid/raw_building.py‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -335,9 +335,15 @@ def object_build_function(
335335

336336
def object_build_datadescriptor(
337337
node: nodes.Module | nodes.ClassDef, member: type
338-
) -> nodes.ClassDef:
339-
"""create astroid for a living data descriptor object"""
340-
return _base_class_object_build(node, member, [])
338+
) -> nodes.EmptyNode:
339+
"""create astroid for a living data descriptor object
340+
341+
What the descriptor returns is not statically known, so it is modelled as an
342+
unknown value. Modelling it as a class named after the attribute made
343+
attribute access on the descriptor's value infer that class instead, which
344+
reported the value's own attributes as missing.
345+
"""
346+
return build_dummy(_EMPTY_OBJECT_MARKER)
341347

342348

343349
def object_build_methoddescriptor(
@@ -511,7 +517,7 @@ def object_build(
511517
elif inspect.ismethoddescriptor(member):
512518
child: nodes.NodeNG = object_build_methoddescriptor(node, member)
513519
elif inspect.isdatadescriptor(member):
514-
child = object_build_datadescriptor(node, member)
520+
child: nodes.NodeNG = object_build_datadescriptor(node, member)
515521
elif isinstance(member, tuple(node_classes.CONST_CLS)):
516522
# Special case: __hash__ = None overrides ObjectModel for unhashable types.
517523
# See https://docs.python.org/3/reference/datamodel.html#object.__hash__

‎tests/brain/test_ctypes.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import pytest
88

9-
from astroid import extract_node, nodes
9+
from astroid import extract_node, nodes, util
1010

1111
pytestmark = pytest.mark.skipif(
1212
hasattr(sys, "pypy_version_info"),
@@ -95,9 +95,9 @@ def test_cdata_member_access() -> None:
9595
"""
9696
node = extract_node(src)
9797
assert isinstance(node, nodes.NodeNG)
98-
node_inf = node.inferred()[0]
99-
assert node_inf.display_type() == "Class"
100-
assert node_inf.qname() == "_ctypes._SimpleCData._objects"
98+
# ``_objects`` is a data descriptor, so the attribute resolves but the value
99+
# it returns is not statically known.
100+
assert node.inferred() == [util.Uninferable]
101101

102102

103103
def test_other_ctypes_member_untouched() -> None:

‎tests/test_object_model.py‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -666,13 +666,13 @@ def test():
666666
doc = next(ast_nodes[1].infer())
667667
self.assertEqual(doc.value, "a")
668668

669+
# ``gi_code`` and ``gi_frame`` are data descriptors, so they resolve but
670+
# the values they return are not statically known.
669671
gi_code = next(ast_nodes[2].infer())
670-
self.assertIsInstance(gi_code, nodes.ClassDef)
671-
self.assertEqual(gi_code.name, "gi_code")
672+
self.assertIs(gi_code, util.Uninferable)
672673

673674
gi_frame = next(ast_nodes[3].infer())
674-
self.assertIsInstance(gi_frame, nodes.ClassDef)
675-
self.assertEqual(gi_frame.name, "gi_frame")
675+
self.assertIs(gi_frame, util.Uninferable)
676676

677677
send = next(ast_nodes[4].infer())
678678
self.assertIsInstance(send, astroid.BoundMethod)
@@ -741,6 +741,21 @@ def test_oserror(self) -> None:
741741
assert isinstance(inferred, nodes.Const)
742742
assert inferred.value == value
743743

744+
@staticmethod
745+
def test_traceback_frame_attribute() -> None:
746+
"""The frame a traceback exposes is a data descriptor, so its value is
747+
unknown rather than a class named after the attribute.
748+
749+
Closes https://github.com/pylint-dev/pylint/issues/11218
750+
"""
751+
node = builder.extract_node("""
752+
try:
753+
raise ValueError("a")
754+
except ValueError as err:
755+
err.__traceback__.tb_frame.f_locals #@
756+
""")
757+
assert node.inferred() == [util.Uninferable]
758+
744759
def test_unicodedecodeerror(self) -> None:
745760
code = """
746761
try:

‎tests/test_raw_building.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import tests.testdata.python3.data.fake_module_with_broken_getattr as fm_getattr
2525
import tests.testdata.python3.data.fake_module_with_collection_getattribute as fm_collection
2626
import tests.testdata.python3.data.fake_module_with_warnings as fm
27+
from astroid import nodes, util
2728
from astroid.builder import AstroidBuilder
2829
from astroid.const import IS_PYPY, PY312_PLUS
2930
from astroid.manager import AstroidManager
@@ -34,6 +35,7 @@
3435
build_function,
3536
build_module,
3637
object_build_class,
38+
object_build_datadescriptor,
3739
)
3840

3941
try:
@@ -91,6 +93,15 @@ def test_build_function_kwonlyargs(self) -> None:
9193
assert node.args.kwonlyargs[0].name == "a"
9294
assert node.args.kwonlyargs[1].name == "b"
9395

96+
def test_object_build_datadescriptor(self) -> None:
97+
# Tests https://github.com/pylint-dev/pylint/issues/11218
98+
# A data descriptor used to be modelled as a class named after the
99+
# attribute, so the value it returns was inferred as that class and the
100+
# attributes of the real value were reported as missing.
101+
node = object_build_datadescriptor(DUMMY_MOD, types.TracebackType.tb_frame)
102+
self.assertIsInstance(node, nodes.EmptyNode)
103+
self.assertEqual(node.inferred(), [util.Uninferable])
104+
94105
def test_build_from_import(self) -> None:
95106
names = ["exceptions, inference, inspector"]
96107
node = build_from_import("astroid", names)

0 commit comments

Comments
 (0)