Remove some additional manual iterator matches (#5482)

## Summary

I've done a few of these PRs, I thought I'd caught them all, but missed
this pattern.
This commit is contained in:
Charlie Marsh 2023-07-03 12:29:59 -04:00 committed by GitHub
parent dadad0e9ed
commit 00fbbe4223
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 75 additions and 69 deletions

View file

@ -6,10 +6,7 @@ use ruff_python_ast::helpers::map_callable;
use crate::model::SemanticModel;
use crate::scope::{Scope, ScopeKind};
const CLASS_METHODS: [&str; 3] = ["__new__", "__init_subclass__", "__class_getitem__"];
const METACLASS_BASES: [(&str, &str); 2] = [("", "type"), ("abc", "ABCMeta")];
#[derive(Copy, Clone)]
#[derive(Debug, Copy, Clone)]
pub enum FunctionType {
Function,
Method,
@ -44,24 +41,28 @@ pub fn classify(
})
}) {
FunctionType::StaticMethod
} else if CLASS_METHODS.contains(&name)
// Special-case class method, like `__new__`.
} else if matches!(name, "__new__" | "__init_subclass__" | "__class_getitem__")
// Special-case class method, like `__new__`.
|| scope.bases.iter().any(|expr| {
// The class itself extends a known metaclass, so all methods are class methods.
semantic.resolve_call_path(map_callable(expr)).map_or(false, |call_path| {
METACLASS_BASES
.iter()
.any(|(module, member)| call_path.as_slice() == [*module, *member])
})
semantic
.resolve_call_path(map_callable(expr))
.map_or(false, |call_path| {
matches!(call_path.as_slice(), ["", "type"] | ["abc", "ABCMeta"])
})
})
|| decorator_list.iter().any(|decorator| {
// The method is decorated with a class method decorator (like `@classmethod`).
semantic.resolve_call_path(map_callable(&decorator.expression)).map_or(false, |call_path| {
matches!(call_path.as_slice(), ["", "classmethod"] | ["abc", "abstractclassmethod"]) ||
classmethod_decorators
.iter()
.any(|decorator| call_path == from_qualified_name(decorator))
})
semantic
.resolve_call_path(map_callable(&decorator.expression))
.map_or(false, |call_path| {
matches!(
call_path.as_slice(),
["", "classmethod"] | ["abc", "abstractclassmethod"]
) || classmethod_decorators
.iter()
.any(|decorator| call_path == from_qualified_name(decorator))
})
})
{
FunctionType::ClassMethod