diff options
author | Alexander Kapshuna <kapsh@kap.sh> | 2021-03-05 12:07:45 +0300 |
---|---|---|
committer | Pierre Sassoulas <pierre.sassoulas@gmail.com> | 2021-03-05 10:19:38 +0100 |
commit | 68309155f0da806f9d5dc97fd2d59168869245fe (patch) | |
tree | d842f7146bf00eca403a36ca837c8281e8571855 | |
parent | 25e63b0606721e2cd07d4c2b3d88fcb0461b6db9 (diff) | |
download | pylint-git-68309155f0da806f9d5dc97fd2d59168869245fe.tar.gz |
Add allow-redefined-builtins option to variable checker
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 #3263
-rw-r--r-- | CONTRIBUTORS.txt | 2 | ||||
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | doc/whatsnew/2.7.rst | 2 | ||||
-rw-r--r-- | pylint/checkers/variables.py | 18 | ||||
-rw-r--r-- | tests/functional/r/redefined_builtin_allowed.py | 9 | ||||
-rw-r--r-- | tests/functional/r/redefined_builtin_allowed.rc | 4 | ||||
-rw-r--r-- | tests/functional/r/redefined_builtin_allowed.txt | 2 |
7 files changed, 38 insertions, 4 deletions
diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index 197b8da9f..395c80484 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -454,3 +454,5 @@ contributors: * Lefteris Karapetsas: contributor * Louis Sautier: contributor + +* Alexander Kapshuna: contributor @@ -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 3d5d77ac3..5729e499e 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 c7fd63184..3d7eadc20 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": "<comma separated list>", + "help": "List of names allowed to shadow builtins", + }, + ), ) def __init__(self, linter=None): @@ -829,8 +838,10 @@ class VariablesChecker(BaseChecker): "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 @@ class VariablesChecker(BaseChecker): 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 000000000..ec7697dfc --- /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 000000000..845729dce --- /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 000000000..cd97f3249 --- /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' |