Skip to content

Commit 025e667

Browse files
committed
(imp) support aliases in get_specialized_type_var_map
1 parent 7ba5928 commit 025e667

File tree

4 files changed

+79
-38
lines changed

4 files changed

+79
-38
lines changed

RELEASE.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Release type: minor
2+
3+
Support aliases (TypeVar passthrough) in `get_specialized_type_var_map`.

strawberry/utils/inspect.py

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
import asyncio
22
import inspect
3+
from collections import OrderedDict
34
from functools import lru_cache
5+
from itertools import zip_longest
46
from typing import (
57
Any,
68
Callable,
9+
Generic,
710
Optional,
11+
Protocol,
812
TypeVar,
13+
Union,
914
get_origin,
1015
)
1116
from typing_extensions import get_args
1217

13-
from strawberry.utils.typing import is_generic_alias
14-
1518

1619
def in_async_context() -> bool:
1720
# Based on the way django checks if there's an event loop in the current thread
@@ -67,57 +70,60 @@ class IntBarFoo(IntBar, Foo[str]): ...
6770
# {}
6871
6972
get_specialized_type_var_map(Bar)
70-
# {~T: ~T}
73+
# {}
7174
7275
get_specialized_type_var_map(IntBar)
73-
# {~T: int}
76+
# {~T: int, ~K: int}
7477
7578
get_specialized_type_var_map(IntBarSubclass)
76-
# {~T: int}
79+
# {~T: int, ~K: int}
7780
7881
get_specialized_type_var_map(IntBarFoo)
7982
# {~T: int, ~K: str}
8083
```
8184
"""
8285
from strawberry.types.base import has_object_definition
8386

84-
orig_bases = getattr(cls, "__orig_bases__", None)
85-
if orig_bases is None:
86-
# Specialized generic aliases will not have __orig_bases__
87-
if get_origin(cls) is not None and is_generic_alias(cls):
88-
orig_bases = (cls,)
89-
else:
90-
# Not a specialized type
91-
return None
92-
93-
type_var_map = {}
94-
95-
# only get type vars for base generics (ie. Generic[T]) and for strawberry types
87+
param_args = OrderedDict[TypeVar, Union[None, TypeVar, type]]()
9688

97-
orig_bases = [b for b in orig_bases if has_object_definition(b)]
89+
types: list[type] = [cls]
90+
while types:
91+
tp = types.pop(0)
92+
if (origin := get_origin(tp)) is None or origin in (Generic, Protocol):
93+
origin = tp
9894

99-
for base in orig_bases:
100-
# Recursively get type var map from base classes
101-
if base is not cls:
102-
base_type_var_map = get_specialized_type_var_map(base)
103-
if base_type_var_map is not None:
104-
type_var_map.update(base_type_var_map)
105-
106-
args = get_args(base)
107-
origin = getattr(base, "__origin__", None)
108-
109-
params = origin and getattr(origin, "__parameters__", None)
110-
if params is None:
111-
params = getattr(base, "__parameters__", None)
112-
113-
if not params:
95+
# only get type vars for base generics (i.e. Generic[T]) and for strawberry types
96+
if not has_object_definition(origin):
11497
continue
11598

116-
type_var_map.update(
117-
{p.__name__: a for p, a in zip(params, args) if not isinstance(a, TypeVar)}
118-
)
119-
120-
return type_var_map
99+
if (type_params := getattr(origin, "__parameters__", None)) is not None:
100+
args = get_args(tp)
101+
for type_param, arg in zip_longest(type_params, args):
102+
if type_param not in param_args:
103+
param_args[type_param] = arg
104+
105+
if orig_bases := getattr(origin, "__orig_bases__", None):
106+
types.extend(orig_bases)
107+
if not param_args:
108+
return None
109+
110+
resolve = True
111+
while resolve:
112+
resolve = False
113+
for type_param, arg in list(param_args.items()):
114+
if arg is None or not isinstance(arg, TypeVar):
115+
continue
116+
resolved_arg = param_args.get(arg, None) if arg is not type_param else None
117+
param_args[type_param] = resolved_arg
118+
119+
if resolved_arg:
120+
resolve = True
121+
122+
return {
123+
k.__name__: v
124+
for k, v in reversed(param_args.items())
125+
if v is not None and not isinstance(v, TypeVar)
126+
}
121127

122128

123129
__all__ = ["get_func_args", "get_specialized_type_var_map", "in_async_context"]

tests/python_312/test_inspect.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,22 @@ class BinSubclass(Bin): ...
9191
assert get_specialized_type_var_map(Bin) == {"_T": int}
9292

9393

94+
def test_get_specialized_type_var_map_double_generic_passthrough():
95+
@strawberry.type
96+
class Foo[_T]: ...
97+
98+
@strawberry.type
99+
class Bar[_K](Foo[_K]): ...
100+
101+
@strawberry.type
102+
class Bin(Bar[int]): ...
103+
104+
assert get_specialized_type_var_map(Bin) == {
105+
"_T": int,
106+
"_K": int,
107+
}
108+
109+
94110
def test_get_specialized_type_var_map_multiple_inheritance():
95111
@strawberry.type
96112
class Foo[_T]: ...

tests/utils/test_inspect.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,22 @@ class BinSubclass(Bin): ...
9494
assert get_specialized_type_var_map(Bin) == {"_T": int}
9595

9696

97+
def test_get_specialized_type_var_map_double_generic_passthrough():
98+
@strawberry.type
99+
class Foo(Generic[_T]): ...
100+
101+
@strawberry.type
102+
class Bar(Foo[_K], Generic[_K]): ...
103+
104+
@strawberry.type
105+
class Bin(Bar[int]): ...
106+
107+
assert get_specialized_type_var_map(Bin) == {
108+
"_T": int,
109+
"_K": int,
110+
}
111+
112+
97113
def test_get_specialized_type_var_map_multiple_inheritance():
98114
@strawberry.type
99115
class Foo(Generic[_T]): ...

0 commit comments

Comments
 (0)