Repository navigation
Expand file tree
/
Copy pathbuiltin_genericalias.py
More file actions
96 lines (72 loc) · 2.87 KB
/
Copy pathbuiltin_genericalias.py
File metadata and controls
96 lines (72 loc) · 2.87 KB
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
# See for reference:
# https://github.com/python/cpython/blob/3.9/Objects/genericaliasobject.c
from typing import Text, Tuple, Any, Dict, Optional
from echo.enative_fn import ENativeFn
from echo.epy_object import EPyObject, EPyType, AttrWhere
from echo.interp_result import Result, check_result
from echo.eobjects import EMethod, get_guest_builtin, E_PREFIX
from echo.interp_context import ICtx
class EGenericAliasType(EPyType):
def get_bases(self):
raise NotImplementedError
def get_dict(self):
raise NotImplementedError
def get_mro(self) -> Tuple[EPyType, ...]:
raise NotImplementedError
def getattr(self, name: Text, ictx: ICtx) -> Result[Any]:
if name == '__call__':
return Result(self)
raise NotImplementedError
def setattr(self, name: Text, value: Any, ictx: ICtx) -> Any:
raise NotImplementedError
def hasattr_where(self, name: Text) -> Optional[AttrWhere]:
if name == '__call__':
return AttrWhere.SELF_SPECIAL
if name == '__eq__':
return AttrWhere.CLS
raise NotImplementedError(name)
def get_name(self) -> str:
return 'types.GenericAlias'
def __repr__(self) -> str:
return f"<{E_PREFIX}class 'types.GenericAlias'>"
def get_type(self) -> EPyType:
return get_guest_builtin('type')
@check_result
def invoke(self,
args: Tuple[Any, ...],
kwargs: Dict[Text, Any],
locals_dict: Dict[Text, Any],
ictx: ICtx,
globals_: Optional[Dict[Text, Any]] = None
) -> Result['EGenericAlias']:
assert isinstance(ictx, ICtx), ictx
origin, tup = args
return Result(EGenericAlias(origin, tup))
EGenericAliasType_singleton = EGenericAliasType()
class EGenericAlias(EPyObject):
def __init__(self, origin, args):
self.origin = origin
self.args = args
def get_type(self) -> EPyType:
return EGenericAliasType_singleton
def getattr(self, name: Text, ictx: ICtx) -> Result[Any]:
if name == '__eq__':
return Result(EMethod(ENativeFn(_do_ga_eq,
'types.GenericAlias.__eq__'),
bound_self=self))
raise NotImplementedError(self, name)
def setattr(self, name: Text, value: Any, ictx: ICtx) -> Any:
raise NotImplementedError
def hasattr_where(self, name: Text) -> Optional[AttrWhere]:
raise NotImplementedError
@check_result
def _do_ga_eq(
args: Tuple[Any, ...],
kwargs: Dict[Text, Any],
locals_dict: Dict[Text, Any],
ictx: ICtx,
globals_: Optional[Dict[Text, Any]] = None,
) -> Result[bool]:
assert len(args) == 2 and not kwargs, (args, kwargs)
lhs, rhs = args
return Result(lhs.origin == rhs.origin and lhs.args == rhs.args)