diff options
author | Claudiu Popa <pcmanticore@gmail.com> | 2019-11-28 09:55:05 +0100 |
---|---|---|
committer | Claudiu Popa <pcmanticore@gmail.com> | 2019-12-04 13:31:38 +0100 |
commit | 55b43f6755d4839cefaf010eb2ced747d014d459 (patch) | |
tree | 423bce7071ee07f5e3ffb9d983b38296f5c9acc2 /astroid/objects.py | |
parent | 4952320cda79c79f6868e4fb59f09e188cd9cafc (diff) | |
download | astroid-git-55b43f6755d4839cefaf010eb2ced747d014d459.tar.gz |
Add support for inferring properties
These new capabilities will allow inferring both the `property` builtin
as well as property attributes such as `.deleter` and `.setter`.
Diffstat (limited to 'astroid/objects.py')
-rw-r--r-- | astroid/objects.py | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/astroid/objects.py b/astroid/objects.py index 888ca36e..93c5f406 100644 --- a/astroid/objects.py +++ b/astroid/objects.py @@ -184,6 +184,9 @@ class Super(node_classes.NodeNG): yield inferred elif self._class_based or inferred.type == "staticmethod": yield inferred + elif isinstance(inferred, Property): + function = inferred.function + yield from function.infer_call_result(caller=self, context=context) elif bases._is_property(inferred): # TODO: support other descriptors as well. try: @@ -280,3 +283,26 @@ class PartialFunction(scoped_nodes.FunctionDef): # TODO: Hack to solve the circular import problem between node_classes and objects # This is not needed in 2.0, which has a cleaner design overall node_classes.Dict.__bases__ = (node_classes.NodeNG, DictInstance) + + +class Property(scoped_nodes.FunctionDef): + """Class representing a Python property""" + + def __init__( + self, function, name=None, doc=None, lineno=None, col_offset=None, parent=None + ): + super().__init__(name, doc, lineno, col_offset, parent) + self.function = function + + # pylint: disable=unnecessary-lambda + special_attributes = util.lazy_descriptor(lambda: objectmodel.PropertyModel()) + type = "property" + + def pytype(self): + return "%s.property" % BUILTINS + + def infer_call_result(self, caller=None, context=None): + raise exceptions.InferenceError("Properties are not callable") + + def infer(self, context=None, **kwargs): + return iter((self,)) |