Skip to content

Commit

Permalink
Add allow-redefined-builtins option to variable checker
Browse files Browse the repository at this point in the history
Some builtins have little-to-no use in application code while being
convenient as variables names (e.g. id, dir). New option allows
to configure allowed to override names for redefined-builtin checker.

Closes pylint-dev#3263
  • Loading branch information
kapsh committed May 23, 2020
1 parent 1fc490c commit c9aa2ea
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 2 deletions.
2 changes: 2 additions & 0 deletions doc/whatsnew/2.6.rst
Expand Up @@ -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
18 changes: 16 additions & 2 deletions pylint/checkers/variables.py
Expand Up @@ -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": "<comma separated list>",
"help": "List of names allowed to shadow builtins",
},
),
)

def __init__(self, linter=None):
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions 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)
2 changes: 2 additions & 0 deletions tests/functional/r/redefined_builtin_allowed.rc
@@ -0,0 +1,2 @@
[variables]
allow-redefined-builtins = dir
1 change: 1 addition & 0 deletions tests/functional/r/redefined_builtin_allowed.txt
@@ -0,0 +1 @@
redefined-builtin:6:function:Redefining built-in 'dict'

0 comments on commit c9aa2ea

Please sign in to comment.