mirror of https://github.com/astral-sh/ruff
Treat @staticmethod as higher-precedence than ABC (#2635)
This commit is contained in:
parent
2bc16eb4e3
commit
8ee51eb5c6
|
|
@ -46,6 +46,10 @@ class MetaClass(ABCMeta):
|
||||||
def good_method(cls):
|
def good_method(cls):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def static_method(not_cls) -> bool:
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
def func(x):
|
def func(x):
|
||||||
return x
|
return x
|
||||||
|
|
|
||||||
|
|
@ -26,8 +26,18 @@ pub fn classify(
|
||||||
let ScopeKind::Class(scope) = &scope.kind else {
|
let ScopeKind::Class(scope) = &scope.kind else {
|
||||||
return FunctionType::Function;
|
return FunctionType::Function;
|
||||||
};
|
};
|
||||||
// Special-case class method, like `__new__`.
|
if decorator_list.iter().any(|expr| {
|
||||||
if CLASS_METHODS.contains(&name)
|
// The method is decorated with a static method decorator (like
|
||||||
|
// `@staticmethod`).
|
||||||
|
checker.resolve_call_path(expr).map_or(false, |call_path| {
|
||||||
|
staticmethod_decorators
|
||||||
|
.iter()
|
||||||
|
.any(|decorator| call_path == to_call_path(decorator))
|
||||||
|
})
|
||||||
|
}) {
|
||||||
|
FunctionType::StaticMethod
|
||||||
|
} else if CLASS_METHODS.contains(&name)
|
||||||
|
// Special-case class method, like `__new__`.
|
||||||
|| scope.bases.iter().any(|expr| {
|
|| scope.bases.iter().any(|expr| {
|
||||||
// The class itself extends a known metaclass, so all methods are class methods.
|
// The class itself extends a known metaclass, so all methods are class methods.
|
||||||
checker.resolve_call_path(expr).map_or(false, |call_path| {
|
checker.resolve_call_path(expr).map_or(false, |call_path| {
|
||||||
|
|
@ -46,16 +56,6 @@ pub fn classify(
|
||||||
})
|
})
|
||||||
{
|
{
|
||||||
FunctionType::ClassMethod
|
FunctionType::ClassMethod
|
||||||
} else if decorator_list.iter().any(|expr| {
|
|
||||||
// The method is decorated with a static method decorator (like
|
|
||||||
// `@staticmethod`).
|
|
||||||
checker.resolve_call_path(expr).map_or(false, |call_path| {
|
|
||||||
staticmethod_decorators
|
|
||||||
.iter()
|
|
||||||
.any(|decorator| call_path == to_call_path(decorator))
|
|
||||||
})
|
|
||||||
}) {
|
|
||||||
FunctionType::StaticMethod
|
|
||||||
} else {
|
} else {
|
||||||
// It's an instance method.
|
// It's an instance method.
|
||||||
FunctionType::Method
|
FunctionType::Method
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue