Skip to content

Commit 1823ec7

Browse files
committed
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 dcf081c commit 1823ec7

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

astroid/brain/brain_builtin_inference.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -645,12 +645,13 @@ 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 this property isn't within
652+
# node.frame (or anywhere else, really)
653+
parent=AstroidManager().adhoc_module,
651654
)
652-
# Set parent outside __init__: https://github.com/pylint-dev/astroid/issues/1490
653-
prop_func.parent = node
654655
prop_func.postinit(
655656
body=[],
656657
args=inferred.args,

tests/brain/test_builtin.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@
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(

0 commit comments

Comments
 (0)