Skip to main content

Typing Nuances

Self Type

Supported Use the Self type to reference a class name within the class First, from typing import Any, Self

Then, to use


from typing import Any, Self

class MyClass:

def my_func(self, item: Any) -> Self:
self.item.append(item)
return self

Cleaning Up Types

You can just declare types that ar esimplificatoins of other types


MyComplexType = Iterable[Map[str,str]]


type ListOrSet[T] = list[T] | set[T]

You can use TypeVar to clean up typing


from typing import TypeVar

TMyWeirdType = TypeVar("TMyWeirdType", bound="MyWeirdType")

@dataclass
class MyWeirdType:
first_var: int
second_var: int

def my_func(self: TMyWeirdType) -> TMyWeirdType:
print (self.first_var)
return self

Use this for empty or something


def foo(x: str | int) -> None: ...
def foo(x: str | None) -> int | None: ...

Named Tuples


from typing import NamedTuple

class MyClass(NamedTuple):
first_var: str
second_var: str

TypedDict


from typing import TypedDict

class MyClass(NamedTuple):
first_var: str
second_var: int

References