diff options
author | cpopa <devnull@localhost> | 2014-07-12 12:08:41 +0300 |
---|---|---|
committer | cpopa <devnull@localhost> | 2014-07-12 12:08:41 +0300 |
commit | 235ee6dd14de0678d4f219bd57624c10af816cd0 (patch) | |
tree | cd0988774c4c11fb203731be90b83f525c092ad8 | |
parent | 738cfea5cdc05ee4a1cc7914188d507dfebcfea3 (diff) | |
download | pylint-235ee6dd14de0678d4f219bd57624c10af816cd0.tar.gz |
Don't emit 'no-name-in-module' for ignored modules. Closes issue #223.
-rw-r--r-- | ChangeLog | 3 | ||||
-rw-r--r-- | checkers/variables.py | 8 | ||||
-rw-r--r-- | test/unittest_checker_variables.py | 15 | ||||
-rw-r--r-- | testutils.py | 3 | ||||
-rw-r--r-- | utils.py | 20 |
5 files changed, 47 insertions, 2 deletions
@@ -33,6 +33,9 @@ ChangeLog for Pylint * Add 'assigning-non-slot' warning, which detects assignments to attributes not defined in slots. + * Don't emit 'no-name-in-module' for ignored modules. + Closes issue #223. + 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 b84ad4f..6cbcdb6 100644 --- a/checkers/variables.py +++ b/checkers/variables.py @@ -25,6 +25,7 @@ from astroid import are_exclusive, builtin_lookup, AstroidBuildingException from logilab.common.modutils import file_from_modpath from pylint.interfaces import IAstroidChecker +from pylint.utils import get_global_option from pylint.checkers import BaseChecker from pylint.checkers.utils import (PYMETHODS, is_ancestor_name, is_builtin, is_defined_before, is_error, is_func_default, is_func_decorator, @@ -679,6 +680,8 @@ builtins. Remember that you should avoid to define new builtins when possible.' if the latest access name corresponds to a module, return it """ assert isinstance(module, astroid.Module), module + ignored_modules = get_global_option(self, 'ignored-modules', + default=[]) while module_names: name = module_names.pop(0) if name == '__dict__': @@ -689,7 +692,10 @@ builtins. Remember that you should avoid to define new builtins when possible.' if module is astroid.YES: return None except astroid.NotFoundError: - self.add_message('no-name-in-module', args=(name, module.name), node=node) + if module.name in ignored_modules: + return None + self.add_message('no-name-in-module', + args=(name, module.name), node=node) return None except astroid.InferenceError: return None diff --git a/test/unittest_checker_variables.py b/test/unittest_checker_variables.py index 30bc5e6..20a0d9e 100644 --- a/test/unittest_checker_variables.py +++ b/test/unittest_checker_variables.py @@ -4,7 +4,7 @@ import os from astroid import test_utils from pylint.checkers import variables -from pylint.testutils import CheckerTestCase, linter +from pylint.testutils import CheckerTestCase, linter, set_config class VariablesCheckerTC(CheckerTestCase): @@ -22,6 +22,19 @@ class VariablesCheckerTC(CheckerTestCase): with self.assertNoMessages(): self.walk(module) + @set_config(ignored_modules=('argparse',)) + def test_no_name_in_module_skipped(self): + """Make sure that 'from ... import ...' does not emit a + 'no-name-in-module' with a module that is configured + to be ignored. + """ + + node = test_utils.extract_node(""" + from argparse import THIS_does_not_EXIST + """) + with self.assertNoMessages(): + self.checker.visit_from(node) + class MissingSubmoduleTest(CheckerTestCase): CHECKER_CLASS = variables.VariablesChecker diff --git a/testutils.py b/testutils.py index fb8170a..daf7477 100644 --- a/testutils.py +++ b/testutils.py @@ -152,6 +152,9 @@ class UnittestLinter(object): self.stats[name] = value return self.stats + @property + def options_providers(self): + return linter.options_providers def set_config(**kwargs): """Decorator for setting config values on a checker.""" @@ -742,3 +742,23 @@ def register_plugins(linter, directory): module.register(linter) imported[base] = 1 +def get_global_option(checker, option, default=None): + """ Retrieve an option defined by the given *checker* or + by all known option providers. + + It will look in the list of all options providers + until the given *option* will be found. + If the option wasn't found, the *default* value will be returned. + """ + # First, try in the given checker's config. + # After that, look in the options providers. + + try: + return getattr(checker.config, option.replace("-", "_")) + except AttributeError: + pass + for provider in checker.linter.options_providers: + for options in provider.options: + if options[0] == option: + return getattr(provider.config, option.replace("-", "_")) + return default |