Skip to main content

How to Return the Same Class from a Method with typing.Self

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Some methods give back the object they were called on, so calls can be chained: counter.add(2).add(3). The type hint for that return value is typing.Self, which means "this class", whatever class the object really is. This guide writes such a method and runs it on Python 3.15.2.

Syntax​

Syntax
from typing import Self


class <ClassName>:
def <method>(self, <parameters>) -> Self:
...
return self
PartWhat it means
from typing import SelfMakes the name Self available
-> SelfThe method returns an object of the class it's called on
return selfGives back the same object, so the caller can keep calling methods on it

The typing reference defines it as a "Special type to represent the current enclosed class."

The Example​

A Counter class starts at zero and has an add method that raises the value and returns the counter itself. The script chains two calls and prints the result and its type.

Prerequisites​

  • uv and Python 3.11 or newer. Self was added in Python 3.11. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

counter.py
from typing import Self


class Counter:
def __init__(self) -> None:
self.value = 0

def add(self, amount: int) -> Self:
self.value += amount
return self


total = Counter().add(2).add(3)
print(total.value)
print(type(total).__name__)

The counter.py script in a macOS-style code window: a Counter class whose add method is annotated -> Self and returns self, then Counter().add(2).add(3) and two prints.

Step 2: Run It​

Terminal
uv run python counter.py
Output
5
Counter

The terminal after uv run python counter.py: 5, then Counter.

Counter().add(2) returned the same counter, so .add(3) ran on it too, and the value reached 5. The command works unchanged in PowerShell.

Why Self and Not the Class Name​

Self follows subclasses. With an empty subclass, class LoudCounter(Counter): pass, the call LoudCounter().add(1) returns a LoudCounter; printing type(LoudCounter().add(1)).__name__ gives:

Output
LoudCounter

A hint written as -> "Counter" would tell your editor that the result is a plain Counter, and you'd lose the subclass's own methods in its suggestions.

Common Mistakes​

MistakeWhat happensDo this instead
Using Self without importing itThe script still runs. The error comes only when something reads the annotations: typing.get_type_hints(Counter.add) raised NameError: name 'Self' is not definedAdd from typing import Self
Forgetting return selfThe method returns None, so the next chained call fails: AttributeError: 'NoneType' object has no attribute 'add'End the method with return self

Troubleshooting​

SymptomLikely causeFix
NameError: name 'Self' is not defined from a library such as pydantic, long after the class was definedThe import is missing, and Python evaluated the annotation only when the library read itImport Self from typing

FAQ​

Why did the script run even though Self wasn't imported?​

Because annotations aren't evaluated when the function is defined. The Python language reference says: "Changed in version 3.14: Annotations are now lazily evaluated by default." The missing name only fails when a tool asks for the annotation.

Can I use Self on Python 3.10?​

Not from typing. The typing reference marks Self as "Added in version 3.11."

Conclusion​

-> Self told Python and your editor that add returns the object it was called on. Chaining .add(2).add(3) reached 5, and a subclass got its own type back. Import Self from typing, or the mistake only shows up later, when something reads the annotations.

Additional Resources​