How to Return the Same Class from a Method with typing.Self
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
from typing import Self
class <ClassName>:
def <method>(self, <parameters>) -> Self:
...
return self
| Part | What it means |
|---|---|
from typing import Self | Makes the name Self available |
-> Self | The method returns an object of the class it's called on |
return self | Gives 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.
Selfwas added in Python 3.11. This guide uses uv 0.13.10 with Python 3.15.2, in a project made withuv init.
Step 1: Write the Script
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__)

Step 2: Run It
uv run python counter.py
5
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:
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
| Mistake | What happens | Do this instead |
|---|---|---|
Using Self without importing it | The script still runs. The error comes only when something reads the annotations: typing.get_type_hints(Counter.add) raised NameError: name 'Self' is not defined | Add from typing import Self |
Forgetting return self | The method returns None, so the next chained call fails: AttributeError: 'NoneType' object has no attribute 'add' | End the method with return self |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
NameError: name 'Self' is not defined from a library such as pydantic, long after the class was defined | The import is missing, and Python evaluated the annotation only when the library read it | Import 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
- typing.Self — the reference, with an example.
- Annotations — how and when Python evaluates them.
