Skip to content

Commit 8364693

Browse files
authored
Fix in place properties (#2553)
* fix construction of in-place properties This is an example of an in-place property: `bar = property(getter)`. They just create a nameless object, not the one with the name of the getter. Thus, the name was changed to "<property>". Furthermore, the definition of that property is not attached to any scope, as it's again nameless. it's a part of the campaign to get rid of non-module roots
1 parent 20890b8 commit 8364693

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

astroid/brain/brain_builtin_inference.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -645,12 +645,15 @@ def infer_property(
645645

646646
prop_func = objects.Property(
647647
function=inferred,
648-
name=inferred.name,
648+
name="<property>",
649649
lineno=node.lineno,
650650
col_offset=node.col_offset,
651+
# ↓ semantically, the definition of the class of property isn't within
652+
# node.frame. It's somewhere in the builtins module, but we are special
653+
# casing it for each "property()" call, so we are making up the
654+
# definition on the spot, ad-hoc.
655+
parent=AstroidManager().adhoc_module,
651656
)
652-
# Set parent outside __init__: https://github.com/pylint-dev/astroid/issues/1490
653-
prop_func.parent = node
654657
prop_func.postinit(
655658
body=[],
656659
args=inferred.args,

tests/brain/test_builtin.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,23 @@
1414

1515
class BuiltinsTest(unittest.TestCase):
1616
def test_infer_property(self):
17-
class_with_property = _extract_single_node(
17+
property_assign = _extract_single_node(
1818
"""
1919
class Something:
2020
def getter():
2121
return 5
2222
asd = property(getter) #@
2323
"""
2424
)
25-
inferred_property = next(iter(class_with_property.value.infer()))
25+
inferred_property = next(iter(property_assign.value.infer()))
2626
self.assertTrue(isinstance(inferred_property, objects.Property))
27-
class_parent = inferred_property.parent.parent.parent
27+
class_parent = property_assign.scope()
2828
self.assertIsInstance(class_parent, nodes.ClassDef)
2929
self.assertFalse(
3030
any(
31-
isinstance(getter, objects.Property)
32-
for getter in class_parent.locals["getter"]
31+
isinstance(def_, objects.Property)
32+
for def_list in class_parent.locals.values()
33+
for def_ in def_list
3334
)
3435
)
3536
self.assertTrue(hasattr(inferred_property, "args"))

0 commit comments

Comments
 (0)