Source code for jgdv._abstract.protocols.stdlib

  1#!/usr/bin/env python3
  2"""
  3An Adaptation of typeshed's protocol's of the stdlib.
  4"""
  5# mypy: disable-error-code="explicit-override"
  6# ruff: noqa: PLW1641, ANN401
  7
  8# Imports:
  9from __future__ import annotations
 10
 11# ##-- stdlib imports
 12import datetime
 13import enum
 14import functools as ftz
 15import itertools as itz
 16import logging as logmod
 17import pathlib as pl
 18import re
 19import time
 20import types
 21import collections
 22import contextlib
 23import hashlib
 24from copy import deepcopy
 25from uuid import UUID, uuid1
 26from weakref import ref
 27import atexit # for @atexit.register
 28import faulthandler
 29# ##-- end stdlib imports
 30
 31# ##-- types
 32# isort: off
 33import abc
 34import collections.abc
 35from typing import TYPE_CHECKING, cast, assert_type, assert_never
 36from typing import Generic, NewType, Never
 37# Protocols:
 38from typing import Protocol, runtime_checkable
 39# Typing Decorators:
 40from typing import no_type_check, final, override, overload
 41
 42if TYPE_CHECKING:
 43    from jgdv import Maybe
 44    from typing import Final
 45    from typing import ClassVar, Any, LiteralString
 46    from typing import Self, Literal
 47    from typing import TypeGuard
 48    from collections.abc import Iterable, Iterator, Callable, Generator
 49    from collections.abc import Sequence, Mapping, MutableMapping, Hashable
 50
 51##--|
 52
 53# isort: on
 54# ##-- end types
 55
 56##-- logging
 57logging = logmod.getLogger(__name__)
 58##-- end logging
 59
 60# Vars:
 61
 62##--| General:
 63
