« ^ »

Pythonの型ヒント

所要時間: 約 1分

型変数の例

from dataclasses import dataclass
from functools import singledispatch
from typing import Dict, Generic, List, NewType, Optional, TypeVar


@dataclass
class FooEvent:
    pass


@dataclass
class BarEvent:
    pass


@dataclass
class BazEvent:
    pass


Event = TypeVar("Event", FooEvent, BarEvent, BazEvent)


@singledispatch
def as_custom_dict(ev: Event) -> Dict:
    return {}


@as_custom_dict.register
def _(ev: BazEvent) -> Dict:
    return {}


def func_effect(ev: Event) -> Dict:
    return as_custom_dict(ev)