/
_errorgeneric.py
115 lines (101 loc) · 5.38 KB
/
_errorgeneric.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
#!/usr/bin/env python3
# --------------------( LICENSE )--------------------
# Copyright (c) 2014-2021 Beartype authors.
# See "LICENSE" for further details.
'''
**Beartype PEP-compliant generic type hint exception raisers** (i.e., functions
raising human-readable exceptions called by :mod:`beartype`-decorated callables
on the first invalid parameter or return value failing a type-check against the
PEP-compliant generic type hint annotating that parameter or return).
This private submodule is *not* intended for importation by downstream callers.
'''
# ....................{ IMPORTS }....................
from beartype._decor._error._errortype import (
get_cause_or_none_type)
from beartype._decor._error._errorsleuth import CauseSleuth
from beartype._data.hint.pep.sign.datapepsigns import HintSignGeneric
from beartype._util.hint.pep.proposal.utilpep484 import (
get_hint_pep484_generic_base_erased_from_unerased)
from beartype._util.hint.pep.proposal.utilpep585 import (
is_hint_pep585_builtin)
from beartype._util.hint.pep.utilpepget import (
get_hint_pep_generic_type_or_none)
from beartype._util.hint.pep.utilpeptest import is_hint_pep_typing
from beartype._util.hint.utilhinttest import is_hint_ignorable
from typing import Optional
# See the "beartype.cave" submodule for further commentary.
__all__ = ['STAR_IMPORTS_CONSIDERED_HARMFUL']
# ....................{ GETTERS }....................
def get_cause_or_none_generic(sleuth: CauseSleuth) -> Optional[str]:
'''
Human-readable string describing the failure of the passed arbitrary object
to satisfy the passed `PEP 484`_-compliant **generic** (i.e., type hint
subclassing a combination of one or more of the :mod:`typing.Generic`
superclass, the :mod:`typing.Protocol` superclass, and/or other
:mod:`typing` non-class pseudo-superclasses) if this object actually fails
to satisfy this hint *or* ``None`` otherwise (i.e., if this object
satisfies this hint).
Parameters
----------
sleuth : CauseSleuth
Type-checking error cause sleuth.
'''
assert isinstance(sleuth, CauseSleuth), f'{repr(sleuth)} not cause sleuth.'
assert sleuth.hint_sign is HintSignGeneric, (
f'{repr(sleuth.hint_sign)} not generic.')
# If this hint is *NOT* a class, reduce this hint to the object originating
# this hint if any. See the is_hint_pep484_generic() tester for details.
sleuth.hint = get_hint_pep_generic_type_or_none(sleuth.hint)
assert isinstance(sleuth.hint, type), f'{repr(sleuth.hint)} not class.'
# Human-readable string describing the failure of this pith to be an
# instance of this generic if this pith is not an instance of this generic
# *OR* "None" otherwise.
pith_cause = get_cause_or_none_type(sleuth)
# If this pith is *NOT* an instance of this generic, return this string.
if pith_cause is not None:
return pith_cause
# Else, this pith is an instance of this generic.
# For each pseudo-superclass of this generic...
for hint_base in sleuth.hint_childs:
# If this pseudo-superclass is an actual superclass, this
# pseudo-superclass is effectively ignorable. Why? Because the
# isinstance() call above already type-checked this pith against the
# generic subclassing this superclass and thus this superclass as well.
# In this case, skip to the next pseudo-superclass.
if isinstance(hint_base, type):
continue
# Else, this pseudo-superclass is *NOT* an actual class.
#
# If this pseudo-superclass is neither...
elif not (
# A PEP 585-compliant type hint *NOR*...
is_hint_pep585_builtin(hint_base) and
# A PEP-compliant type hint defined by the "typing" module...
is_hint_pep_typing(hint_base)
):
# Reduce this pseudo-superclass to the real superclass originating
# this pseudo-superclass. See the "_pephint" submodule.
hint_base = get_hint_pep484_generic_base_erased_from_unerased(
hint_base)
# Else, this pseudo-superclass is defined by the "typing" module.
# If this superclass is ignorable, do so.
if is_hint_ignorable(hint_base):
continue
# Else, this superclass is unignorable.
# Human-readable string describing the failure of this pith to satisfy
# this pseudo-superclass if this pith actually fails to satisfy
# this pseudo-superclass *or* "None" otherwise.
# print(f'tuple pith: {pith_item}\ntuple hint child: {hint_child}')
pith_base_cause = sleuth.permute(hint=hint_base).get_cause_or_none()
# If this pseudo-superclass is the cause of this failure, return a
# substring describing this failure by embedding this failure in a
# longer string.
if pith_base_cause is not None:
# print(f'tuple pith: {sleuth_copy.pith}\ntuple hint child: {sleuth_copy.hint}\ncause: {pith_item_cause}')
return f'generic base {repr(hint_base)} {pith_base_cause}'
# Else, this pseudo-superclass is *NOT* the cause of this failure.
# Silently continue to the next.
# Return "None", as this pith satisfies both this generic itself *AND* all
# pseudo-superclasses subclassed by this generic, implying this pith to
# deeply satisfy this hint.
return None