diff options
| author | Claudiu Popa <pcmanticore@gmail.com> | 2016-06-05 11:49:12 +0100 |
|---|---|---|
| committer | Claudiu Popa <pcmanticore@gmail.com> | 2016-06-05 13:08:44 +0100 |
| commit | cfd086b1a1f9f979866d82707dc6558d64b74cf8 (patch) | |
| tree | 2c60b57da12541a2b469d69019458d5a8ed7cb93 | |
| parent | 16d24126772d0da3d7e71690b731fff79ee50c99 (diff) | |
| download | astroid-git-cfd086b1a1f9f979866d82707dc6558d64b74cf8.tar.gz | |
Give priority to local special attributes for classes and modules
For instance, a module can decide to redefine its __file__ attribute.
Previously, this was defaulting to the file name itself, without taking
in account local redefinitions.
| -rw-r--r-- | astroid/tests/unittest_object_model.py | 18 | ||||
| -rw-r--r-- | astroid/tests/unittest_scoped_nodes.py | 4 | ||||
| -rw-r--r-- | astroid/tree/scoped_nodes.py | 27 |
3 files changed, 43 insertions, 6 deletions
diff --git a/astroid/tests/unittest_object_model.py b/astroid/tests/unittest_object_model.py index f63a1b28..de4384fa 100644 --- a/astroid/tests/unittest_object_model.py +++ b/astroid/tests/unittest_object_model.py @@ -125,6 +125,16 @@ class UnboundMethodModelTest(unittest.TestCase): class ClassModelTest(unittest.TestCase): + def test_priority_to_local_defined_values(self): + ast_node = test_utils.extract_node(''' + class A: + __doc__ = "first" + A.__doc__ #@ + ''') + inferred = next(ast_node.infer()) + self.assertIsInstance(inferred, astroid.Const) + self.assertEqual(inferred.value, "first") + @test_utils.require_version(maxver='3.0') def test__mro__old_style(self): ast_node = test_utils.extract_node(''' @@ -232,6 +242,14 @@ class ClassModelTest(unittest.TestCase): class ModuleModelTest(unittest.TestCase): + def test_priority_to_local_defined_values(self): + ast_node = astroid.parse(''' + __file__ = "mine" + ''') + file_value = next(ast_node.igetattr('__file__')) + self.assertIsInstance(file_value, astroid.Const) + self.assertEqual(file_value.value, "mine") + def test__path__not_a_package(self): ast_node = test_utils.extract_node(''' import sys diff --git a/astroid/tests/unittest_scoped_nodes.py b/astroid/tests/unittest_scoped_nodes.py index d9583ddb..6a8012f8 100644 --- a/astroid/tests/unittest_scoped_nodes.py +++ b/astroid/tests/unittest_scoped_nodes.py @@ -612,8 +612,8 @@ class ClassNodeTest(ModuleLoader, unittest.TestCase): def test_cls_special_attributes_2(self): astroid = builder.parse(''' - class A: pass - class B: pass + class A(object): pass + class B(object): pass A.__bases__ += (B,) ''', __name__) diff --git a/astroid/tree/scoped_nodes.py b/astroid/tree/scoped_nodes.py index 627903c8..0e73ec12 100644 --- a/astroid/tree/scoped_nodes.py +++ b/astroid/tree/scoped_nodes.py @@ -231,9 +231,11 @@ class Module(QualifiedNameMixin, lookup.LocalsDictNode): def getattr(self, name, context=None, ignore_locals=False): result = [] - if name in self.special_attributes: + name_in_locals = name in self.locals + + if name in self.special_attributes and not ignore_locals and not name_in_locals: result = [self.special_attributes.lookup(name)] - elif not ignore_locals and name in self.locals: + elif not ignore_locals and name_in_locals: result = self.locals[name] # TODO: should ignore_locals also affect external_attrs? elif name in self.external_attrs: @@ -1520,8 +1522,23 @@ class ClassDef(QualifiedNameMixin, base.FilterStmtsMixin, metaclass will be done. """ - values = self.locals.get(name, []) + self.external_attrs.get(name, []) - if name in self.special_attributes and class_context: + local_values = self.locals.get(name, []) + external_values = self.external_attrs.get(name, []) + values = local_values + external_values + + # Determine if we should look retrieve special attributes. + # If a class has local values with the given name and that given + # name is also a special attribute, then priority should be given + # to the local defined value, irrespective of the underlying + # potential attributes defined by the special model. + # But this is not the case for builtins, for which the + # value can't be redefined locally. In the case of builtins though, + # we always look into the special method and for the rest, + # we only look if there is no local value defined with the same name. + is_builtin = self.root().name == BUILTINS + look_special_attributes = is_builtin or not local_values + + if name in self.special_attributes and class_context and look_special_attributes: result = [self.special_attributes.lookup(name)] if name == '__bases__': # Need special treatment, since they are mutable @@ -1536,9 +1553,11 @@ class ClassDef(QualifiedNameMixin, base.FilterStmtsMixin, if class_context: values += self._metaclass_lookup_attribute(name, context) + if not values: raise exceptions.AttributeInferenceError(target=self, attribute=name, context=context) + return values def _metaclass_lookup_attribute(self, name, context): |
