diff --git a/doc/whatsnew/2.6.rst b/doc/whatsnew/2.6.rst index 5ab2028c163..1485229c614 100644 --- a/doc/whatsnew/2.6.rst +++ b/doc/whatsnew/2.6.rst @@ -23,3 +23,5 @@ Other Changes * The `no-space-check` option has been removed, it's no longer possible to consider empty line like a `trailing-whitespace` by using clever options. * `mixed-indentation` has been removed, it is no longer useful since TabError is included directly in python3 + +* New option `allow-redefined-builtins` to configure variable names allowed to shadow builtins diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py index 28dc2178f10..075ccd76896 100644 --- a/pylint/checkers/variables.py +++ b/pylint/checkers/variables.py @@ -674,6 +674,15 @@ class VariablesChecker(BaseChecker): "help": "Tells whether unused global variables should be treated as a violation.", }, ), + ( + "allow-redefined-builtins", + { + "default": (), + "type": "csv", + "metavar": "", + "help": "List of names allowed to shadow builtins", + }, + ), ) def __init__(self, linter=None): @@ -848,8 +857,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) @@ -1733,6 +1744,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.allow_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..f52b281890c --- /dev/null +++ b/tests/functional/r/redefined_builtin_allowed.py @@ -0,0 +1,7 @@ +"""Tests for redefining builtins.""" + +def function(): + """Allow some redefines.""" + dir = "path" # allowed in config + dict = "bad" # [redefined-builtin] + print(dir, dict) diff --git a/tests/functional/r/redefined_builtin_allowed.rc b/tests/functional/r/redefined_builtin_allowed.rc new file mode 100644 index 00000000000..1b7fe8d7ecb --- /dev/null +++ b/tests/functional/r/redefined_builtin_allowed.rc @@ -0,0 +1,2 @@ +[variables] +allow-redefined-builtins = dir diff --git a/tests/functional/r/redefined_builtin_allowed.txt b/tests/functional/r/redefined_builtin_allowed.txt new file mode 100644 index 00000000000..699de6a8344 --- /dev/null +++ b/tests/functional/r/redefined_builtin_allowed.txt @@ -0,0 +1 @@ +redefined-builtin:6:function:Redefining built-in 'dict'