-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathoptional.py
More file actions
36 lines (24 loc) · 818 Bytes
/
optional.py
File metadata and controls
36 lines (24 loc) · 818 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from typing import TypeAlias, TypeVar, overload
from optional.nothing import Nothing
from optional.something import Something
T = TypeVar("T")
Option: TypeAlias = Something[T] | Nothing
class Optional:
"""Entrypoint of optional.py
Provides static methods to help intelligently create
both :class:`Something` and :class:`Nothing` values"""
@staticmethod
@overload
def of() -> Nothing: ...
@staticmethod
@overload
def of(thing: None) -> Nothing: ... # pyright: ignore[reportOverlappingOverload]
@staticmethod
@overload
def of(thing: T) -> Something[T]: ...
@staticmethod
def of(thing: T | None = None) -> Option[T]:
return Nothing() if thing is None else Something(thing)
@staticmethod
def empty() -> Nothing:
return Nothing()