diff options
author | Claudiu Popa <cpopa@cloudbasesolutions.com> | 2015-05-12 16:43:14 +0300 |
---|---|---|
committer | Claudiu Popa <cpopa@cloudbasesolutions.com> | 2015-05-12 16:43:14 +0300 |
commit | 2b39c2e00bc56155351df44fb9e7b44443a81742 (patch) | |
tree | a769d9b89bfabf1ffa14fd0f3b7b0a1beb396576 /pylint/checkers/utils.py | |
parent | 35543eb71299989dbf13f2d22ebdba477e879ae0 (diff) | |
download | pylint-2b39c2e00bc56155351df44fb9e7b44443a81742.tar.gz |
Improve the detection of undefined variables in function arguments
This patch involves a lot of subtle changes:
* is_defined_before takes in consideration the fact that its argument
node is a default value for a Lambda / Function.
If it is a default value, then the search is left to the parent of
the lambda. Basically for x in the following, the result of is_defined_before
will be determined by foo:
def foo():
x = 42
func = lambda x=x: x * x
return func
* there's another special rule for undefined-variable which verifies that
a node is used in Arguments.defaults and if the actual definition
is happening before the Arguments statement, basically trying
to understand the following:
class A:
x = 42
foo = lambda x=x: x * x
* undefined variable is emitted if both scope types, for the node
analyzed and for its actual definition, is they are both lambda
scopes.
Closes issue #404.
Diffstat (limited to 'pylint/checkers/utils.py')
-rw-r--r-- | pylint/checkers/utils.py | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/pylint/checkers/utils.py b/pylint/checkers/utils.py index 0dbf6cb..59708cc 100644 --- a/pylint/checkers/utils.py +++ b/pylint/checkers/utils.py @@ -160,6 +160,16 @@ def is_defined_before(var_node): return True elif isinstance(_node, (astroid.Lambda, astroid.Function)): if _node.args.is_argument(varname): + # If the name is found inside a default value + # of a function, then let the search continue + # in the parent's tree. + if _node.args.parent_of(var_node): + try: + _node.args.default_value(varname) + _node = _node.parent + continue + except astroid.NoDefault: + pass return True if getattr(_node, 'name', None) == varname: return True |