-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
utilpep484.py
892 lines (780 loc) · 37.7 KB
/
utilpep484.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
#!/usr/bin/env python3
# --------------------( LICENSE )--------------------
# Copyright (c) 2014-2021 Beartype authors.
# See "LICENSE" for further details.
'''
Project-wide :pep:`484`-compliant type hint utilities.
This private submodule is *not* intended for importation by downstream callers.
'''
# ....................{ IMPORTS }....................
import typing
from beartype.roar import (
BeartypeDecorHintForwardRefException,
BeartypeDecorHintPep484Exception,
)
from beartype._data.hint.pep.sign.datapepsigncls import HintSign
from beartype._data.hint.pep.sign.datapepsigns import (
HintSignGeneric,
HintSignNewType,
)
from beartype._data.hint.pep.sign.datapepsignset import HINT_SIGNS_UNION
from beartype._util.py.utilpyversion import (
IS_PYTHON_AT_LEAST_3_10,
IS_PYTHON_AT_LEAST_3_7,
)
from beartype._util.cache.utilcachecall import callable_cached
from beartype._util.cls.utilclstest import is_type_subclass
from types import FunctionType
from typing import Any, Generic, Optional, Tuple, TypeVar
# See the "beartype.cave" submodule for further commentary.
__all__ = ['STAR_IMPORTS_CONSIDERED_HARMFUL']
# ....................{ HINTS }....................
HINT_PEP484_TUPLE_EMPTY = Tuple[()]
'''
:pep:`484`-compliant empty fixed-length tuple type hint.
'''
# Conditionally define the "typing.ForwardRef" superclass depending on the
# current Python version. This superclass was thankfully publicized under
# Python >= 3.7 after its initial privatization under Python <= 3.6.
HINT_PEP484_FORWARDREF_TYPE: Any = (
typing.ForwardRef if IS_PYTHON_AT_LEAST_3_7 else
typing._ForwardRef # type: ignore [attr-defined]
)
'''
:pep:`484`-compliant **forward reference type** (i.e., class of all forward
reference objects implicitly created by all :mod:`typing` type hint factories
when subscripted by a string).
'''
# ....................{ TESTERS ~ ignorable }....................
def is_hint_pep484_ignorable_or_none(
hint: object, hint_sign: HintSign) -> Optional[bool]:
'''
``True`` only if the passed object is a :pep:`484`-compliant **ignorable
type hint,** ``False`` only if this object is a :pep:`484`-compliant
unignorable type hint, and ``None`` if this object is *not* `PEP
484`_-compliant.
Specifically, this tester function returns ``True`` only if this object is
a deeply ignorable :pep:`484`-compliant type hint, including:
* A parametrization of the :class:`typing.Generic` abstract base class
(ABC) by one or more type variables. As the name implies, this ABC is
generic and thus fails to impose any meaningful constraints. Since a type
variable in and of itself also fails to impose any meaningful
constraints, these parametrizations are safely ignorable in all possible
contexts: e.g.,
.. code-block:: python
from typing import Generic, TypeVar
T = TypeVar('T')
def noop(param_hint_ignorable: Generic[T]) -> T: pass
* The :func:`NewType` closure factory function passed an ignorable child
type hint. Unlike most :mod:`typing` constructs, that function does *not*
cache the objects it returns: e.g.,
.. code-block:: python
>>> from typing import NewType
>>> NewType('TotallyNotAStr', str) is NewType('TotallyNotAStr', str)
False
Since this implies every call to ``NewType({same_name}, object)`` returns
a new closure, the *only* means of ignoring ignorable new type aliases is
dynamically within this function.
* The :data:`Optional` or :data:`Union` singleton subscripted by one or
more ignorable type hints (e.g., ``typing.Union[typing.Any, bool]``).
Why? Because unions are by definition only as narrow as their widest
child hint. However, shallowly ignorable type hints are ignorable
precisely because they are the widest possible hints (e.g.,
:class:`object`, :attr:`typing.Any`), which are so wide as to constrain
nothing and convey no meaningful semantics. A union of one or more
shallowly ignorable child hints is thus the widest possible union,
which is so wide as to constrain nothing and convey no meaningful
semantics. Since there exist a countably infinite number of possible
:data:`Union` subscriptions by one or more ignorable type hints, these
subscriptions *cannot* be explicitly listed in the
:data:`HINTS_REPR_IGNORABLE_SHALLOW` frozenset. Instead, these
subscriptions are dynamically detected by this tester at runtime and thus
referred to as **deeply ignorable type hints.**
This tester is intentionally *not* memoized (e.g., by the
:func:`callable_cached` decorator), as this tester is only safely callable
by the memoized parent
:func:`beartype._util.hint.utilhinttest.is_hint_ignorable` tester.
Parameters
----------
hint : object
Type hint to be inspected.
hint_sign : HintSign
**Sign** (i.e., arbitrary object uniquely identifying this hint).
Returns
----------
Optional[bool]
Either:
* If this object is :pep:`484`-compliant:
* If this object is a ignorable, ``True``.
* Else, ``False``.
* If this object is *not* :pep:`484`-compliant, ``None``.
'''
# Avoid circular import dependencies.
from beartype._util.hint.utilhinttest import is_hint_ignorable
from beartype._util.hint.pep.utilpepget import (
get_hint_pep_args,
get_hint_pep_origin_or_none,
)
# print(f'Testing PEP 484 hint {repr(hint)} [{repr(hint_sign)}] deep ignorability...')
# If this hint is a PEP 484-compliant generic...
if hint_sign is HintSignGeneric:
# print(f'Testing generic hint {repr(hint)} deep ignorability...')
# If this generic is the "typing.Generic" superclass directly
# parametrized by one or more type variables (e.g.,
# "typing.Generic[T]"), return true.
#
# Note that we intentionally avoid calling the
# get_hint_pep_type_isinstanceable_or_none() function here, which has been
# intentionally designed to exclude PEP-compliant type hints
# originating from "typing" type origins for stability reasons.
if get_hint_pep_origin_or_none(hint) is Generic:
# print(f'Testing generic hint {repr(hint)} deep ignorability... True')
return True
# Else, this generic is *NOT* the "typing.Generic" superclass directly
# parametrized by one or more type variables and thus *NOT* an
# ignorable non-protocol.
#
# Note that this condition being false is *NOT* sufficient to declare
# this hint to be unignorable. Notably, the type origin originating
# both ignorable and unignorable protocols is "Protocol" rather than
# "Generic". Ergo, this generic could still be an ignorable protocol.
# print(f'Testing generic hint {repr(hint)} deep ignorability... False')
# Else, this hint is *NOT* a generic.
#
# If this hint is a new type, return true only if this new type aliases an
# ignorable child type hint.
elif hint_sign is HintSignNewType:
return is_hint_ignorable(get_hint_pep484_newtype_class(hint))
# Else, this hint is *NOT* a new type.
#
# If this hint is a union, return true only if one or more child hints of
# this union are recursively ignorable. See the function docstring.
elif hint_sign in HINT_SIGNS_UNION:
return any(
is_hint_ignorable(hint_child)
for hint_child in get_hint_pep_args(hint)
)
# Else, this hint is *NOT* a union.
# Return "None", as this hint is unignorable only under PEP 484.
return None
# ....................{ TESTERS ~ kind }....................
def is_hint_pep484_forwardref(hint: object) -> bool:
'''
``True`` only if the passed object is a :pep:`484`-compliant **forward
reference type hint** (i.e., instance of the :class:`typing.ForwardRef`
class implicitly replacing all string arguments subscripting :mod:`typing`
objects).
The :mod:`typing` module implicitly replaces all strings subscripting
:mod:`typing` objects (e.g., the ``MuhType`` in ``List['MuhType']``) with
:class:`typing.ForwardRef` instances containing those strings as instance
variables, for nebulous reasons that make little justifiable sense.
This tester is intentionally *not* memoized (e.g., by the
:func:`callable_cached` decorator), as the implementation trivially reduces
to an efficient one-liner.
Parameters
----------
hint : object
Object to be inspected.
Returns
----------
bool
``True`` only if this object is a :pep:`484`-compliant forward
reference type hint.
'''
# Return true only if this hint is an instance of the PEP 484-compliant
# forward reference superclass.
return isinstance(hint, HINT_PEP484_FORWARDREF_TYPE)
def is_hint_pep484_typevar(hint: object) -> bool:
'''
``True`` only if the passed object either is a PEP-compliant **type
variable** (i.e., instance of the :class:`TypeVar` class).
This tester is intentionally *not* memoized (e.g., by the
:func:`callable_cached` decorator), as the implementation trivially reduces
to an efficient one-liner.
Motivation
----------
Since type variables are not themselves types but rather placeholders
dynamically replaced with types by type checkers according to various
arcane heuristics, both type variables and types parametrized by type
variables warrant special-purpose handling.
Parameters
----------
hint : object
Object to be inspected.
Returns
----------
bool
``True`` only if this object is a type variable.
'''
# Return true only if the type of this hint is that of all type variables.
#
# Note that the "typing.TypeVar" class prohibits subclassing: e.g.,
# >>> import typing as t
# >>> class MutTypeVar(t.TypeVar): pass
# TypeError: Cannot subclass special typing classes
#
# Ergo, the object identity test performed here both suffices and is more
# efficient than the equivalent general-purpose test, which requires an
# implicit breadth- or depth-first search over the method resolution order
# (MRO) of all superclasses of this object: e.g.,
# # This is potentially *MUCH* slower. It's the little things in life.
# return isinstance(hint, TypeVar)
return hint.__class__ is TypeVar
# ....................{ TESTERS ~ kind : generic }....................
# If the active Python interpreter targets at least Python >= 3.7.0, implement
# this function in the standard way.
#
# Sadly, Python 3.7.0 broke backward compatibility with the public API of the
# "typing" module by removing the "typing.GenericMeta" metaclass previously
# referenced by this function under Python 3.6.
if IS_PYTHON_AT_LEAST_3_7:
def is_hint_pep484_generic(hint: object) -> bool:
# Avoid circular import dependencies.
from beartype._util.hint.pep.proposal.pep484585.utilpepgeneric import (
get_hint_pep484585_generic_type_or_none)
# If this hint is *NOT* a class, this hint is *NOT* an unsubscripted
# generic but could still be a subscripted generic (i.e., generic
# subscripted by one or more PEP-compliant child type hints). To
# decide, reduce this hint to the object originating this hint if any,
# enabling the subsequent test to test whether this origin object is an
# unsubscripted generic, which would then imply this hint to be a
# subscripted generic. If this strikes you as insane, you're not alone.
hint = get_hint_pep484585_generic_type_or_none(hint)
# Return true only if this hint is a subclass of the "typing.Generic"
# abstract base class (ABC), in which case this hint is a user-defined
# generic.
#
# Note that this test is robust against edge case, as the "typing"
# module guarantees all user-defined classes subclassing one or more
# "typing" pseudo-superclasses to subclass the "typing.Generic"
# abstract base class (ABC) regardless of whether those classes
# originally did so explicitly. How? By type erasure, of course, the
# malicious gift that keeps on giving:
# >>> import typing as t
# >>> class MuhList(t.List): pass
# >>> MuhList.__orig_bases__
# (typing.List)
# >>> MuhList.__mro__
# (__main__.MuhList, list, typing.Generic, object)
#
# Note that:
# * This issubclass() call implicitly performs a surprisingly
# inefficient search over the method resolution order (MRO) of all
# superclasses of this hint. In theory, the cost of this search might
# be circumventable by observing that this ABC is expected to reside
# at the second-to-last index of the tuple exposing this MRO far all
# generics by virtue of fragile implementation details violating
# privacy encapsulation. In practice, this codebase is already
# fragile enough.
# * The following logic superficially appears to implement the same
# test *WITHOUT* the onerous cost of a search:
# return len(get_hint_pep484_generic_bases_unerased_or_none(hint)) > 0
# Why didn't we opt for that, then? Because this tester is routinely
# passed objects that *CANNOT* be guaranteed to be PEP-compliant.
# Indeed, the high-level is_hint_pep() tester establishing the
# PEP-compliance of arbitrary objects internally calls this
# lower-level tester to do so. Since the
# get_hint_pep484_generic_bases_unerased_or_none() getter internally
# reduces to returning the tuple of the general-purpose
# "__orig_bases__" dunder attribute formalized by PEP 560, testing
# whether that tuple is non-empty or not in no way guarantees this
# object to be a PEP-compliant generic.
return is_type_subclass(hint, Generic) # type: ignore[arg-type]
# Else, the active Python interpreter targets Python 3.6.x. In this case,
# implement this function specific to this Python version.
else:
# Import the Python 3.6.x-specific metaclass required by this tester.
from typing import GenericMeta # type: ignore[attr-defined]
def is_hint_pep484_generic(hint: object) -> bool:
# Avoid circular import dependencies.
from beartype._util.hint.pep.proposal.pep484585.utilpepgeneric import (
get_hint_pep484585_generic_type_or_none)
from beartype._util.hint.pep.utilpeptest import (
is_hint_pep_type_typing)
# If this hint is *NOT* a class, reduce this hint to the object
# originating this hint if any. See the above tester for details.
hint_type = get_hint_pep484585_generic_type_or_none(hint)
# Return true only if...
#
# Note that:
# * The Python >= 3.7.0-specific implementation of this tester does
# *NOT* apply to Python < 3.7.0, as this metaclass unconditionally
# raises exceptions when user-defined "typing" subclasses internally
# requiring this metaclass are passed to the issubclass() builtin.
# * This tester intentionally avoids returning true for *ALL* generics
# (including both those internally defined by the "typing" module and
# those externally defined by third-party callers). Why? Because
# generics internally defined by the "typing" module are effectively
# *NOT* generics and only implemented as such under Python < 3.7.0
# for presumably indefensible low-level reasons -- including:
# * *ALL* callable types (e.g., "typing.Awaitable",
# "typing.Callable", "typing.Coroutine", "typing.Generator").
# * *MOST* container and iterable types (e.g., "typing.Dict",
# "typing.List", "typing.Mapping", "typing.Tuple").
#
# If this tester returned true for *ALL* generics, downstream callers
# would have no means of distinguishing genuine generics from
# disingenuous "typing" pseudo-generics.
return (
# The metaclass of this hint is the "typing.GenericMeta" metaclass
# *AND*...
isinstance(hint_type, GenericMeta) and
# This hint is either...
(
# A parametrization of the "typing.Generic" class (e.g.,
# "Generic[S, T]") *OR*...
hint_type is Generic or
# A subclass *NOT* defined by the "typing" module. See above.
not is_hint_pep_type_typing(hint_type)
)
)
# Docstring for this function regardless of implementation details.
is_hint_pep484_generic.__doc__ = '''
``True`` only if the passed object is a :pep:`484`-compliant **generic**
(i.e., object that may *not* actually be a class originally subclassing at
least one PEP-compliant type hint defined by the :mod:`typing` module).
Specifically, this tester returns ``True`` only if this object was
originally defined as a class subclassing a combination of:
* At least one of:
* The :pep:`484`-compliant :mod:`typing.Generic` superclass.
* The :pep:`544`-compliant :mod:`typing.Protocol` superclass.
* Zero or more non-class :mod:`typing` pseudo-superclasses (e.g.,
``typing.List[int]``).
* Zero or more other standard superclasses.
This tester is intentionally *not* memoized (e.g., by the
:func:`callable_cached` decorator), as the implementation trivially reduces
to an efficient one-liner.
Parameters
----------
hint : object
Object to be inspected.
Returns
----------
bool
``True`` only if this object is a :mod:`typing` generic.
'''
# ....................{ TESTERS ~ newtype }....................
# If the active Python interpreter targets Python >= 3.10 and thus defines
# "typing.NewType" type hints as instances of that class, implement this tester
# unique to prior Python versions to raise an exception.
if IS_PYTHON_AT_LEAST_3_10:
def is_hint_pep484_newtype_pre_python310(hint: object) -> bool:
raise BeartypeDecorHintPep484Exception(
'is_hint_pep484_newtype_pre_python310() assumes Python < 3.10, '
'but current Python interpreter targets Python >= 3.10.'
)
# Else, the active Python interpreter targets Python < 3.10 and thus defines
# "typing.NewType" type hints as closures returned by that function. Since
# these closures are sufficiently dissimilar from all other type hints to
# require unique detection, implement this tester unique to this obsolete
# Python version to detect these closures.
else:
def is_hint_pep484_newtype_pre_python310(hint: object) -> bool:
# Return true only if...
return (
# This hint is a pure-Python function *AND*...
#
# Note that we intentionally do *NOT* call the callable() builtin
# here, as that builtin erroneously returns false positives for
# non-standard classes defining the __call__() dunder method to
# unconditionally raise exceptions. Critically, this includes most
# PEP 484-compliant type hints, which naturally fail to define both
# the "__module__" *AND* "__qualname__" dunder instance variables
# accessed below. Shoot me now, fam.
isinstance(hint, FunctionType) and
# This callable is a closure created and returned by the
# typing.NewType() function. Note that:
#
# * The "__module__" and "__qualname__" dunder instance variables
# are *NOT* generally defined for arbitrary objects but are
# specifically defined for callables.
# * "__qualname__" is safely available under Python >= 3.3.
# * This test derives from the observation that the concatenation
# of this callable's "__qualname__" and "__module" dunder
# instance variables suffices to produce a string unambiguously
# identifying whether this hint is a "NewType"-generated closure:
# >>> from typing import NewType
# >>> UserId = t.NewType('UserId', int)
# >>> UserId.__qualname__
# >>> 'NewType.<locals>.new_type'
# >>> UserId.__module__
# >>> 'typing'
f'{hint.__module__}.{hint.__qualname__}'.startswith(
'typing.NewType.')
)
is_hint_pep484_newtype_pre_python310.__doc__ = '''
``True`` only if the passed object either is a :pep:`484`-compliant **new
type** (i.e., closure created and returned by the :func:`typing.NewType`
closure factory function).
This tester is intentionally *not* memoized (e.g., by the
:func:`callable_cached` decorator), as the implementation trivially reduces
to an efficient one-liner.
Caveats
----------
**New type aliases are a complete farce and thus best avoided.**
Specifically, these PEP-compliant type hints are *not* actually types but
rather **identity closures** that return their passed parameters as is.
Instead, where distinct types are:
* *Not* required, simply annotate parameters and return values with the
desired superclasses.
* Required, simply:
* Subclass the desired superclasses as usual.
* Annotate parameters and return values with those subclasses.
Parameters
----------
hint : object
Object to be inspected.
Returns
----------
bool
``True`` only if this object is a :pep:`484`-compliant new type.
'''
# ....................{ GETTERS ~ forwardref }....................
@callable_cached
def get_hint_pep484_forwardref_type_basename(hint: Any) -> str:
'''
**Unqualified classname** (i.e., name of a class *not* containing a ``.``
delimiter and thus relative to the fully-qualified name of the lexical
scope declaring that class) referred to by the passed :pep:`484`-compliant
**forward reference type hint** (i.e., instance of the
:class:`typing.ForwardRef` class implicitly replacing all string arguments
subscripting :mod:`typing` objects).
This tester is intentionally *not* memoized (e.g., by the
:func:`callable_cached` decorator), as the implementation trivially reduces
to an efficient one-liner.
Parameters
----------
hint : object
Object to be inspected.
Returns
----------
str
Unqualified classname referred to by this :pep:`484`-compliant forward
reference type hint.
Raises
----------
BeartypeDecorHintForwardRefException
If this object is *not* a :pep:`484`-compliant forward reference.
See Also
----------
:func:`is_hint_pep484_forwardref`
Further commentary.
'''
# If this object is *NOT* a PEP 484-compliant forward reference, raise an
# exception.
if not is_hint_pep484_forwardref(hint):
raise BeartypeDecorHintForwardRefException(
f'Type hint {repr(hint)} not forward reference.')
# Else, this object is a PEP 484-compliant forward reference.
# Return the unqualified classname referred to by this reference. Note
# that:
# * This requires violating privacy encapsulation by accessing a dunder
# instance variable unique to the "typing.ForwardRef" class.
# * This object defines a significant number of other "__forward_"-prefixed
# dunder instance variables, which exist *ONLY* to enable the blatantly
# useless typing.get_type_hints() function to avoid repeatedly (and thus
# inefficiently) reevaluating the same forward reference. *sigh*
return hint.__forward_arg__
# ....................{ GETTERS ~ newtype }....................
def get_hint_pep484_newtype_class(hint: Any) -> type:
'''
User-defined class aliased by the passed :pep:`484`-compliant **new type**
(i.e., closure created and returned by the :func:`typing.NewType` closure
factory function).
This getter is intentionally *not* memoized (e.g., by the
:func:`callable_cached` decorator), as the implementation trivially reduces
to an efficient one-liner.
Parameters
----------
hint : object
Object to be inspected.
Returns
----------
type
User-defined class aliased by this :pep:`484`-compliant new type.
Raises
----------
BeartypeDecorHintPep484Exception
If this object is *not* a :pep:`484`-compliant new type.
See Also
----------
:func:`is_hint_pep484_newtype`
Further commentary.
'''
# Avoid circular import dependencies.
from beartype._util.hint.pep.utilpepget import get_hint_pep_sign
# If this object is *NOT* a PEP 484-compliant "NewType" hint, raise an
# exception.
if get_hint_pep_sign(hint) is not HintSignNewType:
raise BeartypeDecorHintPep484Exception(
f'Type hint {repr(hint)} not "typing.NewType".')
# Else, this object is a PEP 484-compliant "NewType" hint.
# Return the unqualified classname referred to by this reference. Note
# that this requires violating privacy encapsulation by accessing a dunder
# instance variable unique to closures created by the typing.NewType()
# closure factory function.
return hint.__supertype__
# ....................{ GETTERS ~ generic }....................
@callable_cached
def get_hint_pep484_generic_base_erased_from_unerased(hint: Any) -> type:
'''
Erased superclass originating the passed :pep:`484`-compliant **unerased
pseudo-superclass** (i.e., :mod:`typing` object originally listed as a
superclass prior to its implicit type erasure by the :mod:`typing` module).
This getter is intentionally *not* memoized (e.g., by the
:func:`callable_cached` decorator), as the implementation trivially reduces
to an efficient one-liner.
Parameters
----------
hint : object
:pep:`484`-compliant unerased pseudo-superclass to be reduced to its
erased superclass.
Returns
----------
type
Erased superclass originating this :pep:`484`-compliant unerased
pseudo-superclass.
Raises
----------
BeartypeDecorHintPep484Exception
if this object is *not* a :pep:`484`-compliant unerased
pseudo-superclass.
'''
# Avoid circular import dependencies.
from beartype._util.hint.pep.utilpepget import get_hint_pep_origin_or_none
# Erased superclass originating this unerased pseudo-superclass if any *OR*
# "None" otherwise.
hint_type_origin = get_hint_pep_origin_or_none(hint)
# If this hint originates from *NO* such superclass, raise an exception.
if hint_type_origin is None:
raise BeartypeDecorHintPep484Exception(
f'Unerased PEP 484 generic or PEP 544 protocol {repr(hint)} '
f'originates from no erased superclass.'
)
# Else, this hint originates from such a superclass.
# Return this superclass.
return hint_type_origin
@callable_cached
def get_hint_pep484_generic_bases_unerased(hint: Any) -> tuple:
'''
Tuple of all unerased :mod:`typing` **pseudo-superclasses** (i.e.,
:mod:`typing` objects originally listed as superclasses prior to their
implicit type erasure under :pep:`560`) of the passed :pep:`484`-compliant
**generic** (i.e., class subclassing at least one non-class :mod:`typing`
object).
This getter is memoized for efficiency.
Parameters
----------
hint : object
Object to be inspected.
Returns
----------
Tuple[object]
Tuple of the one or more unerased pseudo-superclasses of this
:mod:`typing` generic. Specifically:
* If this generic defines an ``__orig_bases__`` dunder instance
variable, the value of that variable.
* Else, the value of the ``__mro__`` dunder instance variable stripped
of all ignorable classes conveying no semantic meaning, including:
* This generic itself.
* The :class:`typing.Generic` superclass.
* The :class:`object` root superclass.
Raises
----------
BeartypeDecorHintPep484Exception
If this hint is either:
* *Not* a :mod:`typing` generic.
* A :mod:`typing` generic that erased *none* of its superclasses but
whose method resolution order (MRO) lists strictly less than four
classes. Valid :pep:`484`-compliant generics should list at least
four classes, including (in order):
#. This class itself.
#. The one or more :mod:`typing` objects directly subclassed by this
generic.
#. The :class:`typing.Generic` superclass.
#. The :class:`object` root superclass.
See Also
----------
:func:`beartype._util.hint.pep.proposal.pep484585.utilpepgeneric.get_hint_pep484585_generic_bases_unerased`
Further details.
'''
#FIXME: This tuple appears to be implemented erroneously -- at least under
#Python 3.7, anyway. Although this tuple is implemented correctly for the
#common case of user-defined types directly subclassing "typing" types,
#this tuple probably is *NOT* implemented correctly for the edge case of
#user-defined types indirectly subclassing "typing" types: e.g.,
#
# >>> import collections.abc, typing
# >>> T = typing.TypeVar('T')
# >>> class Direct(collections.abc.Sized, typing.Generic[T]): pass
# >>> Direct.__orig_bases__
# (collections.abc.Sized, typing.Generic[~T])
# >>> class Indirect(collections.abc.Container, Direct): pass
# >>> Indirect.__orig_bases__
# (collections.abc.Sized, typing.Generic[~T])
#
#*THAT'S COMPLETELY INSANE.* Clearly, their naive implementation failed to
#account for actual real-world use cases.
#
#On the bright side, the current implementation prevents us from actually
#having to perform a breadth-first traversal of all original superclasses
#of this class in method resolution order (MRO). On the dark side, it's
#pants-on-fire balls -- but there's not much we can do about that. *sigh*
#
#If we ever need to perform that breadth-first traversal, resurrect this:
#
# # If this class was *NOT* subject to type erasure, reduce to a noop.
# if not hint_bases:
# return hint_bases
#
# # Fixed list of all typing super attributes to be returned.
# superattrs = acquire_fixed_list(SIZE_BIG)
#
# # 0-based index of the last item of this list.
# superattrs_index = 0
#
# # Fixed list of all transitive superclasses originally listed by this
# # class iterated in method resolution order (MRO).
# hint_orig_mro = acquire_fixed_list(SIZE_BIG)
#
# # 0-based indices of the current and last items of this list.
# hint_orig_mro_index_curr = 0
# hint_orig_mro_index_last = 0
#
# # Initialize this list with the tuple of all direct superclasses of this
# # class, which iteration then expands to all transitive superclasses.
# hint_orig_mro[:len(hint_bases)] = hint_bases
#
# # While the heat death of the universe has been temporarily forestalled...
# while (True):
# # Currently visited superclass of this class.
# hint_base = hint_orig_mro[hint_orig_mro_index_curr]
#
# # If this superclass is a typing attribute...
# if is_hint_pep_type_typing(hint_base):
# # Avoid inserting this attribute into the "hint_orig_mro" list.
# # Most typing attributes are *NOT* actual classes and those that
# # are have no meaningful public superclass. Ergo, iteration
# # terminates with typing attributes.
# #
# # Insert this attribute at the current item of this list.
# superattrs[superattrs_index] = hint_base
#
# # Increment this index to the next item of this list.
# superattrs_index += 1
#
# # If this class subclasses more than the maximum number of "typing"
# # attributes supported by this function, raise an exception.
# if superattrs_index >= SIZE_BIG:
# raise BeartypeDecorHintPep560Exception(
# '{} PEP type {!r} subclasses more than '
# '{} "typing" types.'.format(
# exception_prefix,
# hint,
# SIZE_BIG))
# # Else, this superclass is *NOT* a typing attribute. In this case...
# else:
# # Tuple of all direct superclasses originally listed by this class
# # prior to PEP 484 type erasure if any *OR* the empty tuple
# # otherwise.
# hint_base_bases = getattr(hint_base, '__orig_bases__')
#
# #FIXME: Implement breadth-first traversal here.
#
# # Tuple sliced from the prefix of this list assigned to above.
# superattrs_tuple = tuple(superattrs[:superattrs_index])
#
# # Release and nullify this list *AFTER* defining this tuple.
# release_fixed_list(superattrs)
# del superattrs
#
# # Return this tuple as is.
# return superattrs_tuple
#
#Also resurrect this docstring snippet:
#
# Raises
# ----------
# BeartypeDecorHintPep560Exception
# If this object defines the ``__orig_bases__`` dunder attribute but that
# attribute transitively lists :data:`SIZE_BIG` or more :mod:`typing`
# attributes.
#
#Specifically:
# * Acquire a fixed list of sufficient size (e.g., 64). We probably want
# to make this a constant in "utilcachelistfixedpool" for reuse
# everywhere, as this is clearly becoming a common idiom.
# * Slice-assign "__orig_bases__" into this list.
# * Maintain two simple 0-based indices into this list:
# * "bases_index_curr", the current base being visited.
# * "bases_index_last", the end of this list also serving as the list
# position to insert newly discovered bases at.
# * Iterate over this list and keep slice-assigning from either
# "__orig_bases__" (if defined) or "__mro__" (otherwise) into
# "list[bases_index_last:len(__orig_bases__)]". Note that this has the
# unfortunate disadvantage of temporarily iterating over duplicates,
# but... *WHO CARES.* It still works and we subsequently
# eliminate duplicates at the end.
# * Return a frozenset of this list, thus implicitly eliminating
# duplicate superclasses.
# Avoid circular import dependencies.
from beartype._util.hint.pep.proposal.pep484585.utilpepgeneric import (
get_hint_pep484585_generic_type_or_none)
# If this hint is *NOT* a class, reduce this hint to the object originating
# this hint if any. See is_hint_pep484_generic() for details.
hint = get_hint_pep484585_generic_type_or_none(hint)
# If this hint is *NOT* a PEP 484-compliant generic, raise an exception.
if not is_hint_pep484_generic(hint):
raise BeartypeDecorHintPep484Exception(
f'Type hint {repr(hint)} neither '
f'PEP 484 generic nor PEP 544 protocol.'
)
# Else, this hint is a PEP 484-compliant generic.
# Unerased pseudo-superclasses of this generic if any *OR* "None"
# otherwise (e.g., if this generic is a single-inherited protocol).
hint_bases = getattr(hint, '__orig_bases__', None)
# If this generic erased its superclasses, return these superclasses as is.
if hint_bases is not None:
return hint_bases
# Else, this generic erased *NONE* of its superclasses. These superclasses
# *MUST* by definition be unerased and thus safely returnable as is. In
# this case...
# Unerased superclasses of this generic defined by the method resolution
# order (MRO) for this generic.
hint_bases = hint.__mro__
# Substring prefixing all exceptions raised below.
EXCEPTION_STR_PREFIX = (
f'PEP 484 generic {repr(hint)} '
f'method resolution order {repr(hint_bases)}'
)
# If this MRO lists strictly less than four classes, raise an exception.
# The MRO for any unerased generic should list at least four classes:
# * This class itself.
# * The one or more "typing" objects directly subclassed by this generic.
# * The "typing.Generic" superclass.
# * The "object" root superclass.
if len(hint_bases) < 4:
raise BeartypeDecorHintPep484Exception(
f'{EXCEPTION_STR_PREFIX} lists less than four classes.')
# Else, this MRO lists at least four classes.
#
# If any class listed by this MRO fails to comply with the above
# expectations, raise an exception.
elif hint_bases[0] != hint:
raise BeartypeDecorHintPep484Exception(
f'{EXCEPTION_STR_PREFIX} first item not {hint}.')
elif hint_bases[-2] != Generic:
raise BeartypeDecorHintPep484Exception(
f'{EXCEPTION_STR_PREFIX} second-to-last item not {Generic}.')
elif hint_bases[-1] != object:
raise BeartypeDecorHintPep484Exception(
f'{EXCEPTION_STR_PREFIX} last item not {object}.')
# Else, all classes listed by this MRO comply with the above expectations.
# Return a slice of this tuple preserving *ONLY* the non-ignorable
# superclasses listed by this tuple for conformance with the tuple returned
# by this getter from the "__orig_bases__", which similarly lists *ONLY*
# non-ignorable superclasses. Specifically, strip from this tuple:
# * This class itself.
# * The "typing.Generic" superclass.
# * The "object" root superclass.
return hint_bases[1:-2]