## Summary This rule flags calls such as `Base.method()` when `method` is an abstract classmethod or staticmethod with a trivial body. The new name describes what a suppression asks ty to ignore (“an abstract method call”), so it better conforms to [Ruff’s rule naming convention](https://docs.astral.sh/ruff/rule-proposals/#rule-naming-convention). `ty: ignore[abstract-method-call]` now makes grammatical sense when read as a sentence in English. `call-abstract-method` remains a compatibility alias. Existing configuration and suppression comments still work, and ty warns when they use the old name.
1.5 KiB
What it does
Checks for calls to abstract @classmethods or @staticmethods with "trivial bodies" when accessed
on the class object itself.
"Trivial bodies" are bodies that solely consist of ..., pass, a docstring, and/or
raise NotImplementedError.
Why is this bad?
An abstract method with a trivial body has no concrete implementation to execute, so calling such a method directly on the class will probably not have the desired effect.
It is also unsound to call these methods directly on the class. Unlike other methods, ty permits
abstract methods with trivial bodies to have non-None return types even though they always return
None at runtime. This is because it is expected that these methods will always be overridden
rather than being called directly. As a result of this exception to the normal rule, ty may infer an
incorrect type if one of these methods is called directly, which may then mean that type errors
elsewhere in your code go undetected by ty.
Calling abstract classmethods or staticmethods via type[X] is allowed, since the actual runtime
type could be a concrete subclass with an implementation.
Example
from abc import ABC, abstractmethod
class Foo(ABC):
@classmethod
@abstractmethod
def method(cls) -> int: ...
# cannot call abstract classmethod
Foo.method() # error
Rule history
-
v0.0.102: Renamed from
call-abstract-method.call-abstract-methodremains as a compatibility alias for this rule.