Skip to content

Commit

Permalink
fix construction of in-place properties
Browse files Browse the repository at this point in the history
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
  • Loading branch information
temyurchenko committed Sep 9, 2024
1 parent dcf081c commit 1823ec7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
7 changes: 4 additions & 3 deletions astroid/brain/brain_builtin_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,12 +645,13 @@ def infer_property(

prop_func = objects.Property(
function=inferred,
name=inferred.name,
name="<property>",
lineno=node.lineno,
col_offset=node.col_offset,
# ↓ semantically, the definition of this property isn't within
# node.frame (or anywhere else, really)
parent=AstroidManager().adhoc_module,
)
# Set parent outside __init__: https://github.com/pylint-dev/astroid/issues/1490
prop_func.parent = node
prop_func.postinit(
body=[],
args=inferred.args,
Expand Down
6 changes: 3 additions & 3 deletions tests/brain/test_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@

class BuiltinsTest(unittest.TestCase):
def test_infer_property(self):
class_with_property = _extract_single_node(
property_assign = _extract_single_node(
"""
class Something:
def getter():
return 5
asd = property(getter) #@
"""
)
inferred_property = next(iter(class_with_property.value.infer()))
inferred_property = next(iter(property_assign.value.infer()))
self.assertTrue(isinstance(inferred_property, objects.Property))
class_parent = inferred_property.parent.parent.parent
class_parent = property_assign.scope()
self.assertIsInstance(class_parent, nodes.ClassDef)
self.assertFalse(
any(
Expand Down

0 comments on commit 1823ec7

Please sign in to comment.