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/inference.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/inference.py')
-rw-r--r-- | astroid/inference.py | 39 |
1 files changed, 31 insertions, 8 deletions
diff --git a/astroid/inference.py b/astroid/inference.py index 77c6b1d0..64e79df7 100644 --- a/astroid/inference.py +++ b/astroid/inference.py @@ -38,22 +38,24 @@ from astroid import util MANAGER = manager.AstroidManager() +# Prevents circular imports +objects = util.lazy_import("objects") # .infer method ############################################################### def infer_end(self, context=None): - """inference's end for node such as Module, ClassDef, FunctionDef, - Const... + """Inference's end for nodes that yield themselves on inference + These are objects for which inference does not have any semantic, + such as Module or Consts. """ yield self nodes.Module._infer = infer_end nodes.ClassDef._infer = infer_end -nodes.FunctionDef._infer = infer_end nodes.Lambda._infer = infer_end nodes.Const._infer = infer_end nodes.Slice._infer = infer_end @@ -309,11 +311,13 @@ def infer_attribute(self, context=None): try: context.boundnode = owner yield from owner.igetattr(self.attrname, context) - context.boundnode = None - except (exceptions.AttributeInferenceError, exceptions.InferenceError): - context.boundnode = None - except AttributeError: - # XXX method / function + except ( + exceptions.AttributeInferenceError, + exceptions.InferenceError, + AttributeError, + ): + pass + finally: context.boundnode = None return dict(node=self, context=context) @@ -941,3 +945,22 @@ def infer_ifexp(self, context=None): nodes.IfExp._infer = infer_ifexp + + +def infer_functiondef(self, context=None): + if not self.decorators or not bases._is_property(self): + yield self + return + + prop_func = objects.Property( + function=self, + name=self.name, + doc=self.doc, + lineno=self.lineno, + parent=self.parent, + col_offset=self.col_offset, + ) + yield prop_func + + +nodes.FunctionDef._infer = infer_functiondef |