Skip to content

Python Typing

obj.attribute vs obj["member_of_collection"]

Property vs attribute

  • property is a special kind of attribute
    • has either a __get__, __set__ or __delete__
    • spam.eggs will return the result of __get__
spam = SomeObject()
print(spam.eggs)

TypedDict

from typing import TypedDict


class SalesSummary(TypedDict):
    sales: int
    year: NotRequired[int]
    product_codes: list[str]

def get_sales_summary() -> SalesSummary:
    pass

Circular Dependencies with types

from __future__ import annotations
from typing import TYPE_CHECKING # false at runtime

if TYPE_CHECKING:
    from module_b import B

Last update: 2023-04-24