Open
Description
We could use extra helpers like
MISSING: MISSING_TYPE = MISSING_TYPE()
DEPRECATED: MISSING_TYPE = MISSING_TYPE() # A MISSING that communicates deprecation
Maybe = Union[T, MISSING_TYPE]
ListOrTuple = Union[List[T], Tuple[T, ...]]
def is_missing(x: Any) -> TypeIs[MISSING_TYPE]:
return isinstance(x, MISSING_TYPE)
which would allow something like
# this
arg: Maybe[str] = MISSING
# instead of this
arg: str | MISSING_TYPE = MISSING
This would mostly be for convenience and readability. In particular, MISSING_TYPE
feels like a leaked implementation detail.
We could also have a maybe_missing()
helper that would look something like this:
def is_missing(x: Any) -> TypeIs[MISSING_TYPE]:
return isinstance(x, MISSING_TYPE)
@overload
def maybe_missing(x: MISSING_TYPE, default: T) -> T:
...
@overload
def maybe_missing(x: T, default: Any) -> T:
...
def maybe_missing(x: Any, default: Any) -> Any:
return default if is_missing(x) else x
Originally posted by @gadenbuie in #1822 (comment)
Metadata
Metadata
Assignees
Labels
No labels