← All notes

Annotate a method that returns its class with typing.Self

Answer: Self is a special type to represent the current enclosed class. PEP 673 introduces a concise way to annotate methods that return an instance of their class, compared with the TypeVar-based approach specified in PEP 484.

Return annotations

A common use case is a method that returns self. Annotate that return type as Self. In a subclass, the same method is understood to return the subclass, not only the base class. Foo().return_self() is revealed as Foo. SubclassOfFoo().return_self() is revealed as SubclassOfFoo.

When to prefer Self

Use Self instead of writing a new TypeVar bound to the class for that pattern.

| Self | TypeVar | |---|---| | Self | Special type for the current enclosed class | | return self | Common method annotation | | subclass | The return type follows the subclass | | PEP 673 | More concise than the PEP 484 TypeVar approach |

See also

Claims

Sources

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python PEPs, PEP 673 – Self Type; Python docs, typing — Support for type hints. Public-Domain; PSF-2.0. Text was condensed and changed from the originals.

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Python PEPs, PEP 673 – Self Type; Python docs, typing — Support for type hints. Public-Domain; PSF-2.0.

Cite this

Permanent ID: iil:note:9bmwwztd

iinventedlight. 2026. "Annotate a method that returns its class with typing.Self." iinventedlight Library, iil:note:9bmwwztd, version of 2026-11-19. https://iinventedlight.com/library/notes/python-typing-self/. License: CC-BY-4.0.

@misc{iil_note_9bmwwztd,
  author = {{iinventedlight}},
  title = {{Annotate a method that returns its class with typing.Self}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-19},
  url = {https://iinventedlight.com/library/notes/python-typing-self/},
  note = {Permanent ID iil:note:9bmwwztd. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).

Extra files beside this note

These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.

All artifact files (JSON index)

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-typing-self.md.