2024-03-03 17:15:23 +00:00
|
|
|
# Copyright 2009-2024 Joshua Bronson. All rights reserved.
|
2022-01-24 04:07:52 +00:00
|
|
|
#
|
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
|
|
|
|
"""Provide typing-related objects."""
|
|
|
|
|
2024-03-03 17:15:23 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-11-07 18:06:49 +00:00
|
|
|
import typing as t
|
|
|
|
from enum import Enum
|
2022-01-24 04:07:52 +00:00
|
|
|
|
|
|
|
|
2022-11-07 18:06:49 +00:00
|
|
|
KT = t.TypeVar('KT')
|
|
|
|
VT = t.TypeVar('VT')
|
2024-03-03 17:15:23 +00:00
|
|
|
VT_co = t.TypeVar('VT_co', covariant=True)
|
|
|
|
|
|
|
|
|
|
|
|
Items = t.Iterable[t.Tuple[KT, VT]]
|
|
|
|
|
|
|
|
|
|
|
|
@t.runtime_checkable
|
|
|
|
class Maplike(t.Protocol[KT, VT_co]):
|
|
|
|
"""Like typeshed's SupportsKeysAndGetItem, but usable at runtime."""
|
|
|
|
|
|
|
|
def keys(self) -> t.Iterable[KT]: ...
|
|
|
|
|
|
|
|
def __getitem__(self, __key: KT) -> VT_co: ...
|
|
|
|
|
|
|
|
|
|
|
|
MapOrItems = t.Union[Maplike[KT, VT], Items[KT, VT]]
|
|
|
|
MappOrItems = t.Union[t.Mapping[KT, VT], Items[KT, VT]]
|
|
|
|
ItemsIter = t.Iterator[t.Tuple[KT, VT]]
|
2022-01-24 04:07:52 +00:00
|
|
|
|
|
|
|
|
2022-11-07 18:06:49 +00:00
|
|
|
class MissingT(Enum):
|
|
|
|
"""Sentinel used to represent none/missing when None itself can't be used."""
|
2022-01-24 04:07:52 +00:00
|
|
|
|
2022-11-07 18:06:49 +00:00
|
|
|
MISSING = 'MISSING'
|
2022-01-24 04:07:52 +00:00
|
|
|
|
|
|
|
|
2024-03-03 17:15:23 +00:00
|
|
|
MISSING: t.Final[t.Literal[MissingT.MISSING]] = MissingT.MISSING
|
2022-11-07 18:06:49 +00:00
|
|
|
OKT = t.Union[KT, MissingT] #: optional key type
|
|
|
|
OVT = t.Union[VT, MissingT] #: optional value type
|
2022-01-24 04:07:52 +00:00
|
|
|
|
2022-11-07 18:06:49 +00:00
|
|
|
DT = t.TypeVar('DT') #: for default arguments
|
2024-03-03 17:15:23 +00:00
|
|
|
ODT = t.Union[DT, MissingT] #: optional default arg type
|