diff options
| author | Claudiu Popa <pcmanticore@gmail.com> | 2020-03-26 09:26:00 +0100 |
|---|---|---|
| committer | Claudiu Popa <pcmanticore@gmail.com> | 2020-03-26 09:26:00 +0100 |
| commit | 76fd7aa73cfbe650c97bf6828adb7e39490c3e99 (patch) | |
| tree | 54f1e9bfafce7efa1fe1d2e84c18f4f33ee998d8 | |
| parent | ab9d147d71c3dff39972b7d2173aa66a616ff5b7 (diff) | |
| download | astroid-git-76fd7aa73cfbe650c97bf6828adb7e39490c3e99.tar.gz | |
Allow `FunctionDef.getattr` to look into both instance attrs and special attributes
Modifying an attribute of a function with an augmented assignment resulted in
`FunctionDef.getattr` to prioritize the instance attributes fetching. This means
that only the augmented assignment modification would have been visible.
Close PyCQA/pylint#1078
| -rw-r--r-- | ChangeLog | 4 | ||||
| -rw-r--r-- | astroid/scoped_nodes.py | 8 | ||||
| -rw-r--r-- | tests/unittest_inference.py | 16 |
3 files changed, 26 insertions, 2 deletions
@@ -15,6 +15,10 @@ Release Date: TBA Close PyCQA/pylint#2334 +* Allow `FunctionDef.getattr` to look into both instance attrs and special attributes + + Close PyCQA/pylint#1078 + * Infer qualified ``classmethod`` as a classmethod. Close PyCQA/pylint#3417 diff --git a/astroid/scoped_nodes.py b/astroid/scoped_nodes.py index 6b0e8dd5..f9424706 100644 --- a/astroid/scoped_nodes.py +++ b/astroid/scoped_nodes.py @@ -1560,10 +1560,14 @@ class FunctionDef(mixins.MultiLineBlockMixin, node_classes.Statement, Lambda): raise exceptions.AttributeInferenceError( target=self, attribute=name, context=context ) + + found_attrs = [] if name in self.instance_attrs: - return self.instance_attrs[name] + found_attrs = self.instance_attrs[name] if name in self.special_attributes: - return [self.special_attributes.lookup(name)] + found_attrs.append(self.special_attributes.lookup(name)) + if found_attrs: + return found_attrs raise exceptions.AttributeInferenceError(target=self, attribute=name) def igetattr(self, name, context=None): diff --git a/tests/unittest_inference.py b/tests/unittest_inference.py index 7fc5990a..b2210a8d 100644 --- a/tests/unittest_inference.py +++ b/tests/unittest_inference.py @@ -5775,5 +5775,21 @@ def test_recursion_error_self_reference_type_call(): assert [cls.name for cls in inferred.mro()] == ["B", "A", "object"] +def test_allow_retrieving_instance_attrs_and_special_attrs_for_functions(): + code = """ + class A: + def test(self): + "a" + # Add `__doc__` to `FunctionDef.instance_attrs` via an `AugAssign` + test.__doc__ += 'b' + test #@ + """ + node = extract_node(code) + inferred = next(node.infer()) + attrs = inferred.getattr("__doc__") + # One from the `AugAssign`, one from the special attributes + assert len(attrs) == 2 + + if __name__ == "__main__": unittest.main() |
