Support PEP 562 (#841)

This commit is contained in:
Charlie Marsh
2022-11-20 17:55:57 -05:00
committed by GitHub
parent f96c64b40d
commit 1a3d2ead41
3 changed files with 34 additions and 0 deletions

View File

@@ -10,6 +10,27 @@ def good__():
pass
def nested():
def __bad__():
pass
def __good():
pass
def good__():
pass
class Class:
def __good__(self):
pass
# https://peps.python.org/pep-0562/
def __getattr__(name):
pass
# https://peps.python.org/pep-0562/
def __dir__():
pass

View File

@@ -132,6 +132,11 @@ pub fn dunder_function_name(scope: &Scope, stmt: &Stmt, name: &str) -> Option<Ch
return None;
}
if name.starts_with("__") && name.ends_with("__") {
// Allowed under PEP 562 (https://peps.python.org/pep-0562/).
if matches!(scope.kind, ScopeKind::Module) && (name == "__getattr__" || name == "__dir__") {
return None;
}
return Some(Check::new(
CheckKind::DunderFunctionName,
Range::from_located(stmt),

View File

@@ -10,4 +10,12 @@ expression: checks
row: 5
column: 0
fix: ~
- kind: DunderFunctionName
location:
row: 14
column: 4
end_location:
row: 17
column: 4
fix: ~