-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add bad-dunder-name
checker
#7642
Merged
Pierre-Sassoulas
merged 10 commits into
pylint-dev:main
from
clavedeluna:3038-special-dunder
Oct 31, 2022
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
40b0d77
add `bad-dunder-name` check
clavedeluna d6c7557
should flag private, not protected, methods
clavedeluna d28502d
add extra dunder methods to check for names
clavedeluna 720bfba
bad-dunder-name recognizes properties
clavedeluna 54db3e4
detect __post_init__ name
clavedeluna 682f6c1
add more test examples
clavedeluna 4826538
bad dunder can start with _
clavedeluna 37ca3dd
move checker to an extension
clavedeluna 43ba022
add good-dunder-names option
clavedeluna 6c940a3
Update doc/whatsnew/fragments/3038.new_check
Pierre-Sassoulas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class Apples: | ||
def _init_(self): # [bad-dunder-name] | ||
pass | ||
|
||
def __hello__(self): # [bad-dunder-name] | ||
print("hello") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class Apples: | ||
def __init__(self): | ||
pass | ||
|
||
def hello(self): | ||
print("hello") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[MAIN] | ||
load-plugins=pylint.extensions.dunder |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Added ``bad-dunder-name`` extension check, which flags bad or misspelled dunder methods. | ||
You can use the ``good-dunder-names`` option to allow specific dunder names. | ||
|
||
Closes #3038 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html | ||
# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE | ||
# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt | ||
|
||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
from astroid import nodes | ||
|
||
from pylint.checkers import BaseChecker | ||
from pylint.constants import DUNDER_METHODS, DUNDER_PROPERTIES, EXTRA_DUNDER_METHODS | ||
from pylint.interfaces import HIGH | ||
|
||
if TYPE_CHECKING: | ||
from pylint.lint import PyLinter | ||
|
||
|
||
class DunderChecker(BaseChecker): | ||
"""Checks related to dunder methods.""" | ||
|
||
name = "dunder" | ||
priority = -1 | ||
msgs = { | ||
"W3201": ( | ||
"Bad or misspelled dunder method name %s.", | ||
"bad-dunder-name", | ||
"Used when a dunder method is misspelled or defined with a name " | ||
"not within the predefined list of dunder names.", | ||
), | ||
} | ||
options = ( | ||
( | ||
"good-dunder-names", | ||
{ | ||
"default": [], | ||
"type": "csv", | ||
"metavar": "<comma-separated names>", | ||
"help": "Good dunder names which should always be accepted.", | ||
}, | ||
), | ||
) | ||
|
||
def open(self) -> None: | ||
self._dunder_methods = ( | ||
EXTRA_DUNDER_METHODS | ||
+ DUNDER_PROPERTIES | ||
+ self.linter.config.good_dunder_names | ||
) | ||
for since_vers, dunder_methods in DUNDER_METHODS.items(): | ||
if since_vers <= self.linter.config.py_version: | ||
self._dunder_methods.extend(list(dunder_methods.keys())) | ||
|
||
def visit_functiondef(self, node: nodes.FunctionDef) -> None: | ||
"""Check if known dunder method is misspelled or dunder name is not one | ||
of the pre-defined names. | ||
""" | ||
# ignore module-level functions | ||
if not node.is_method(): | ||
return | ||
|
||
# Detect something that could be a bad dunder method | ||
if ( | ||
node.name.startswith("_") | ||
and node.name.endswith("_") | ||
and node.name not in self._dunder_methods | ||
): | ||
self.add_message( | ||
"bad-dunder-name", | ||
node=node, | ||
args=(node.name), | ||
confidence=HIGH, | ||
) | ||
|
||
|
||
def register(linter: PyLinter) -> None: | ||
linter.register_checker(DunderChecker(linter)) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I missed the discussion here, but is there a reason why we don't check for
__
? Could_test_
be something private?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A function starting with
_
is protected, a function starting with__
is private those should not raise. But_init__
is a bad dunder name that we should detect so a single underscore is enough.