Skip to content

Commit f473ef1

Browse files
committed
Fix type checking for constructing tuples.
1 parent 4be3e0d commit f473ef1

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

gel/_internal/_codegen/_models/_pydantic.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,14 @@ def _indirection_key(path: Indirection) -> tuple[str, ...]:
839839
SchemaPath("std", "multirange"),
840840
}
841841
)
842+
COLLECTION_TYPES = frozenset(
843+
{
844+
SchemaPath("std", "array"),
845+
SchemaPath("std", "tuple"),
846+
SchemaPath("std", "range"),
847+
SchemaPath("std", "multirange"),
848+
}
849+
)
842850

843851
PSEUDO_TYPES = frozenset(("anytuple", "anyobject", "anytype"))
844852

@@ -2517,7 +2525,17 @@ def write_generic_types(
25172525
suggested_module_alias=rel_import.module_alias,
25182526
)
25192527
type_ident = ident(gt.name)
2520-
self.write(f"{type_ident} = {imported_name}")
2528+
2529+
if gt in COLLECTION_TYPES:
2530+
# Import collection classes directly so mypy sees them as
2531+
# generic classes, not type aliases
2532+
self.write(
2533+
f"from {rel_import.module} "
2534+
f"import {type_ident} as {type_ident}"
2535+
)
2536+
else:
2537+
self.write(f"{type_ident} = {imported_name}")
2538+
25212539
self.export(type_ident)
25222540

25232541
def _write_enum_scalar_type(

gel/_internal/_qbmodel/_abstract/_primitive.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ def __edgeql_literal__(self) -> _qb.Literal:
358358
)
359359

360360
@classmethod
361-
def cast(cls, expr: _qb.ExprCompatible) -> type[Tuple[Unpack[_Ts]]]:
361+
def cast(cls, expr: _qb.ExprCompatible) -> Self:
362362
return _qb.AnnotatedExpr( # type: ignore [return-value]
363363
cls,
364364
_qb.CastOp(
@@ -367,6 +367,10 @@ def cast(cls, expr: _qb.ExprCompatible) -> type[Tuple[Unpack[_Ts]]]:
367367
),
368368
)
369369

370+
if TYPE_CHECKING:
371+
def __new__(cls, args: tuple[Any, ...]) -> Self: ... # type: ignore[misc]
372+
def __init__(self, args: tuple[Unpack[_Ts]]): ... # type: ignore[misc]
373+
370374

371375
if TYPE_CHECKING:
372376

tests/test_qb.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1399,13 +1399,13 @@ def test_qb_cast_array_04(self):
13991399
std.array[std.tuple[std.str, std.str]](
14001400
[
14011401
std.tuple[std.str, std.str](
1402-
[std.str("1"), std.str("Red")]
1402+
(std.str("1"), std.str("Red"))
14031403
),
14041404
std.tuple[std.str, std.str](
1405-
[std.str("2"), std.str("Green")]
1405+
(std.str("2"), std.str("Green"))
14061406
),
14071407
std.tuple[std.str, std.str](
1408-
[std.str("3"), std.str("Blue")]
1408+
(std.str("3"), std.str("Blue"))
14091409
),
14101410
]
14111411
)

0 commit comments

Comments
 (0)