Source code for stdlibx.option.types
1from __future__ import annotations
2
3from typing import Callable, Literal, Protocol, TypeVar, Union, runtime_checkable
4
5from typing_extensions import TypeAlias
6
7T = TypeVar("T")
8U = TypeVar("U")
9
10Operation: TypeAlias = Callable[[T], U]
11
12
[docs]
13@runtime_checkable
14class Some(Protocol[T]):
15 __match_args__ = ("value",)
16
17 value: T
18
[docs]
19 def is_some(self) -> Literal[True]: ...
[docs]
20 def is_none(self) -> Literal[False]: ...
[docs]
21 def apply(self, operation: Operation[Option[T], U]) -> U: ...
22
23
[docs]
24@runtime_checkable
25class Nothing(Protocol):
[docs]
26 def is_some(self) -> Literal[False]: ...
[docs]
27 def is_none(self) -> Literal[True]: ...
[docs]
28 def apply(self, operation: Operation[Option[T], U]) -> U: ...
29
30
31Option: TypeAlias = Union[Some[T], Nothing]