summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaudiu Popa <pcmanticore@gmail.com>2016-07-28 21:57:56 +0300
committerClaudiu Popa <pcmanticore@gmail.com>2016-07-28 21:57:56 +0300
commit871a55bb4c80acfb62579a5da7235d800bcd7a90 (patch)
treeede7905fe1dab4068475e1c81ba2aa5bcdd52e53
parent86e0743ead0ee08bfd7a5ce32c5f76785d633117 (diff)
downloadastroid-git-871a55bb4c80acfb62579a5da7235d800bcd7a90.tar.gz
Try to patch special_attributes instead of returning a new node
It seems we have some problems with transforms that are creating new nodes. While they work okay in most of the cases, we found a problem with regard to pylint. For instance, if we recreate a function and return it from a transform, pylint can see that we had two instances of the same function in the module locals and will emit an error saying that the function is redefined. In fact, the old node is still available in the module locals, while it should have been replaced. Unfortunately, there is not much we can do right now, instead of having something as the zipper integrated. This patch would fix the undefined-variable false positives we had in pylint.
-rw-r--r--astroid/brain/brain_functools.py20
1 files changed, 8 insertions, 12 deletions
diff --git a/astroid/brain/brain_functools.py b/astroid/brain/brain_functools.py
index cbd563f2..6dcf7cd9 100644
--- a/astroid/brain/brain_functools.py
+++ b/astroid/brain/brain_functools.py
@@ -42,19 +42,15 @@ class LruWrappedModel(objectmodel.FunctionModel):
return BoundMethod(proxy=node, bound=self._instance.parent.scope())
-
-class LruWrappedFunctionDef(astroid.FunctionDef):
- special_attributes = LruWrappedModel()
-
-
def _transform_lru_cache(node, context=None):
- # TODO: this needs the zipper, because the new node's attributes
- # will still point to the old node.
- new_func = LruWrappedFunctionDef(name=node.name, doc=node.name,
- lineno=node.lineno, col_offset=node.col_offset,
- parent=node.parent)
- new_func.postinit(node.args, node.body, node.decorators, node.returns)
- return new_func
+ # TODO: this is not ideal, since the node should be immutable,
+ # but due to https://github.com/PyCQA/astroid/issues/354,
+ # there's not much we can do now.
+ # Replacing the node would work partially, because,
+ # in pylint, the old node would still be available, leading
+ # to spurious false positives.
+ node.special_attributes = LruWrappedModel()(node)
+ return
def _looks_like_lru_cache(node):