# Tests regarding abstract classes ## Instantiation is forbidden Classes with unimplemented abstract methods cannot be instantiated. Type checkers are expected to detect possible attempts to instantiate abstract classes: ```py import abc from typing import Protocol class AbstractBase(abc.ABC): @abc.abstractmethod def bar(self): ... class StillAbstract(AbstractBase): ... # snapshot: abstract-class-instantiation StillAbstract() class AbstractBase2(abc.ABC): @abc.abstractmethod def bar(self): ... @abc.abstractmethod def bar2(self): ... # snapshot: abstract-class-instantiation AbstractBase2() class StillAbstract2(AbstractBase2): ... # error: [abstract-class-instantiation] StillAbstract2() class AbstractBase3(Protocol): def bar(self) -> None: ... class StillAbstract3(AbstractBase3): ... # snapshot: abstract-class-instantiation StillAbstract3() ``` ```snapshot error[abstract-class-instantiation]: Cannot instantiate abstract class `StillAbstract` --> src/mdtest_snippet.py:11:1 | 11 | StillAbstract() | ^^^^^^^^^^^^^^^ `bar` is unimplemented | ::: src/mdtest_snippet.py:5:5 | 5 | / @abc.abstractmethod 6 | | def bar(self): ... | |______________________- `bar` declared as abstract on superclass `AbstractBase` error[abstract-class-instantiation]: Cannot instantiate abstract class `AbstractBase2` --> src/mdtest_snippet.py:20:1 | 20 | AbstractBase2() | ^^^^^^^^^^^^^^^ Abstract methods `bar` and `bar2` are unimplemented | ::: src/mdtest_snippet.py:14:5 | 14 | / @abc.abstractmethod 15 | | def bar(self): ... | |______________________- `bar` declared as abstract error[abstract-class-instantiation]: Cannot instantiate abstract class `StillAbstract3` --> src/mdtest_snippet.py:33:1 | 33 | StillAbstract3() | ^^^^^^^^^^^^^^^^ `bar` is unimplemented | ::: src/mdtest_snippet.py:28:5 | 28 | def bar(self) -> None: ... | -------------------------- `bar` declared as abstract on superclass `AbstractBase3` info: `AbstractBase3.bar` is implicitly abstract because `AbstractBase3` is a `Protocol` class and `bar` lacks an implementation --> src/mdtest_snippet.py:27:7 | 27 | class AbstractBase3(Protocol): | ----------------------- `AbstractBase3` declared here help: Change the body of `bar` to `return` or `return None` if it was not intended to be abstract ``` Abstract methods can be concretely overridden by synthesized methods: ```py from abc import ABC, abstractmethod from dataclasses import dataclass from functools import total_ordering class AbstractOrdered(ABC): @abstractmethod def __lt__(self, other): ... @dataclass(order=True) class ConcreteOrdered(AbstractOrdered): ... ConcreteOrdered() # ok @total_ordering class AlsoConcreteOrdered(AbstractOrdered): def __gt__(self, other): ... # total_ordering does not override a comparison method # if it already exists in the MRO, even if the one that # exists in the MRO is abstract! # # error: [abstract-class-instantiation] AlsoConcreteOrdered() ``` We also allow abstract methods or properties to be "overridden" by a `ClassVar` annotation, even if it is not accompanied by a binding in the class body: we assume that a class like this will have the override added dynamically (e.g., by a metaclass): ```py from typing import ClassVar, Callable class AbstractDynamic(ABC): @property @abstractmethod def f(self) -> int: ... @abstractmethod def g(self) -> str: ... class ConcreteDynamic(AbstractDynamic): f: ClassVar[int] g: ClassVar[Callable[..., str]] ConcreteDynamic() # ok ``` But if the annotation does not use `ClassVar`, we do not see that as overriding the abstract method: ```py class StillAbstractDynamic(AbstractDynamic): f: int g: Callable[..., str] # snapshot: abstract-class-instantiation StillAbstractDynamic() ``` ```snapshot error[abstract-class-instantiation]: Cannot instantiate abstract class `StillAbstractDynamic` --> src/mdtest_snippet.py:76:1 | 76 | StillAbstractDynamic() | ^^^^^^^^^^^^^^^^^^^^^^ Abstract methods `f` and `g` are unimplemented | ::: src/mdtest_snippet.py:62:9 | 62 | def f(self) -> int: ... | - `f` declared as abstract on superclass `AbstractDynamic` info: The instance-attribute annotation for `f` does not override the abstract method help: Either assign a value or add `ClassVar` to this declaration --> src/mdtest_snippet.py:72:5 | 72 | f: int | - Instance-attribute declaration ``` Abstract methods can be overridden by mixin classes, but the concrete override on the mixin must come earlier in the MRO: ```py class AbstractMixin(ABC): @abstractmethod def bar(self): ... class ConcreteMixin: def bar(self): ... class Sub1(AbstractMixin, ConcreteMixin): ... class Sub2(ConcreteMixin, AbstractMixin): ... Sub1() # error: [abstract-class-instantiation] Sub2() # ok ``` When a class has many unimplemented abstract methods, the diagnostic lists only a few unless `--verbose` is enabled. ```py from typing import Protocol class Abstract(Protocol): def aaaaaaaaa(self) -> int: ... def bbbbbbbb(self) -> int: ... def cccccccc(self) -> int: ... def dddddddddd(self) -> int: ... def eeeeeeee(self) -> int: ... def fffffff(self) -> int: ... def ggggggggg(self) -> int: ... def hhhhhhhhh(self) -> int: ... def iiiiiiiiii(self) -> int: ... def kkkkkkkkk(self) -> int: ... class StillSadlyAbstract(Abstract): ... # snapshot: abstract-class-instantiation StillSadlyAbstract() ``` ```snapshot error[abstract-class-instantiation]: Cannot instantiate abstract class `StillSadlyAbstract` --> src/mdtest_snippet.py:106:1 | 106 | StillSadlyAbstract() | ^^^^^^^^^^^^^^^^^^^^ 10 abstract methods are unimplemented, including `aaaaaaaaa`, `bbbbbbbb` and `cccccccc` | ::: src/mdtest_snippet.py:92:5 | 92 | def aaaaaaaaa(self) -> int: ... | ------------------------------- `aaaaaaaaa` declared as abstract on superclass `Abstract` info: Use `--verbose` to see all 10 unimplemented abstract methods info: `Abstract.aaaaaaaaa` is implicitly abstract because `Abstract` is a `Protocol` class and `aaaaaaaaa` lacks an implementation --> src/mdtest_snippet.py:91:7 | 91 | class Abstract(Protocol): | ------------------ `Abstract` declared here ``` ## Verbose diagnostics With `--verbose`, the diagnostic lists every unimplemented abstract method. ```toml verbose = true ``` ```py from typing import Protocol class Abstract(Protocol): def first(self) -> int: ... def second(self) -> int: ... def third(self) -> int: ... def fourth(self) -> int: ... class StillAbstract(Abstract): ... # snapshot: abstract-class-instantiation StillAbstract() ``` ```snapshot error[abstract-class-instantiation]: Cannot instantiate abstract class `StillAbstract` --> src/mdtest_snippet.py:12:1 | 12 | StillAbstract() | ^^^^^^^^^^^^^^^ Abstract methods `first`, `second`, `third` and `fourth` are unimplemented | ::: src/mdtest_snippet.py:4:5 | 4 | def first(self) -> int: ... | --------------------------- `first` declared as abstract on superclass `Abstract` info: `Abstract.first` is implicitly abstract because `Abstract` is a `Protocol` class and `first` lacks an implementation --> src/mdtest_snippet.py:3:7 | 3 | class Abstract(Protocol): | ------------------ `Abstract` declared here info: rule `abstract-class-instantiation` is enabled by default ``` ## Abstract methods without `ABCMeta` The `abstractmethod` decorator requires subclasses to implement a method even if the class does not use `ABCMeta`. A default implementation does not remove that requirement. Invalid constructor calls still retain their inferred instance type for error recovery. ```py from abc import abstractmethod class Abstract: @abstractmethod def method(self) -> int: return 42 # error: [abstract-class-instantiation] reveal_type(Abstract()) # revealed: Abstract class Concrete(Abstract): def method(self) -> int: return super().method() Concrete() # ok ``` ## Generic abstract classes and aliases Specializing or aliasing an abstract class does not implement its abstract methods. A concrete subclass can be instantiated with the same type arguments. ```toml [environment] python-version = "3.12" ``` ```py from abc import ABC, abstractmethod class Abstract[T](ABC): @abstractmethod def method(self) -> T: ... Abstract[int]() # error: [abstract-class-instantiation] Alias = Abstract[str] Alias() # error: [abstract-class-instantiation] class Concrete[T](Abstract[T]): def method(self) -> T: raise NotImplementedError Concrete[int]() # ok ``` ## Constructor calls through `type[]` A `type[Abstract]` parameter can refer to a concrete subclass, so calling it is allowed. We also continue to accept abstract class objects as arguments; this check concerns direct construction, not assignability to `type[]`. ```py from abc import ABC, abstractmethod class Abstract(ABC): @abstractmethod def method(self) -> int: ... class Concrete(Abstract): def method(self) -> int: return 42 def construct(cls: type[Abstract]) -> Abstract: return cls() # ok construct(Concrete) # ok construct(Abstract) # ok ``` ## Abstract property accessors A property remains abstract if its setter or deleter is abstract, even when its getter has a concrete implementation. Replacing the abstract accessor makes the subclass concrete. ```py from abc import ABC, abstractmethod class AbstractSetter(ABC): @property def value(self) -> int: return 0 @value.setter @abstractmethod def value(self, value: int) -> None: ... AbstractSetter() # error: [abstract-class-instantiation] class ConcreteSetter(AbstractSetter): @AbstractSetter.value.setter def value(self, value: int) -> None: ... ConcreteSetter() # ok class AbstractDeleter(ABC): @property def value(self) -> int: return 0 @value.deleter @abstractmethod def value(self) -> None: ... AbstractDeleter() # error: [abstract-class-instantiation] class ConcreteDeleter(AbstractDeleter): @AbstractDeleter.value.deleter def value(self) -> None: ... ConcreteDeleter() # ok ``` ## Implicit override of an abstract hash Defining `__eq__` implicitly sets `__hash__ = None`, which overrides the abstract method on `collections.abc.Hashable`. ```py from collections.abc import Hashable class Concrete(Hashable): __eq__ = object.__eq__ Concrete() # ok ``` ## Slots overriding abstract properties A slot creates a descriptor in the class namespace, which overrides an inherited abstract property. The resulting concrete class can be marked `final`. ```py from abc import ABC, abstractmethod from typing import final class Abstract(ABC): @property @abstractmethod def value(self) -> int: ... @value.setter @abstractmethod def value(self, value: int) -> None: ... # ok: the abstract property is implemented by the slot @final class Concrete(Abstract): __slots__ = ("value",) Concrete() # ok ``` ## Slots and abstract properties in the MRO A slot inherited from a mixin overrides an abstract property when the mixin comes first in the MRO. ```py from abc import ABC, abstractmethod class Abstract(ABC): @property @abstractmethod def value(self) -> int: ... class SlotMixin: __slots__ = ("value",) class Concrete(SlotMixin, Abstract): ... Concrete() # ok ``` With the bases reversed, the class remains abstract. The getter-only property also removes writes permitted by the mixin's slot, so the class definition receives an override error. ```py class StillAbstract(Abstract, SlotMixin): ... # error: [incompatible-property-override] StillAbstract() # error: [abstract-class-instantiation] ``` Further subclasses do not change that ordering, even though they inherit the mixin's slot. ```py class StillAbstractChild(StillAbstract): __slots__ = () StillAbstractChild() # error: [abstract-class-instantiation] ``` ## Dataclass slots overriding abstract properties Slots generated by `dataclass(slots=True)` also override inherited abstract properties. ```py from abc import ABC, abstractmethod from dataclasses import dataclass class Abstract(ABC): @property @abstractmethod def value(self) -> int: ... @dataclass(slots=True) class Concrete(Abstract): value: int Concrete(1) # ok ``` ## Methods in stub files Protocol methods in stub files are abstract only when explicitly decorated with `abstractmethod`. An empty body can also describe a concrete default implementation whose body is omitted from the stub. `interface.pyi`: ```pyi from abc import abstractmethod from typing import Protocol class Interface(Protocol): def default(self) -> None: ... @abstractmethod def required(self) -> int: ... ``` `main.py`: ```py from interface import Interface class StillAbstract(Interface): ... StillAbstract() # error: [abstract-class-instantiation] "unimplemented abstract method `required`" class Concrete(Interface): def required(self) -> int: return 42 Concrete() # ok ``` ## Abstract overloads An abstract overload keeps a method abstract even when its implementation is not decorated with `abstractmethod`. The implementation can provide a body for subclasses to call with `super()`. ```py from abc import ABC, abstractmethod from typing import overload class Abstract(ABC): @overload @abstractmethod def method(self, value: int) -> int: ... @overload @abstractmethod def method(self, value: str) -> str: ... def method(self, value: int | str) -> int | str: return value Abstract() # error: [abstract-class-instantiation] ``` ## Aliased abstract methods Assigning an abstract method to another class preserves its abstractness, even when that class does not inherit from the class that defined the method. ```py from abc import abstractmethod class Abstract: @abstractmethod def method(self) -> None: ... class Alias: method = Abstract.method Alias() # error: [abstract-class-instantiation] ``` A decorator can also return an abstract method. Here the decorator replaces `Decorated.method` with `Abstract.method`, so `Decorated` is abstract. ```py from abc import ABC from collections.abc import Callable from typing import TypeVar T = TypeVar("T") def replace_with(value: T) -> Callable[[object], T]: return lambda _: value class Decorated(ABC): @replace_with(Abstract.method) def method(self) -> None: pass Decorated() # error: [abstract-class-instantiation] ``` Python applies the inner decorator first. It returns a plain object, which the outer decorator replaces with `Abstract.method`. ```py class Stacked(ABC): @replace_with(Abstract.method) @replace_with(object()) def method(self) -> None: pass Stacked() # error: [abstract-class-instantiation] ``` A decorator can also get an abstract method by calling the function it decorates. Here ty infers `T` from the return annotation of `make_method`. The decorator replaces `make_method` with `Abstract.method`, so `Factory` is abstract. ```py from ty_extensions._internal import TypeOf def evaluate(f: Callable[[object], T]) -> T: return f(object()) class Factory: @evaluate def make_method(self: object) -> TypeOf[Abstract.method]: return Abstract.method Factory() # error: [abstract-class-instantiation] ``` ## Narrowing a method in the class body The identity decorator leaves `method` unchanged. The class body then marks it abstract, and the type guard narrows it to the type of the abstract `Interface.method`. ```py from abc import ABC, abstractmethod from typing import TypeGuard, TypeVar F = TypeVar("F") def identity(f: F) -> F: return f def same(f: object, other: F) -> TypeGuard[F]: return f is other class Interface(ABC): @abstractmethod def method(self: object) -> None: ... class Example(ABC): @identity def method(self: object) -> None: pass Interface.method = abstractmethod(method) assert same(method, Interface.method) Example() # error: [abstract-class-instantiation] ```