/
types.py
1767 lines (1392 loc) · 47.4 KB
/
types.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# ======================================================================================
# Copyright and other protections apply. Please see the accompanying LICENSE file for
# rights and restrictions governing use of this software. All rights not expressly
# waived or licensed are reserved. If that file is missing or appears to be modified
# from its original, then please contact the author before viewing or using this
# software in any capacity.
# ======================================================================================
from __future__ import annotations
import logging
import math
import sys
import traceback
from abc import abstractmethod, abstractproperty
from decimal import Decimal
from fractions import Fraction
from typing import TYPE_CHECKING, Any, Generic, Optional
from typing import Protocol as _Protocol
from typing import Tuple, Type, TypeVar, Union, overload, runtime_checkable
from beartype.typing import SupportsAbs as _SupportsAbs
from beartype.typing import SupportsComplex as _SupportsComplex
from beartype.typing import SupportsFloat as _SupportsFloat
from beartype.typing import SupportsIndex as _SupportsIndex
from beartype.typing import SupportsInt as _SupportsInt
from beartype.typing import SupportsRound as _SupportsRound
from .bt import beartype
from .protocol import CachingProtocolMeta
if TYPE_CHECKING:
from typing import Protocol
else:
from beartype.typing import Protocol
__all__ = (
"IntegralLike",
"RealLike",
)
r"""
<!-- BEGIN MONKEY PATCH --
>>> from typing import Any
>>> _: Any
-- END MONKEY PATCH -->
"""
# ---- Types ---------------------------------------------------------------------------
_T_co = TypeVar("_T_co", covariant=True)
def _assert_isinstance(*num_ts: type, target_t: type) -> None:
assert issubclass(
target_t.__class__, CachingProtocolMeta
), f"{target_t.__class__} is not subclass of {CachingProtocolMeta}"
for num_t in num_ts:
assert isinstance(num_t(0), target_t), f"{num_t!r}, {target_t!r}"
@runtime_checkable
class SupportsAbs(
_SupportsAbs[_T_co],
Protocol,
Generic[_T_co],
metaclass=CachingProtocolMeta,
):
r"""
A derivative of ``beartype.typing.SupportsAbs`` with an override-able cache.
"""
_assert_isinstance(int, float, bool, Decimal, Fraction, target_t=SupportsAbs)
@runtime_checkable
class SupportsComplex(
_SupportsComplex,
Protocol,
metaclass=CachingProtocolMeta,
):
r"""
A derivative of ``beartype.typing.SupportsComplex`` with an override-able cache.
"""
_assert_isinstance(Decimal, Fraction, target_t=SupportsComplex)
@runtime_checkable
class SupportsFloat(
_SupportsFloat,
Protocol,
metaclass=CachingProtocolMeta,
):
r"""
A derivative of ``beartype.typing.SupportsFloat`` with an override-able cache.
"""
_assert_isinstance(int, float, bool, Decimal, Fraction, target_t=SupportsFloat)
@runtime_checkable
class SupportsInt(
_SupportsInt,
Protocol,
metaclass=CachingProtocolMeta,
):
r"""
A derivative of ``beartype.typing.SupportsInt`` with an override-able cache.
"""
_assert_isinstance(int, float, bool, target_t=SupportsInt)
@runtime_checkable
class SupportsIndex(
_SupportsIndex,
Protocol,
metaclass=CachingProtocolMeta,
):
r"""
A derivative of ``beartype.typing.SupportsIndex`` with an override-able cache.
"""
_assert_isinstance(int, bool, target_t=SupportsIndex)
@runtime_checkable
class SupportsRound(
_SupportsRound[_T_co],
Protocol,
Generic[_T_co],
metaclass=CachingProtocolMeta,
):
r"""
A derivative of ``beartype.typing.SupportsRound`` with an override-able cache.
"""
_assert_isinstance(int, float, bool, Decimal, Fraction, target_t=SupportsRound)
@runtime_checkable
class _SupportsConjugate(_Protocol):
r"""
The non-caching version of
[``SupportsConjugate``][numerary.types.SupportsConjugate].
"""
@abstractmethod
def conjugate(self) -> Any:
pass
@runtime_checkable
class SupportsConjugate(
_SupportsConjugate,
Protocol,
metaclass=CachingProtocolMeta,
):
r"""
A caching ABC defining the [``conjugate``
method](https://docs.python.org/3/library/numbers.html#numbers.Complex.conjugate).
([``_SupportsConjugate``][numerary.types._SupportsConjugate] is the raw, non-caching
version that defines the actual methods.)
``` python
>>> from typing import TypeVar
>>> from numerary.types import SupportsConjugate
>>> MyConjugateT = TypeVar("MyConjugateT", bound=SupportsConjugate)
>>> def conjugate_my_thing(arg: MyConjugateT) -> MyConjugateT:
... assert isinstance(arg, SupportsConjugate)
... return arg.conjugate()
>>> conjugate_my_thing(3)
3
>>> from decimal import Decimal
>>> conjugate_my_thing(Decimal(2.5))
Decimal('2.5')
>>> import sympy
>>> conjugate_my_thing(sympy.Float(3.5))
3.5
>>> type(_)
<class 'sympy.core.numbers.Float'>
>>> # error: Value of type variable "MyConjugateT" of "conjugate_my_thing" cannot be "str"
>>> conjugate_my_thing("not-a-number") # type: ignore [type-var]
Traceback (most recent call last):
...
AssertionError
```
"""
_assert_isinstance(
int, float, bool, complex, Decimal, Fraction, target_t=SupportsConjugate
)
@runtime_checkable
class _SupportsRealImag(_Protocol):
r"""
The non-caching version of [``SupportsRealImag``][numerary.types.SupportsRealImag].
"""
@abstractproperty
def real(self) -> Any:
pass
@abstractproperty
def imag(self) -> Any:
pass
@runtime_checkable
class SupportsRealImag(
_SupportsRealImag,
Protocol,
metaclass=CachingProtocolMeta,
):
r"""
A caching ABC defining the
[``real``](https://docs.python.org/3/library/numbers.html#numbers.Complex.real) and
[``imag``](https://docs.python.org/3/library/numbers.html#numbers.Complex.imag)
properties.
([``_SupportsRealImag``][numerary.types._SupportsRealImag] is
the raw, non-caching version that defines the actual methods.)
``` python
>>> from typing import Any, Tuple, TypeVar
>>> from numerary.types import SupportsRealImag, real, imag
>>> MyRealImagT = TypeVar("MyRealImagT", bound=SupportsRealImag)
>>> def real_imag_my_thing(arg: MyRealImagT) -> Tuple[Any, Any]:
... assert isinstance(arg, SupportsRealImag)
... return (real(arg), imag(arg))
>>> real_imag_my_thing(3)
(3, 0)
>>> from decimal import Decimal
>>> real_imag_my_thing(Decimal(2.5))
(Decimal('2.5'), Decimal('0'))
>>> # error: Value of type variable "MyRealImagT" of "real_imag_my_thing" cannot be "str"
>>> real_imag_my_thing("not-a-number") # type: ignore [type-var]
Traceback (most recent call last):
...
AssertionError
```
"""
_assert_isinstance(int, float, bool, Decimal, Fraction, target_t=SupportsRealImag)
@runtime_checkable
class _SupportsRealImagAsMethod(_Protocol):
r"""
The non-caching version of
[``SupportsRealImagAsMethod``][numerary.types.SupportsRealImagAsMethod].
"""
@abstractmethod
def as_real_imag(self) -> Tuple[Any, Any]:
pass
@runtime_checkable
class SupportsRealImagAsMethod(
_SupportsRealImagAsMethod,
Protocol,
metaclass=CachingProtocolMeta,
):
r"""
A caching ABC defining the ``#!python as_real_imag`` method that returns a 2-tuple.
([``_SupportsRealImagAsMethod``][numerary.types._SupportsRealImagAsMethod]
is the raw, non-caching version that defines the actual methods.)
See also the [``real``][numerary.types.real] and [``imag``][numerary.types.imag]
helper functions.
``` python
>>> from typing import Any, Tuple, TypeVar
>>> from numerary.types import SupportsRealImagAsMethod, real, imag
>>> MyRealImagAsMethodT = TypeVar("MyRealImagAsMethodT", bound=SupportsRealImagAsMethod)
>>> def as_real_imag_my_thing(arg: MyRealImagAsMethodT) -> Tuple[Any, Any]:
... assert isinstance(arg, SupportsRealImagAsMethod)
... return (real(arg), imag(arg))
>>> as_real_imag_my_thing(sympy.Float(3.5))
(3.5, 0)
>>> tuple(type(i) for i in _)
(<class 'sympy.core.numbers.Float'>, <class 'sympy.core.numbers.Zero'>)
>>> # error: Value of type variable "MyRealImagAsMethodT" of "as_real_imag_my_thing" cannot be "str"
>>> as_real_imag_my_thing("not-a-number") # type: ignore [type-var]
Traceback (most recent call last):
...
AssertionError
```
"""
# See <https://github.com/mkdocstrings/mkdocstrings/issues/333>
SupportsRealImagMixedU = Union[
SupportsRealImag,
SupportsRealImagAsMethod,
]
rf"""
{SupportsRealImagMixedU!r}
"""
SupportsRealImagMixedT = (
SupportsRealImag,
SupportsRealImagAsMethod,
)
rf"""
{SupportsRealImagMixedT!r}
"""
assert SupportsRealImagMixedU.__args__ == SupportsRealImagMixedT # type: ignore [attr-defined]
@runtime_checkable
class _SupportsTrunc(_Protocol):
r"""
The non-caching version of [``SupportsTrunc``][numerary.types.SupportsTrunc].
"""
@abstractmethod
def __trunc__(self) -> int:
pass
@runtime_checkable
class SupportsTrunc(
_SupportsTrunc,
Protocol,
metaclass=CachingProtocolMeta,
):
r"""
A caching ABC defining the [``__trunc__``
method](https://docs.python.org/3/reference/datamodel.html#object.__trunc__).
See also the [``__trunc__`` helper function][numerary.types.__trunc__].
([``_SupportsTrunc``][numerary.types._SupportsTrunc] is the raw, non-caching version
that defines the actual methods.)
``` python
>>> from typing import Any, Tuple, TypeVar
>>> from numerary.types import SupportsTrunc, __trunc__
>>> MyTruncT = TypeVar("MyTruncT", bound=SupportsTrunc)
>>> def trunc_my_thing(arg: MyTruncT) -> Tuple[Any, Any]:
... assert isinstance(arg, SupportsTrunc)
... return __trunc__(arg)
>>> trunc_my_thing(3)
3
>>> from decimal import Decimal
>>> trunc_my_thing(Decimal(2.5))
2
>>> import sympy
>>> trunc_my_thing(sympy.Float(3.5))
3
>>> type(_)
<class 'sympy.core.numbers.Integer'>
>>> # error: Value of type variable "MyTruncT" of "trunc_my_thing" cannot be "str"
>>> trunc_my_thing("not-a-number") # type: ignore [type-var]
Traceback (most recent call last):
...
AssertionError
```
"""
_assert_isinstance(int, bool, float, Decimal, Fraction, target_t=SupportsTrunc)
@runtime_checkable
class _SupportsFloorCeil(_Protocol):
r"""
The non-caching version of
[``SupportsFloorCeil``][numerary.types.SupportsFloorCeil].
"""
@abstractmethod
def __floor__(self) -> int:
pass
@abstractmethod
def __ceil__(self) -> int:
pass
@runtime_checkable
class SupportsFloorCeil(
_SupportsFloorCeil,
Protocol,
metaclass=CachingProtocolMeta,
):
r"""
A caching ABC defining the
[``__floor__``](https://docs.python.org/3/reference/datamodel.html#object.__floor__).
and
[``__ceil__``](https://docs.python.org/3/reference/datamodel.html#object.__ceil__)
methods.
([``_SupportsFloorCeil``][numerary.types._SupportsFloorCeil] is the raw, non-caching
version that defines the actual methods.)
!!! note
This is of limited value for Python versions prior to 3.9, since ``#!python
float.__floor__`` and ``#!python float.__ceil__`` were not defined. If support
for those environments is important, consider using
[``SupportsFloat``][numerary.types.SupportsFloat] instead.
See also the [``__floor__``][numerary.types.__floor__] and
[``__ceil__``][numerary.types.__ceil__] helper functions.
``` python
>>> from typing import Any, Tuple, TypeVar
>>> from numerary.types import SupportsFloorCeil, __ceil__, __floor__
>>> MyFloorCeilT = TypeVar("MyFloorCeilT", bound=SupportsFloorCeil)
>>> def floor_ceil_my_thing(arg: MyFloorCeilT) -> Tuple[int, int]:
... assert isinstance(arg, SupportsFloorCeil)
... return __floor__(arg), __ceil__(arg)
>>> floor_ceil_my_thing(3)
(3, 3)
>>> from decimal import Decimal
>>> floor_ceil_my_thing(Decimal(2.5))
(2, 3)
>>> import sympy
>>> floor_ceil_my_thing(sympy.Float(3.5))
(3, 4)
>>> tuple(type(i) for i in _)
(<class 'sympy.core.numbers.Integer'>, <class 'sympy.core.numbers.Integer'>)
>>> # error: Value of type variable "MyFloorCeilT" of "floor_ceil_my_thing" cannot be "str"
>>> floor_ceil_my_thing("not-a-number") # type: ignore [type-var]
Traceback (most recent call last):
...
AssertionError
```
"""
# Prior to Python 3.9, floats didn't have an explicit __floor__ method; it was
# "directly" supported in math.floor, so the pure protocol approach thinks they're not
# supported
if sys.version_info < (3, 9):
SupportsFloorCeil.includes(float)
_assert_isinstance(int, float, bool, Decimal, Fraction, target_t=SupportsFloorCeil)
@runtime_checkable
class _SupportsDivmod(
_Protocol,
Generic[_T_co],
):
r"""
The non-caching version of [``SupportsDivmod``][numerary.types.SupportsDivmod].
"""
@abstractmethod
def __divmod__(self, other: Any) -> Tuple[_T_co, _T_co]:
pass
@abstractmethod
def __rdivmod__(self, other: Any) -> Tuple[_T_co, _T_co]:
pass
@runtime_checkable
class SupportsDivmod(
_SupportsDivmod[_T_co],
Protocol,
Generic[_T_co],
metaclass=CachingProtocolMeta,
):
r"""
A caching ABC defining the
[``__divmod__``](https://docs.python.org/3/reference/datamodel.html#object.__divmod__)
and
[``__rdivmod__``](https://docs.python.org/3/reference/datamodel.html#object.__rdivmod__)
methods. Each returns a 2-tuple of covariants.
([``_SupportsDivmod``][numerary.types._SupportsDivmod] is the raw, non-caching
version that defines the actual methods.)
``` python
>>> from typing import Any, Tuple, TypeVar
>>> from numerary.types import SupportsDivmod
>>> MyDivmodT = TypeVar("MyDivmodT", bound=SupportsDivmod)
>>> def divmod_my_thing(arg: MyDivmodT, other: Any) -> Tuple[MyDivmodT, MyDivmodT]:
... assert isinstance(arg, SupportsDivmod)
... return divmod(arg, other)
>>> divmod_my_thing(2, 1)
(2, 0)
>>> from decimal import Decimal
>>> divmod_my_thing(Decimal(2.5), Decimal(1.5))
(Decimal('1'), Decimal('1.0'))
>>> import sympy
>>> divmod_my_thing(sympy.Float(3.5), sympy.Float(1.5))
(2, 0.5)
>>> tuple(type(i) for i in _)
(<class 'sympy.core.numbers.Integer'>, <class 'sympy.core.numbers.Float'>)
>>> # error: Value of type variable "MyDivmodT" of "divmod_my_thing" cannot be "str"
>>> divmod_my_thing("not-a-number", "still-not-a-number") # type: ignore [type-var]
Traceback (most recent call last):
...
AssertionError
```
"""
# complex defines these methods, but only to raise exceptions
SupportsDivmod.excludes(complex)
_assert_isinstance(int, bool, float, Decimal, Fraction, target_t=SupportsDivmod)
@runtime_checkable
class _SupportsNumeratorDenominator(_Protocol):
r"""
The non-caching version of
[``SupportsNumeratorDenominator``][numerary.types.SupportsNumeratorDenominator].
"""
@abstractproperty
def numerator(self) -> int:
pass
@abstractproperty
def denominator(self) -> int:
pass
@runtime_checkable
class SupportsNumeratorDenominator(
_SupportsNumeratorDenominator,
Protocol,
metaclass=CachingProtocolMeta,
):
r"""
A caching ABC defining the
[``numerator``](https://docs.python.org/3/library/numbers.html#numbers.Rational.numerator)
and
[``denominator``](https://docs.python.org/3/library/numbers.html#numbers.Rational.denominator)
properties.
([``_SupportsNumeratorDenominator``][numerary.types._SupportsNumeratorDenominator]
is the raw, non-caching version that defines the actual properties.)
``` python
>>> from typing import Any, Tuple, TypeVar
>>> from numerary.types import SupportsNumeratorDenominator, denominator, numerator
>>> MyNumDenomT = TypeVar("MyNumDenomT", bound=SupportsNumeratorDenominator)
>>> def num_denom_my_thing(arg: MyNumDenomT) -> Tuple[int, int]:
... assert isinstance(arg, SupportsNumeratorDenominator)
... return numerator(arg), denominator(arg)
>>> num_denom_my_thing(3)
(3, 1)
>>> from fractions import Fraction
>>> num_denom_my_thing(Fraction(2, 3))
(2, 3)
>>> import sympy
>>> num_denom_my_thing(sympy.Rational(3, 4))
(3, 4)
>>> tuple(type(i) for i in _)
(<class 'int'>, <class 'int'>)
>>> # error: Value of type variable "MyNumDenomT" of "num_denom_my_thing" cannot be "str"
>>> num_denom_my_thing("not-a-number") # type: ignore [type-var]
Traceback (most recent call last):
...
AssertionError
```
"""
_assert_isinstance(int, bool, Fraction, target_t=SupportsNumeratorDenominator)
@runtime_checkable
class _SupportsNumeratorDenominatorMethods(_Protocol):
r"""
The non-caching version of
[``SupportsNumeratorDenominatorMethods``][numerary.types.SupportsNumeratorDenominatorMethods].
"""
@abstractmethod
def numerator(self) -> SupportsInt:
pass
@abstractmethod
def denominator(self) -> SupportsInt:
pass
@runtime_checkable
class SupportsNumeratorDenominatorMethods(
_SupportsNumeratorDenominatorMethods,
Protocol,
metaclass=CachingProtocolMeta,
):
r"""
A caching ABC defining ``#!python numerator`` and ``#!python denominator`` methods.
Each returns a [``SupportsInt``][numerary.types.SupportsInt].
([``_SupportsNumeratorDenominatorMethods``][numerary.types._SupportsNumeratorDenominatorMethods]
is the raw, non-caching version that defines the actual methods.)
See also the [``numerator``][numerary.types.numerator] and
[``denominator``][numerary.types.denominator] helper functions.
"""
# See <https://github.com/mkdocstrings/mkdocstrings/issues/333>
SupportsNumeratorDenominatorMixedU = Union[
SupportsNumeratorDenominator,
SupportsNumeratorDenominatorMethods,
]
rf"""
{SupportsNumeratorDenominatorMixedU!r}
"""
SupportsNumeratorDenominatorMixedT = (
SupportsNumeratorDenominator,
SupportsNumeratorDenominatorMethods,
)
rf"""
{SupportsNumeratorDenominatorMixedT!r}
"""
assert SupportsNumeratorDenominatorMixedU.__args__ == SupportsNumeratorDenominatorMixedT # type: ignore [attr-defined]
@runtime_checkable
class _SupportsComplexOps(
_Protocol,
Generic[_T_co],
):
r"""
The non-caching version of
[``SupportsComplexOps``][numerary.types.SupportsComplexOps].
"""
@abstractmethod
def __add__(self, other: Any) -> _T_co:
pass
@abstractmethod
def __radd__(self, other: Any) -> _T_co:
pass
@abstractmethod
def __sub__(self, other: Any) -> _T_co:
pass
@abstractmethod
def __rsub__(self, other: Any) -> _T_co:
pass
@abstractmethod
def __mul__(self, other: Any) -> _T_co:
pass
@abstractmethod
def __rmul__(self, other: Any) -> _T_co:
pass
@abstractmethod
def __truediv__(self, other: Any) -> _T_co:
pass
@abstractmethod
def __rtruediv__(self, other: Any) -> _T_co:
pass
@abstractmethod
def __neg__(self) -> _T_co:
pass
@abstractmethod
def __pos__(self) -> _T_co:
pass
@runtime_checkable
class SupportsComplexOps(
_SupportsComplexOps[_T_co],
Protocol,
Generic[_T_co],
metaclass=CachingProtocolMeta,
):
r"""
A caching ABC defining the [``Complex`` operator
methods](https://docs.python.org/3/library/numbers.html#numbers.Complex) with
covariant return values.
([``_SupportsComplexOps``][numerary.types._SupportsComplexOps] is the raw,
non-caching version that defines the actual methods.)
``` python
>>> from typing import Any, Tuple, TypeVar
>>> from numerary.types import SupportsComplexOps
>>> MyComplexOpsT = TypeVar("MyComplexOpsT", bound=SupportsComplexOps)
>>> def complex_ops_my_thing(arg: MyComplexOpsT) -> MyComplexOpsT:
... assert isinstance(arg, SupportsComplexOps)
... return arg * -2 + 5
>>> complex_ops_my_thing(3)
-1
>>> from decimal import Decimal
>>> complex_ops_my_thing(Decimal("2.5"))
Decimal('0.0')
>>> import sympy
>>> complex_ops_my_thing(sympy.Float(3.5))
-2.0
>>> type(_)
<class 'sympy.core.numbers.Float'>
>>> # error: Value of type variable "MyComplexOpsT" of "complex_ops_my_thing" cannot be "str"
>>> complex_ops_my_thing("not-a-number") # type: ignore [type-var]
Traceback (most recent call last):
...
AssertionError
```
"""
_assert_isinstance(int, float, bool, Decimal, Fraction, target_t=SupportsComplexOps)
@runtime_checkable
class _SupportsComplexPow(_Protocol):
r"""
The non-caching version of
[``SupportsComplexPow``][numerary.types.SupportsComplexPow].
"""
@abstractmethod
def __pow__(self, exponent: Any) -> Any:
pass
@abstractmethod
def __rpow__(self, exponent: Any) -> Any:
pass
@runtime_checkable
class SupportsComplexPow(
_SupportsComplexPow,
Protocol,
metaclass=CachingProtocolMeta,
):
r"""
A caching ABC defining the ``#!python Complex`` (i.e., non-modulo) versions of the
[``__pow__``](https://docs.python.org/3/reference/datamodel.html#object.__pow__) and
[``__rpow__``](https://docs.python.org/3/reference/datamodel.html#object.__rpow__).
([``_SupportsComplexPow``][numerary.types._SupportsComplexPow] is the raw,
non-caching version that defines the actual methods.)
``` python
>>> from typing import Any, Tuple, TypeVar
>>> from numerary.types import SupportsComplexPow
>>> MyComplexPowT = TypeVar("MyComplexPowT", bound=SupportsComplexPow)
>>> def complex_pow_my_thing(arg: MyComplexPowT) -> MyComplexPowT:
... assert isinstance(arg, SupportsComplexPow)
... return arg ** -2
>>> complex_pow_my_thing(3)
0.1111111111111111
>>> from decimal import Decimal
>>> complex_pow_my_thing(Decimal("2.5"))
Decimal('0.16')
>>> import sympy
>>> complex_pow_my_thing(sympy.Float(3.5))
0.0816326530612245
>>> type(_)
<class 'sympy.core.numbers.Float'>
>>> # error: Value of type variable "MyComplexPowT" of "complex_pow_my_thing" cannot be "str"
>>> complex_pow_my_thing("not-a-number") # type: ignore [type-var]
Traceback (most recent call last):
...
AssertionError
```
"""
_assert_isinstance(int, float, bool, Decimal, Fraction, target_t=SupportsComplexPow)
@runtime_checkable
class _SupportsRealOps(
_Protocol,
Generic[_T_co],
):
r"""
The non-caching version of [``SupportsRealOps``][numerary.types.SupportsRealOps].
"""
@abstractmethod
def __lt__(self, other: Any) -> bool:
pass
@abstractmethod
def __le__(self, other: Any) -> bool:
pass
@abstractmethod
def __ge__(self, other: Any) -> bool:
pass
@abstractmethod
def __gt__(self, other: Any) -> bool:
pass
@abstractmethod
def __floordiv__(self, other: Any) -> Any: # TODO(posita): should be int
pass
@abstractmethod
def __rfloordiv__(self, other: Any) -> Any: # TODO(posita): should be int
pass
@abstractmethod
def __mod__(self, other: Any) -> _T_co:
pass
@abstractmethod
def __rmod__(self, other: Any) -> _T_co:
pass
@runtime_checkable
class SupportsRealOps(
_SupportsRealOps[_T_co],
Protocol,
Generic[_T_co],
metaclass=CachingProtocolMeta,
):
r"""
A caching ABC defining the [``Real`` operator
methods](https://docs.python.org/3/library/numbers.html#numbers.Real) with covariant
return values.
([``_SupportsRealOps``][numerary.types._SupportsRealOps] is the raw, non-caching
version that defines the actual methods.)
``` python
>>> from typing import Any, Tuple, TypeVar
>>> from numerary.types import SupportsRealOps
>>> MyRealOpsT = TypeVar("MyRealOpsT", bound=SupportsRealOps)
>>> def real_ops_my_thing(arg: MyRealOpsT) -> Any:
... assert isinstance(arg, SupportsRealOps)
... return arg // -2
>>> real_ops_my_thing(3)
-2
>>> from decimal import Decimal
>>> real_ops_my_thing(Decimal("2.5"))
Decimal('-1')
>>> import sympy
>>> real_ops_my_thing(sympy.Float(3.5))
-2
>>> type(_)
<class 'sympy.core.numbers.Integer'>
>>> # error: Value of type variable "MyRealOpsT" of "real_ops_my_thing" cannot be "str"
>>> real_ops_my_thing("not-a-number") # type: ignore [type-var]
Traceback (most recent call last):
...
AssertionError
```
"""
# complex defines these methods, but only to raise exceptions
SupportsRealOps.excludes(complex)
_assert_isinstance(int, float, bool, Decimal, Fraction, target_t=SupportsRealOps)
@runtime_checkable
class _SupportsIntegralOps(
_Protocol,
Generic[_T_co],
):
r"""
The non-caching version of
[``SupportsIntegralOps``][numerary.types.SupportsIntegralOps].
"""
@abstractmethod
def __lshift__(self, other: Any) -> _T_co:
pass
@abstractmethod
def __rlshift__(self, other: Any) -> _T_co:
pass
@abstractmethod
def __rshift__(self, other: Any) -> _T_co:
pass
@abstractmethod
def __rrshift__(self, other: Any) -> _T_co:
pass
@abstractmethod
def __and__(self, other: Any) -> _T_co:
pass
@abstractmethod
def __rand__(self, other: Any) -> _T_co:
pass
@abstractmethod
def __xor__(self, other: Any) -> _T_co:
pass
@abstractmethod
def __rxor__(self, other: Any) -> _T_co:
pass
@abstractmethod
def __or__(self, other: Any) -> _T_co:
pass
@abstractmethod
def __ror__(self, other: Any) -> _T_co:
pass
@abstractmethod
def __invert__(self) -> _T_co:
pass
@runtime_checkable
class SupportsIntegralOps(
_SupportsIntegralOps[_T_co],
Protocol,
Generic[_T_co],
metaclass=CachingProtocolMeta,
):
r"""