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 Mar 5, 2021
1 parent 25e63b0 commit 942b02f
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -454,3 +454,5 @@ contributors:
* Lefteris Karapetsas: contributor

* Louis Sautier: contributor

* Alexander Kapshuna: contributor
5 changes: 3 additions & 2 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -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?
===========================
Expand All @@ -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
Expand Down Expand Up @@ -298,7 +300,6 @@ What's New in Pylint 2.5.4?

Close #3564


What's New in Pylint 2.5.3?
===========================

Expand Down
2 changes: 2 additions & 0 deletions doc/whatsnew/2.7.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
18 changes: 16 additions & 2 deletions pylint/checkers/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -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": "<comma separated list>",
"help": "List of names allowed to shadow builtins",
},
),
)

def __init__(self, linter=None):
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions tests/functional/r/redefined_builtin_allowed.py
Original file line number Diff line number Diff line change
@@ -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]
4 changes: 4 additions & 0 deletions tests/functional/r/redefined_builtin_allowed.rc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[messages control]
disable = invalid-name
[variables]
allowed-redefined-builtins = dir, list
2 changes: 2 additions & 0 deletions tests/functional/r/redefined_builtin_allowed.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
redefined-builtin:6:4:function:Redefining built-in 'dict'
redefined-builtin:9:0::Redefining built-in 'list'

0 comments on commit 942b02f

Please sign in to comment.