Skip to content

Commit 1bb557c

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

File tree

4 files changed

+78
-38
lines changed

4 files changed

+78
-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: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
import asyncio
22
import inspect
33
from functools import lru_cache
4+
from itertools import zip_longest
45
from typing import (
56
Any,
67
Callable,
8+
Generic,
79
Optional,
10+
Protocol,
811
TypeVar,
12+
Union,
913
get_origin,
1014
)
1115
from typing_extensions import get_args
1216

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

1618
def in_async_context() -> bool:
1719
# Based on the way django checks if there's an event loop in the current thread
@@ -67,57 +69,60 @@ class IntBarFoo(IntBar, Foo[str]): ...
6769
# {}
6870
6971
get_specialized_type_var_map(Bar)
70-
# {~T: ~T}
72+
# {}
7173
7274
get_specialized_type_var_map(IntBar)
73-
# {~T: int}
75+
# {~T: int, ~K: int}
7476
7577
get_specialized_type_var_map(IntBarSubclass)
76-
# {~T: int}
78+
# {~T: int, ~K: int}
7779
7880
get_specialized_type_var_map(IntBarFoo)
7981
# {~T: int, ~K: str}
8082
```
8183
"""
8284
from strawberry.types.base import has_object_definition
8385

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
86+
param_args: dict[TypeVar, Union[None, TypeVar, type]] = {}
9687

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

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:
94+
# only get type vars for base generics (i.e. Generic[T]) and for strawberry types
95+
if not has_object_definition(origin):
11496
continue
11597

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

122127

123128
__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)