Skip to content

Commit

Permalink
Fix in place properties (#2553)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
temyurchenko authored Sep 10, 2024
1 parent 20890b8 commit 8364693
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
9 changes: 6 additions & 3 deletions astroid/brain/brain_builtin_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,12 +645,15 @@ 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 the class of property isn't within
# node.frame. It's somewhere in the builtins module, but we are special
# casing it for each "property()" call, so we are making up the
# definition on the spot, ad-hoc.
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
11 changes: 6 additions & 5 deletions tests/brain/test_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,23 @@

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(
isinstance(getter, objects.Property)
for getter in class_parent.locals["getter"]
isinstance(def_, objects.Property)
for def_list in class_parent.locals.values()
for def_ in def_list
)
)
self.assertTrue(hasattr(inferred_property, "args"))
Expand Down

0 comments on commit 8364693

Please sign in to comment.