Respect self as positional-only argument in annotation rules (#2927)

This commit is contained in:
Charlie Marsh 2023-02-15 10:25:17 -05:00 committed by GitHub
parent 57a5071b4e
commit cb971d3a48
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 4 deletions

View file

@ -103,3 +103,7 @@ class Foo:
@classmethod
def foo(cls, a: int, b: int) -> int:
pass
# ANN101
def foo(self, /, a: int, b: int) -> int:
pass

View file

@ -475,9 +475,9 @@ pub fn definition(
// ANN001, ANN401
for arg in args
.args
.posonlyargs
.iter()
.chain(args.posonlyargs.iter())
.chain(args.args.iter())
.chain(args.kwonlyargs.iter())
.skip(
// If this is a non-static method, skip `cls` or `self`.
@ -581,7 +581,7 @@ pub fn definition(
// ANN101, ANN102
if is_method && !visibility::is_staticmethod(checker, cast::decorator_list(stmt)) {
if let Some(arg) = args.args.first() {
if let Some(arg) = args.posonlyargs.first().or_else(|| args.args.first()) {
if arg.node.annotation.is_none() {
if visibility::is_classmethod(checker, cast::decorator_list(stmt)) {
if checker.settings.rules.enabled(&Rule::MissingTypeCls) {

View file

@ -244,4 +244,15 @@ expression: diagnostics
column: 15
fix: ~
parent: ~
- kind:
MissingTypeSelf:
name: self
location:
row: 108
column: 12
end_location:
row: 108
column: 16
fix: ~
parent: ~

View file

@ -225,7 +225,6 @@ fn sorted_child_nodes_inner<'a>(node: &Node<'a>, result: &mut Vec<Node<'a>>) {
for decorator in decorator_list {
result.push(Node::Expr(decorator));
}
// TODO(charlie): Retain order.
for arg in &args.posonlyargs {
if let Some(expr) = &arg.node.annotation {
result.push(Node::Expr(expr));