Skip to content

Commit 11f57e3

Browse files
committed
Tweak mro computation
1 parent e34d4ba commit 11f57e3

1 file changed

Lines changed: 17 additions & 11 deletions

File tree

typemap/type_eval/_apply_generic.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,19 @@ class Boxed:
2121
args: dict[Any, Any]
2222

2323
str_args: dict[str, Any] = dataclasses.field(init=False)
24+
mro: list[Boxed] = dataclasses.field(init=False)
2425

2526
def __post_init__(self):
2627
object.__setattr__(
2728
self,
2829
"str_args",
2930
{str(k): v for k, v in self.args.items()},
3031
)
32+
object.__setattr__(
33+
self,
34+
"mro",
35+
_compute_mro(self),
36+
)
3137

3238
def __repr__(self):
3339
return f"Boxed<{self.cls} {self.args}>"
@@ -90,6 +96,8 @@ def _box(cls: type[Any], args: dict[Any, Any]) -> Boxed:
9096

9197
if isinstance(cls, (typing._GenericAlias, types.GenericAlias)): # type: ignore[attr-defined]
9298
# XXX this feels out of place, `box()` needs to only accept types.
99+
# this never gets activated now, but I want to basically
100+
# support this later -sully
93101
args = dict(
94102
zip(cls.__origin__.__parameters__, cls.__args__, strict=True)
95103
)
@@ -103,10 +111,10 @@ def _box(cls: type[Any], args: dict[Any, Any]) -> Boxed:
103111
return _box(cls, args)
104112

105113

106-
def merge_boxed_mro(seqs: list[list[Boxed]]) -> list[Boxed]:
107-
res: list[Boxed] = []
114+
def merge_boxed_mro[T](seqs: list[list[T]]) -> list[T]:
115+
res: list[T] = []
108116
i = 0
109-
cand: Boxed | None = None
117+
cand: T | None = None
110118
while 1:
111119
nonemptyseqs = [seq for seq in seqs if seq]
112120
if not nonemptyseqs:
@@ -130,13 +138,7 @@ def merge_boxed_mro(seqs: list[list[Boxed]]) -> list[Boxed]:
130138

131139

132140
def _compute_mro(C: Boxed) -> list[Boxed]:
133-
return merge_boxed_mro(
134-
[[C]] + [_compute_mro(b) for b in C.bases] + [list(C.bases)]
135-
)
136-
137-
138-
def compute_mro(C: type[Any]) -> list[Boxed]:
139-
return _compute_mro(box(C))
141+
return merge_boxed_mro([[C]] + [b.mro for b in C.bases] + [list(C.bases)])
140142

141143

142144
def make_func(
@@ -173,7 +175,8 @@ def make_func(
173175
def apply(
174176
cls: type[Any], ctx: _eval_typing.EvalContext
175177
) -> type[_eval_typing._EvalProxy]:
176-
mro_boxed = compute_mro(cls)
178+
cls_boxed = box(cls)
179+
mro_boxed = cls_boxed.mro
177180

178181
annos: dict[str, Any] = {}
179182
dct: dict[str, Any] = {}
@@ -189,6 +192,9 @@ def apply(
189192
},
190193
)
191194

195+
# TODO: I think we want to create the whole mro chain...
196+
# before we evaluate the contents?
197+
192198
# Run through the mro
193199
for boxed in reversed(mro_boxed):
194200
if af := getattr(boxed.cls, "__annotate__", None):

0 commit comments

Comments
 (0)