Skip to content

Commit 840845f

Browse files
authored
Make the return type of type intersection the same as the argument type. (#954)
1 parent 17035e8 commit 840845f

3 files changed

Lines changed: 9 additions & 26 deletions

File tree

gel/_internal/_qbmodel/_abstract/_methods.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@
4242
from collections.abc import Callable
4343

4444

45-
_T_SelfModel = TypeVar("_T_SelfModel", bound="type[BaseGelModel]")
46-
_T_OtherModel = TypeVar("_T_OtherModel", bound="type[BaseGelModel]")
45+
_T_OtherModel = TypeVar("_T_OtherModel", bound="BaseGelModel")
4746

4847

4948
class BaseGelModel(AbstractGelModel):
@@ -84,10 +83,13 @@ def limit(cls, /, expr: Any) -> type[Self]: ...
8483
@classmethod
8584
def offset(cls, /, expr: Any) -> type[Self]: ...
8685

86+
# We pretend that the return type is _T_OtherModel so that the type
87+
# checker is aware of _T_OtherModel's pointers. We don't get Self's
88+
# pointers, but that's ok most of the time.
8789
@classmethod
8890
def is_(
89-
cls: _T_SelfModel, /, other_model: _T_OtherModel
90-
) -> type[BaseGelModelIntersection[_T_SelfModel, _T_OtherModel]]: ...
91+
cls: type[Self], /, other_model: type[_T_OtherModel]
92+
) -> type[_T_OtherModel]: ...
9193

9294
@classmethod
9395
def __gel_assert_single__(
@@ -207,11 +209,11 @@ def offset(
207209
@_qb.exprmethod
208210
@classmethod
209211
def is_(
210-
cls: _T_SelfModel,
212+
cls: type[Self],
211213
/,
212-
value: _T_OtherModel,
214+
value: type[_T_OtherModel],
213215
__operand__: _qb.ExprAlias | None = None,
214-
) -> type[BaseGelModelIntersection[_T_SelfModel, _T_OtherModel]]:
216+
) -> type[BaseGelModelIntersection[type[Self], type[_T_OtherModel]]]:
215217
return _qb.AnnotatedExpr( # type: ignore [return-value]
216218
create_intersection(cls, value),
217219
add_object_type_filter(cls, value, __operand__=__operand__),

gel/_internal/_qbmodel/_pydantic/_models.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -923,10 +923,6 @@ def __gel_validate__(cls, value: Any) -> GelSourceModel:
923923
return res
924924

925925

926-
_T_SelfModel = TypeVar("_T_SelfModel", bound="type[_abstract.BaseGelModel]")
927-
_T_OtherModel = TypeVar("_T_OtherModel", bound="type[_abstract.BaseGelModel]")
928-
929-
930926
class GelModel(
931927
GelSourceModel,
932928
_abstract.BaseGelModel,
@@ -1326,20 +1322,6 @@ def model_copy(
13261322
ll_setattr(copied, "__gel_new__", ll_getattr(self, "__gel_new__"))
13271323
return copied
13281324

1329-
if TYPE_CHECKING:
1330-
# Pretend that is_ returns a proper GelModel
1331-
@classmethod
1332-
def is_(
1333-
cls: _T_SelfModel, /, other_model: _T_OtherModel
1334-
) -> type[GelModelIntersection[_T_SelfModel, _T_OtherModel]]: ...
1335-
1336-
1337-
class GelModelIntersection(
1338-
GelModel, _abstract.BaseGelModelIntersection[_T_SelfModel, _T_OtherModel]
1339-
):
1340-
def __init__(self) -> None:
1341-
raise NotImplementedError("Type expressions cannot be instantiated.")
1342-
13431325

13441326
class GelLinkModel(
13451327
GelSourceModel,

tests/test_qb.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1581,7 +1581,6 @@ def test_qb_is_type_basic_03(self):
15811581
excluded_fields={'b', 'c', 'ab', 'ac', 'bc', 'abc', 'ab_ac'},
15821582
)
15831583

1584-
@tb.skip_typecheck
15851584
def test_qb_is_type_basic_04(self):
15861585
# Model Select
15871586
# with computed single prop using type intersection

0 commit comments

Comments
 (0)