Advertisement
mwchase

Somewhat redacted fake hkt code

Nov 22nd, 2023
1,491
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.24 KB | None | 0 0
  1. """Map from Type1[T] to Type2[T], where T is specific to the key."""
  2.  
  3. from __future__ import annotations
  4.  
  5. import typing
  6.  
  7. import pyrsistent
  8.  
  9. _T = typing.TypeVar("_T")
  10. _K = typing.TypeVar("_K")
  11. _V = typing.TypeVar("_V")
  12.  
  13. _TCallable = typing.TypeVar("_TCallable", bound=typing.Callable[..., object])
  14. _TCallable_co = typing.TypeVar(
  15.     "_TCallable_co", bound=typing.Callable[..., object], covariant=True
  16. )
  17.  
  18.  
  19. class Magma(typing.Protocol[_TCallable_co]):
  20.     """Helper protocol for combining two values in a DependentMapping."""
  21.  
  22.     def __call__(
  23.         self: Magma[typing.Callable[..., _V]], __lhs: _V, __rhs: _V
  24.     ) -> _V:
  25.         """Combine stored values."""
  26.  
  27.  
  28. class DependentMapping(typing.Protocol[_TCallable]):
  29.     """Perform type manipulations so advanced, Mypy doesn't allow them."""
  30.  
  31.     # Various other methods redacted for space
  32.  
  33.     def __iter__(
  34.         self: DependentMapping[typing.Callable[[_K], _V]]
  35.     ) -> typing.Iterator[_K]:
  36.         """Iterate over the keys in the mapping."""
  37.  
  38.     def items(
  39.         self: DependentMapping[typing.Callable[[_K], _V]]
  40.     ) -> typing.ItemsView[_K, _V]:
  41.         """Return an iterable of the key-value pairs stored in the mapping."""
  42.  
  43.     def iteritems(
  44.         self: DependentMapping[typing.Callable[[_K], _V]]
  45.     ) -> typing.Iterable[tuple[_K, _V]]:
  46.         """Return an iterable of the key-value pairs stored in the mapping."""
  47.  
  48.     def iterkeys(
  49.         self: DependentMapping[typing.Callable[[_K], _V]]
  50.     ) -> typing.Iterable[_K]:
  51.         """Return an iterable of the keys stored in the mapping."""
  52.  
  53.     def itervalues(
  54.         self: DependentMapping[typing.Callable[[_K], _V]]
  55.     ) -> typing.Iterable[_V]:
  56.         """Return an iterable of the values stored in the mapping."""
  57.  
  58.     def keys(
  59.         self: DependentMapping[typing.Callable[[_K], _V]]
  60.     ) -> typing.KeysView[_K]:
  61.         """Return an iterable of the keys stored in the mapping."""
  62.  
  63.     # Methods redacted for space
  64.  
  65.     def values(
  66.         self: DependentMapping[typing.Callable[[_K], _V]]
  67.     ) -> typing.ValuesView[_V]:
  68.         """Return an iterable of the values stored in the mapping."""
  69.  
  70.  
  71. _AnyMap = DependentMapping[typing.Any]
  72. MAPPING: _AnyMap = typing.cast("_AnyMap", pyrsistent.pmap())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement