Skip to content

Commit 689bc95

Browse files
committed
fix actual_ret_type for TypeGuard
1 parent 796b51d commit 689bc95

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 (
@@ -1018,25 +1021,18 @@ def visit_callable_type(self, template: CallableType) -> list[Constraint]:
10181021
param_spec = template.param_spec()
10191022

10201023
template_ret_type, cactual_ret_type = template.ret_type, cactual.ret_type
1021-
bool_type = UnionType(
1022-
[LiteralType(True, cactual_ret_type), LiteralType(False, cactual_ret_type)] # type: ignore[arg-type]
1023-
)
10241024

10251025
if template.type_guard is not None and cactual.type_guard is not None:
10261026
template_ret_type = template.type_guard
10271027
cactual_ret_type = cactual.type_guard
10281028
elif template.type_guard is not None:
10291029
template_ret_type = AnyType(TypeOfAny.special_form)
1030-
elif cactual.type_guard is not None:
1031-
cactual_ret_type = bool_type
10321030

10331031
if template.type_is is not None and cactual.type_is is not None:
10341032
template_ret_type = template.type_is
10351033
cactual_ret_type = cactual.type_is
10361034
elif template.type_is is not None:
10371035
template_ret_type = AnyType(TypeOfAny.special_form)
1038-
elif cactual.type_is is not None:
1039-
cactual_ret_type = bool_type
10401036

10411037
res.extend(infer_constraints(template_ret_type, cactual_ret_type, self.direction))
10421038

@@ -1340,6 +1336,40 @@ def visit_type_type(self, template: TypeType) -> list[Constraint]:
13401336
else:
13411337
return []
13421338

1339+
def _make_type_info(
1340+
self,
1341+
name: str,
1342+
module_name: str | None = None,
1343+
mro: list[TypeInfo] | None = None,
1344+
bases: list[Instance] | None = None,
1345+
) -> TypeInfo:
1346+
"""Make a TypeInfo suitable for use in unit tests."""
1347+
1348+
class_def = ClassDef(name, Block([]), None, [])
1349+
class_def.fullname = name
1350+
1351+
if module_name is None:
1352+
if "." in name:
1353+
module_name = name.rsplit(".", 1)[0]
1354+
else:
1355+
module_name = "__main__"
1356+
1357+
info = TypeInfo(SymbolTable(), class_def, module_name)
1358+
if mro is None:
1359+
mro = []
1360+
if name != "builtins.object":
1361+
mro.append(self.oi)
1362+
info.mro = [info] + mro
1363+
if bases is None:
1364+
if mro:
1365+
# By default, assume that there is a single non-generic base.
1366+
bases = [Instance(mro[0], [])]
1367+
else:
1368+
bases = []
1369+
info.bases = bases
1370+
1371+
return info
1372+
13431373

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

0 commit comments

Comments
 (0)