diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index 197b8da9f8..395c804841 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -454,3 +454,5 @@ contributors: * Lefteris Karapetsas: contributor * Louis Sautier: contributor + +* Alexander Kapshuna: contributor diff --git a/ChangeLog b/ChangeLog index 5ba13b7755..6af40d64be 100644 --- a/ChangeLog +++ b/ChangeLog @@ -21,6 +21,9 @@ Release date: TBA Closes #4149 +* Add `allowed-redefined-builtins` option for fine tuning `redefined-builtin` check. + + Close #3263 What's New in Pylint 2.7.2? =========================== @@ -35,7 +38,6 @@ Release date: 2021-02-28 * Workflow and packaging improvements - What's New in Pylint 2.7.1? =========================== Release date: 2021-02-23 @@ -298,7 +300,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 3d5d77ac3e..5729e499e4 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 c7fd631848..3d7eadc20d 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 0000000000..ec7697dfce --- /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 0000000000..845729dcee --- /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 0000000000..cd97f3249f --- /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'