Main Ecosystems
HomePython CorePandas ReferenceNumPy ScientificFastAPI & PydanticDjango Enterprise
More Ecosystems
Environment & SetupRequests & HTTPAsyncIO ConcurrencyObject-Oriented OOPPyTorch Deep LearningScikit-Learn MLFlask FrameworkWeb ScrapingDatabase & ORMDevOps & Docker

TypeError: Cant instantiate abstract class with abstract methods in Python

Verified FixPython 3.10+Python Native ABCSilo: oop

Quick Fix / Solution Rapide

Implement every method decorated with @abstractmethod in your concrete derived class before instantiating.

Root Cause Analysis

This error occurs when Python attempts to create an instance of a class that inherits from abc.ABC (or uses metaclass=abc.ABCMeta), but the child class has not implemented all abstract methods, properties, or class methods marked with @abstractmethod in the parent interface.

1. The Abstract Base Class (ABC) Contract

Python's abc module enforces interface contracts at instantiation time. If class PaymentGateway(ABC) defines @abstractmethod def process_payment(self):, any concrete subclass (like StripeGateway) must override and implement process_payment(). Omitting the implementation causes instantiation to fail.

2. Misspelled Method Names in Child Classes

If the parent class defines @abstractmethod def connect(self): and the subclass implements def connet(self): (with a typo), Python considers the abstract method unimplemented.

3. Abstract Properties and Setters

Decorating properties with @property and @abstractmethod requires subclasses to implement both getter and setter properties if marked abstract.

4. Instantiating the Base Class Directly

Attempting to instantiate AbstractBase() directly rather than using a concrete implementation.

Reproduction Code (MCVE)

Example: Bug Reproduction
import abc

class AbstractRepository(abc.ABC):
    @abc.abstractmethod
    def save(self, data):
        pass

class SQLiteRepository(AbstractRepository):
    pass  # Forgot to implement save() method

# Instantiating incomplete child class raises TypeError
repo = SQLiteRepository()

Solution 1: Implement All Required Abstract Methods

Override and define concrete implementations for every abstract method in the subclass.

Example: Recommended Solution
import abc

class AbstractRepository(abc.ABC):
    @abc.abstractmethod
    def save(self, data):
        pass

class SQLiteRepository(AbstractRepository):
    def save(self, data):
        print(f'Data saved to SQLite: {data}')
        return True

repo = SQLiteRepository()
repo.save({'id': 1, 'name': 'Item'})

Solution 2: Use typing.Protocol for Structural Subtyping (Duck Typing)

Use typing.Protocol (PEP 544) for static interface checking without enforcing runtime ABC instantiation restrictions.

Example: Alternative Solution
from typing import Protocol

class RepositoryProtocol(Protocol):
    def save(self, data: dict) -> bool: ...

class MemoryRepo:
    def save(self, data: dict) -> bool:
        return True

repo: RepositoryProtocol = MemoryRepo()
print(f'Protocol-compliant repo save: {repo.save({})}')

A common mistake is calling super().abstract_method() expecting a default implementation. While abstract methods can contain code callable via super(), the subclass must still explicitly define the overriding method. Edge cases occur with abstract class methods: use @classmethod outside @abstractmethod: @classmethod @abstractmethod def from_config(cls):. Contrast this error with NotImplementedError, which is an exception raised manually inside method bodies rather than an instantiation blocker.