Skip to content

Refactor MISSING_TYPE and MISSING #1873

Open
@gadenbuie

Description

@gadenbuie

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions