mathspp
  • Blog
    • Pydon'ts
    • Problems
    • TIL
    • Twitter threads
  • Books
  • Talks
  • Trainings
    • Advanced iteration
    • Python for scripting and automation
    • Rust for Python developers
  • Courses
  • About
Link blog

Explicit method overriding with @typing.override

by Redowan Delowar on 18-08-2025 13:33

This article is where I first learned about typing.overrides, a decorator you can use to flag all method overrides when you're subclassing.

A method that is tagged with typing.overrides is assumed to be overriding a method from a super class and if the type checker doesn't find the original method you're overriding, it'll let you know there's an issue with your code (maybe a typo? maybe the method changed name?).

As Redowan shows, overrides also works with properties, class methods, and more. Here's an example where mypy will complain because you forgot to specify that Cat.species is a property:

from typing import override

class Animal:
    @property
    def species(self) -> str:
        return "Unknown"

class Cat(Animal):
    @override
    def species(self) -> str:
        return "Catus"

Previous link

There should be no such thing as boring mathematics.

mathspp
  • Blog
    • Pydon'ts
    • Problems
    • TIL
    • Twitter threads
  • Books
  • Talks
  • Trainings
    • Advanced iteration
    • Python for scripting and automation
    • Rust for Python developers
  • Courses
  • About