-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsomething.py
More file actions
35 lines (23 loc) · 902 Bytes
/
something.py
File metadata and controls
35 lines (23 loc) · 902 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
from types import NotImplementedType
from typing import Generic, TypeVar, final
from typing_extensions import override
T = TypeVar("T")
@final
class Something(Generic[T]):
"""Represents the presence of a value.
Rarely instantiated on its own, see :func:`Optional.of`"""
__match_args__ = ("_value",)
def __init__(self, value: T) -> None:
if value is None:
raise ValueError("Invalid value for Something: None")
self._value = value
@override
def __eq__(self, other: object) -> bool | NotImplementedType:
if not isinstance(other, Something):
return NotImplemented
return self._value == other._value # pyright: ignore[reportUnknownVariableType, reportUnknownMemberType]
@override
def __repr__(self) -> str:
return f"Optional.of({self._value!r})"
def __bool__(self) -> bool:
return True