diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index 2b0cf022bde..3bcff001626 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -452,3 +452,5 @@ contributors: * Tiago Honorato: contributor * Lefteris Karapetsas: contributor + +* Alexander Kapshuna: contributor diff --git a/ChangeLog b/ChangeLog index df57bcf5719..f9c5da7d79b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -12,6 +12,9 @@ What's New in Pylint 2.7.2? .. Put bug fixes that will be cherry-picked to latest major version here +* Add `allowed-redefined-builtins` option for fine tuning `redefined-builtin` check. + + Close #3263 What's New in Pylint 2.7.1? =========================== @@ -275,7 +278,6 @@ What's New in Pylint 2.5.4? Close #3564 - What's New in Pylint 2.5.3? =========================== diff --git a/doc/whatsnew/2.7.rst b/doc/whatsnew/2.7.rst index 3d5d77ac3e6..5729e499e49 100644 --- a/doc/whatsnew/2.7.rst +++ b/doc/whatsnew/2.7.rst @@ -54,3 +54,5 @@ Other Changes * `len-as-conditions` is now triggered only for classes that are inheriting directly from list, dict, or set and not implementing the `__bool__` function, or from generators like range or list/dict/set comprehension. This should reduce the false positive for other classes, like pandas's DataFrame or numpy's Array. * Fixes duplicate code detection for --jobs=2+ + +* New option `allowed-redefined-builtins` defines variable names allowed to shadow builtins. diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py index c7fd6318483..3d7eadc20d4 100644 --- a/pylint/checkers/variables.py +++ b/pylint/checkers/variables.py @@ -667,6 +667,15 @@ class VariablesChecker(BaseChecker): "help": "Tells whether unused global variables should be treated as a violation.", }, ), + ( + "allowed-redefined-builtins", + { + "default": (), + "type": "csv", + "metavar": "", + "help": "List of names allowed to shadow builtins", + }, + ), ) def __init__(self, linter=None): @@ -829,8 +838,10 @@ def visit_functiondef(self, node): "redefined-outer-name", args=(name, line), node=stmt ) - elif utils.is_builtin(name) and not self._should_ignore_redefined_builtin( - stmt + elif ( + utils.is_builtin(name) + and not self._allowed_redefined_builtin(name) + and not self._should_ignore_redefined_builtin(stmt) ): # do not print Redefining builtin for additional builtins self.add_message("redefined-builtin", args=name, node=stmt) @@ -1752,6 +1763,9 @@ def _should_ignore_redefined_builtin(self, stmt): return False return stmt.modname in self.config.redefining_builtins_modules + def _allowed_redefined_builtin(self, name): + return name in self.config.allowed_redefined_builtins + def _has_homonym_in_upper_function_scope(self, node, index): """ Return True if there is a node with the same name in the to_consume dict of an upper scope diff --git a/tests/functional/r/redefined_builtin_allowed.py b/tests/functional/r/redefined_builtin_allowed.py new file mode 100644 index 00000000000..ec7697dfce3 --- /dev/null +++ b/tests/functional/r/redefined_builtin_allowed.py @@ -0,0 +1,9 @@ +"""Tests for redefining builtins.""" + +def function(): + """Allow some redefines.""" + dir = "path" # allowed in config + dict = "wrong" # [redefined-builtin] + print(dir, dict) + +list = "not in globals" # [redefined-builtin] diff --git a/tests/functional/r/redefined_builtin_allowed.rc b/tests/functional/r/redefined_builtin_allowed.rc new file mode 100644 index 00000000000..845729dceea --- /dev/null +++ b/tests/functional/r/redefined_builtin_allowed.rc @@ -0,0 +1,4 @@ +[messages control] +disable = invalid-name +[variables] +allowed-redefined-builtins = dir, list diff --git a/tests/functional/r/redefined_builtin_allowed.txt b/tests/functional/r/redefined_builtin_allowed.txt new file mode 100644 index 00000000000..cd97f3249fd --- /dev/null +++ b/tests/functional/r/redefined_builtin_allowed.txt @@ -0,0 +1,2 @@ +redefined-builtin:6:4:function:Redefining built-in 'dict' +redefined-builtin:9:0::Redefining built-in 'list'