Based on Python 3.12abstract method error text · Python 3.12+
Python ABC and @abstractmethod in a report template, and when to use Protocol.
In one line
The parent states what must exist (method names and shapes) and leaves how to do it to the children. In Python you use ABC and @abstractmethod from the abc module.
Why you need it
Copy the title line and line-count footer into every report type, and changing the footer means editing them all. Use a plain parent class instead, and a child that forgets the body method goes unnoticed until it runs.
Let the parent own the outline in render() and make body() abstract. The outline is fixed in one place, and a child without body fails as soon as it is created.
Everyday picture
Think of a job application form. The form fixes the order and layout of the boxes; applicants fill in only the experience box. Leave it blank and the desk hands it straight back. The form is the abstract class, the experience box the abstract method.
Example code
main.py · 45 lines
from abc importABC, abstractmethodclassReport(ABC):def__init__(self, title):self.title = titledefrender(self): lines = [f"== {self.title} =="] lines += self.body() lines.append(f"({len(lines) - 1} lines)")return"\n".join(lines)@abstractmethoddefbody(self): ...classSalesReport(Report):def__init__(self, sales):super().__init__("Sales")self.sales = salesdefbody(self): total = sum(self.sales.values())return [f"{day}: {n}"for day, n inself.sales.items()] + [f"total: {total}"]classStockReport(Report):def__init__(self, stock, low=5):super().__init__("Stock")self.stock = stockself.low = lowdefbody(self):return [f"{item} is low ({n})"for item, n inself.stock.items() if n < self.low]try:Report("Empty")exceptTypeErroras e:print("TypeError:", e)sales = SalesReport({"mon": 120, "tue": 95})print(sales.render())print(StockReport({"pen": 3, "ink": 12, "tape": 1}).render())
Press Run to try your edited code right away. The step-through and the output above follow the original code.
Three ways to require a method
ABC + @abstractmethod
raise NotImplementedError
typing.Protocol
Must subclass
O
O
X
When a missing method shows up
When the object is created
When the method is called
When a type checker (mypy) runs
Can hold shared code
O
O
X
Good fit
The parent owns the outline and order
Small scripts, quick start
Accepting classes you did not write
typing.Protocol exists since Python 3.8. A class that has the right method names and signatures counts as following the contract without inheriting.
When to use it
The steps are the same and only one or two differ per kind (reports, file readers, checkout flows)
Several people write child classes that plug into one structure
Forgetting a method would cause real damage
The wording "without an implementation for abstract method" is from Python 3.12. Up to 3.11 the message reads "with abstract method body".
Common mistakes
Inheriting ABC without @abstractmethod
Subclassing ABC on its own enforces nothing; the empty body quietly returns None. Mark required methods with @abstractmethod so creation fails until they are filled in.
Wrong
from abc importABCclassReport(ABC):defbody(self):passr = Report()print(r.body())
When you run itNone
Fixed
from abc importABC, abstractmethodclassReport(ABC):@abstractmethoddefbody(self): ...try:Report()exceptTypeError:print("make a subclass first")
When you run itmake a subclass first
Misspelling the abstract method
bodyy is a different method, so body is still empty. The error appears the moment you create the object, not halfway through a run.
The parent expects body() to return a list of lines and extends with lines +=. A plain string gets split into single characters. The return shape is part of the contract, not just the name.