Typing and Generics

typing.Self

Represents the enclosing class in a way that preserves the concrete subtype in instance and class method annotations. Use it for fluent APIs returning self; do not use it when the method always returns a fixed base class.

Formal Syntax & Structure

python syntaxpython
typing.Self

Return Value

type expression

Typing construct or runtime result as described; static typing rules are not runtime validation.

Working Example

Example Snippetpython
from typing import Self
class Builder:    def reset(self) -> Self: return selfclass Special(Builder): passitem=Special()print(item.reset() is item)
Python Reference