From f12378d90b206278b704624fadcdcbea15d13463 Mon Sep 17 00:00:00 2001 From: cpopa Date: Wed, 23 Jul 2014 09:25:37 +0200 Subject: Don't use set comprehension. --- checkers/strings.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/checkers/strings.py b/checkers/strings.py index 4fe16dd..ad63580 100644 --- a/checkers/strings.py +++ b/checkers/strings.py @@ -327,10 +327,10 @@ class StringMethodsChecker(BaseChecker): self.add_message('bad-format-string', node=node) return - manual_fields = {field[0] for field in fields - if isinstance(field[0], int)} - named_fields = {field[0] for field in fields - if isinstance(field[0], str)} + manual_fields = set(field[0] for field in fields + if isinstance(field[0], int)) + named_fields = set(field[0] for field in fields + if isinstance(field[0], str)) if manual_fields and num_args: self.add_message('format-combined-specification', node=node) -- cgit v1.2.1 From 4bdb95ad8d3d14fd419119c7ae379da3bddcb126 Mon Sep 17 00:00:00 2001 From: Sylvain Th?nault Date: Wed, 23 Jul 2014 12:36:42 +0200 Subject: stop having ASTWalker as BaseChecker base class, it's not used anymore (and for a while) --- checkers/__init__.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/checkers/__init__.py b/checkers/__init__.py index af7965b..693a5ff 100644 --- a/checkers/__init__.py +++ b/checkers/__init__.py @@ -42,7 +42,6 @@ import sys import tokenize import warnings -from astroid.utils import ASTWalker from logilab.common.configuration import OptionsProviderMixIn from pylint.reporters import diff_string @@ -69,7 +68,7 @@ def table_lines_from_stats(stats, old_stats, columns): return lines -class BaseChecker(OptionsProviderMixIn, ASTWalker): +class BaseChecker(OptionsProviderMixIn): """base class for checkers""" # checker name (you may reuse an existing one) name = None @@ -87,7 +86,6 @@ class BaseChecker(OptionsProviderMixIn, ASTWalker): linter is an object implementing ILinter """ - ASTWalker.__init__(self, self) self.name = self.name.lower() OptionsProviderMixIn.__init__(self) self.linter = linter -- cgit v1.2.1 From 1df3bf598b0f5256802cb1d5486be3bffde20632 Mon Sep 17 00:00:00 2001 From: cpopa Date: Wed, 23 Jul 2014 13:17:29 +0200 Subject: Don't emit 'unused-variable' when assigning to a nonlocal. Closes issue #275. --- ChangeLog | 3 +++ checkers/variables.py | 6 ++++++ test/input/func_noerror_unused_variable_py30.py | 14 ++++++++++++++ 3 files changed, 23 insertions(+) create mode 100644 test/input/func_noerror_unused_variable_py30.py diff --git a/ChangeLog b/ChangeLog index 27b7380..0c8add8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -42,6 +42,9 @@ ChangeLog for Pylint * Definition order is considered for classes, function arguments and annotations. Closes issue #257. + * Don't emit 'unused-variable' when assigning to a nonlocal. + Closes issue #275. + 2014-04-30 -- 1.2.1 * Restore the ability to specify the init-hook option via the configuration file, which was accidentally broken in 1.2.0. diff --git a/checkers/variables.py b/checkers/variables.py index 29f9529..4150706 100644 --- a/checkers/variables.py +++ b/checkers/variables.py @@ -422,8 +422,11 @@ builtins. Remember that you should avoid to define new builtins when possible.' called_overridden = False argnames = node.argnames() global_names = set() + nonlocal_names = set() for global_stmt in node.nodes_of_class(astroid.Global): global_names.update(set(global_stmt.names)) + for nonlocal_stmt in node.nodes_of_class(astroid.Nonlocal): + nonlocal_names.update(set(nonlocal_stmt.names)) for name, stmts in not_consumed.iteritems(): # ignore some special names specified by user configuration @@ -470,6 +473,9 @@ builtins. Remember that you should avoid to define new builtins when possible.' continue self.add_message('unused-argument', args=name, node=stmt) else: + if stmt.parent and isinstance(stmt.parent, astroid.Assign): + if name in nonlocal_names: + continue self.add_message('unused-variable', args=name, node=stmt) @check_messages('global-variable-undefined', 'global-variable-not-assigned', 'global-statement', diff --git a/test/input/func_noerror_unused_variable_py30.py b/test/input/func_noerror_unused_variable_py30.py new file mode 100644 index 0000000..ffcc978 --- /dev/null +++ b/test/input/func_noerror_unused_variable_py30.py @@ -0,0 +1,14 @@ +""" Test nonlocal uses and unused-variable. """ + +__revision__ = 1 + +def test_nonlocal(): + """ Test that assigning to a nonlocal does not trigger + an 'unused-variable' warnings. + """ + attr = True + def set_value(val): + """ Set the value in a nonlocal. """ + nonlocal attr + attr = val + return set_value -- cgit v1.2.1