[docs] 64class Hashable_p(Protocol): 65 66 def __hash__(self) -> int: ...
67
[docs] 68class Iterable_p[V](Protocol): 69 70 def __iter__(self) -> Iterator_p[V]: ...
71
[docs] 72class Iterator_p[V](Iterable_p, Protocol): 73 74 def __next__(self) -> V: ... 75 76 def __iter__(self) -> Self: ...
77
[docs] 78class Reversible_p(Iterable_p, Protocol): 79 80 def __reversed__(self) -> Self: ...
81
[docs] 82class Generator_p(Iterator_p, Protocol): 83 84 def __next__(self) -> Any: ... 85
[docs] 86 def send(self, value:Any) -> Any: ...
87
[docs] 88 def throw(self, typ:Any, val:Maybe=None, tb:Maybe=None) -> Any: ...
89
[docs] 90 def close(self) -> None: ...
91
[docs] 92class Sized_p(Protocol): 93 94 def __len__(self) -> int: ...
95
[docs] 96class Container_p[V](Protocol): 97 98 def __contains__(self, x:V) -> bool: ...
99
[docs] 100class Collection_p(Sized_p, Iterable_p, Container_p, Protocol): 101 pass
102
[docs] 103class Buffer_p(Protocol): 104 105 def __buffer__(self, flags: int) -> memoryview: ...
106
[docs] 107class Callable_p[*A, K, R](Protocol): 108 109 def __call__(self, *args:*A, **kwds:K) -> R: ...
110 111##--| Sets 112
[docs] 113class Set_p[V](Collection_p, Protocol): 114 """A set is a finite, iterable container. 115 116 This class provides concrete generic implementations of all 117 methods except for __contains__, __iter__ and __len__. 118 119 To override the comparisons (presumably for speed, as the 120 semantics are fixed), redefine __le__ and __ge__, 121 then the other operations will automatically follow suit. 122 """ 123 124 def __le__(self, other:Self) -> bool: ... 125 126 def __lt__(self, other:Self) -> bool: ... 127 128 def __gt__(self, other:Self) -> bool: ... 129 130 def __ge__(self, other:Self) -> bool: ... 131 132 def __eq__(self, other:object) -> bool: ... 133
[docs] 134 @classmethod 135 def _from_iterable(cls:type[Set_p], it:Iterable_p[V]) -> Set_p[V]: ...
136 137 def __and__(self, other:Self) -> Self: ... 138 139 def __rand__(self, other:Self) -> Self: ... 140
[docs] 141 def isdisjoint(self, other:Self) -> bool: ...
142 143 def __or__(self, other:Self) -> Self: ... 144 145 def __ror__(self, other:Self) -> Self: ... 146 147 def __sub__(self, other:Self) -> Self: ... 148 149 def __rsub__(self, other:Self) -> Self: ... 150 151 def __xor__(self, other:Self) -> Self: ... 152 153 def __rxor__(self, other:Self) -> Self: ... 154
[docs] 155 def _hash(self) -> int: ...
156
[docs] 157class MutableSet_p[V](Set_p[V], Protocol): 158 """A mutable set is a finite, iterable container. 159 160 This class provides concrete generic implementations of all 161 methods except for __contains__, __iter__, __len__, 162 add(), and discard(). 163 164 To override the comparisons (presumably for speed, as the 165 semantics are fixed), all you have to do is redefine __le__ and 166 then the other operations will automatically follow suit. 167 """ 168
[docs] 169 def add(self, value:V) -> Self: ...
170
[docs] 171 def discard(self, value:V) -> None: ...
172
[docs] 173 def remove(self, value:V) -> None: ...
174
[docs] 175 def pop(self) -> V: ...
176
[docs] 177 def clear(self) -> None: ...
178 179 def __ior__(self, it:Iterable_p) -> Self: ... 180 181 def __iand__(self, it:Iterable_p) -> Self: ... 182 183 def __ixor__(self, it:Iterable_p) -> Self: ... 184 185 def __isub__(self, it:Iterable_p) -> Self: ...
186 187##--| Mappings 188
[docs] 189class Mapping_p[K,V](Collection_p, Protocol): 190 """A Mapping_p is a generic container for associating key/value 191 pairs. 192 193 This class provides concrete generic implementations of all 194 methods except for __getitem__, __iter__, and __len__. 195 """ 196 197 def __getitem__(self, key:K) -> V: ... 198
[docs] 199 def get[D:Maybe](self, key:K, default:Maybe[Any]=None) -> V|D: ...
200 201 def __contains__(self, key:K) -> bool: ... 202
[docs] 203 def keys(self) -> KeysView_p: ...
204
[docs] 205 def items(self) -> ItemsView_p: ...
206
[docs] 207 def values(self) -> ValuesView_p: ...
208 209 def __eq__(self, other:object) -> bool: ...
210
[docs] 211class MappingView_p(Sized_p, Protocol): 212 213 def __init__(self, mapping:Mapping_p) -> None: ... 214 215 def __len__(self) -> int: ... 216 217 def __repr__(self) -> str: ...
218
[docs] 219class KeysView_p[K](MappingView_p, Set_p, Protocol): 220
[docs] 221 @classmethod 222 def _from_iterable[V](cls:type[KeysView_p], it:Iterable_p[K]) -> KeysView_p[K]: ...
223 224 def __contains__(self, key:K) -> bool: ... 225 226 def __iter__(self) -> Iterator_p: ...
227
[docs] 228class ItemsView_p[K,V](MappingView_p, Set_p, Protocol): 229
[docs] 230 @classmethod 231 def _from_iterable(cls:type[ItemsView_p], it:Iterable_p) -> ItemsView_p: ...
232 233 def __contains__(self, item:tuple[K,V]) -> bool: ... 234 235 def __iter__(self) -> Iterator_p[tuple[K,V]]: ...
236
[docs] 237class ValuesView_p[V](MappingView_p, Collection_p, Protocol): 238 239 def __contains__(self, value:V) -> bool: ... 240 241 def __iter__(self) -> Iterator_p: ...
242
[docs] 243class MutableMapping_p[K,V](Mapping_p[K,V], Protocol): 244 """A MutableMapping is a generic container for associating 245 key/value pairs. 246 247 This class provides concrete generic implementations of all 248 methods except for __getitem__, __setitem__, __delitem__, 249 __iter__, and __len__. 250 """ 251 252 def __setitem__(self, key:K, value:V) -> None: ... 253 254 def __delitem__(self, key:K) -> None: ... 255
[docs] 256 def pop[D:V|Maybe[object]](self, key:K, default:Maybe[D]=None) -> D: ...
257
[docs] 258 def popitem(self) -> V: ...
259
[docs] 260 def clear(self) -> None: ...
261
[docs] 262 def update(self, other:Iterable_p=(), /, **kwds:Any) -> None: ...
263
[docs] 264 def setdefault[D:V|Maybe](self, key:K, default:Maybe[D]=None) -> V|D: ...
265 266##--| Sequences 267
[docs] 268class Sequence_p[V](Reversible_p, Collection_p, Protocol): 269 """All the operations on a read-only sequence. 270 271 Concrete subclasses must override __new__ or __init__, 272 __getitem__, and __len__. 273 """ 274 275 # Tell ABCMeta.__new__ that this class should have TPFLAGS_SEQUENCE set. 276 277 def __getitem__(self, index:int) -> V: ... 278 279 def __iter__(self) -> Iterator_p: ... 280 281 def __contains__(self, value:V) -> bool: ... 282 283 def __reversed__(self) -> Self: ... 284
[docs] 285 def index(self, value:V, start:int=0, stop:Maybe[int]=None) -> int: ...
286
[docs] 287 def count(self, value:V) -> int: ...
288
[docs] 289class MutableSequence_p[V](Sequence_p, Protocol): 290 """All the operations on a read-write sequence. 291 292 Concrete subclasses must provide __new__ or __init__, 293 __getitem__, __setitem__, __delitem__, __len__, and insert(). 294 """ 295 296 def __setitem__(self, index:int, value:V) -> None: ... 297 298 def __delitem__(self, index:int) -> None: ... 299
[docs] 300 def insert(self, index:int, value:V) -> None: ...
301
[docs] 302 def append(self, value:V) -> None: ...
303
[docs] 304 def clear(self) -> None: ...
305
[docs] 306 def reverse(self) -> None: ...
307
[docs] 308 def extend(self, values:Iterable_p[V]) -> None: ...
309
[docs] 310 def pop(self, index:int=-1) -> V: ...
311
[docs] 312 def remove(self, value:V) -> None: ...
313 314 def __iadd__(self, values:Iterable_p[V]) -> Self: ...