Skip to content

Commit c86a616

Browse files
committed
fix actual_ret_type for TypeGuard
1 parent 58fdff5 commit c86a616

File tree

1 file changed

+37
-7
lines changed

1 file changed

+37
-7
lines changed

mypy/constraints.py

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
CONTRAVARIANT,
1818
COVARIANT,
1919
ArgKind,
20+
Block,
21+
ClassDef,
22+
SymbolTable,
2023
TypeInfo,
2124
)
2225
from mypy.types import (
@@ -1026,25 +1029,18 @@ def visit_callable_type(self, template: CallableType) -> list[Constraint]:
10261029
param_spec = template.param_spec()
10271030

10281031
template_ret_type, cactual_ret_type = template.ret_type, cactual.ret_type
1029-
bool_type = UnionType(
1030-
[LiteralType(True, cactual_ret_type), LiteralType(False, cactual_ret_type)] # type: ignore[arg-type]
1031-
)
10321032

10331033
if template.type_guard is not None and cactual.type_guard is not None:
10341034
template_ret_type = template.type_guard
10351035
cactual_ret_type = cactual.type_guard
10361036
elif template.type_guard is not None:
10371037
template_ret_type = AnyType(TypeOfAny.special_form)
1038-
elif cactual.type_guard is not None:
1039-
cactual_ret_type = bool_type
10401038

10411039
if template.type_is is not None and cactual.type_is is not None:
10421040
template_ret_type = template.type_is
10431041
cactual_ret_type = cactual.type_is
10441042
elif template.type_is is not None:
10451043
template_ret_type = AnyType(TypeOfAny.special_form)
1046-
elif cactual.type_is is not None:
1047-
cactual_ret_type = bool_type
10481044

10491045
res.extend(infer_constraints(template_ret_type, cactual_ret_type, self.direction))
10501046

@@ -1352,6 +1348,40 @@ def visit_type_type(self, template: TypeType) -> list[Constraint]:
13521348
else:
13531349
return []
13541350

1351+
def _make_type_info(
1352+
self,
1353+
name: str,
1354+
module_name: str | None = None,
1355+
mro: list[TypeInfo] | None = None,
1356+
bases: list[Instance] | None = None,
1357+
) -> TypeInfo:
1358+
"""Make a TypeInfo suitable for use in unit tests."""
1359+
1360+
class_def = ClassDef(name, Block([]), None, [])
1361+
class_def.fullname = name
1362+
1363+
if module_name is None:
1364+
if "." in name:
1365+
module_name = name.rsplit(".", 1)[0]
1366+
else:
1367+
module_name = "__main__"
1368+
1369+
info = TypeInfo(SymbolTable(), class_def, module_name)
1370+
if mro is None:
1371+
mro = []
1372+
if name != "builtins.object":
1373+
mro.append(self.oi)
1374+
info.mro = [info] + mro
1375+
if bases is None:
1376+
if mro:
1377+
# By default, assume that there is a single non-generic base.
1378+
bases = [Instance(mro[0], [])]
1379+
else:
1380+
bases = []
1381+
info.bases = bases
1382+
1383+
return info
1384+
13551385

13561386
def neg_op(op: int) -> int:
13571387
"""Map SubtypeOf to SupertypeOf and vice versa."""

0 commit comments

Comments
 (0)