Skip to content

Commit d03f201

Browse files
hauntsaninjailevkivskyi
authored andcommitted
Suggest using upper bound for unbound tvar (#13730)
Also don't complain about other TypeVarLikeTypes Implements #13166 (comment)
1 parent 5b17cc6 commit d03f201

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

mypy/checker.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,13 +1235,23 @@ def check_func_def(self, defn: FuncItem, typ: CallableType, name: str | None) ->
12351235

12361236
def check_unbound_return_typevar(self, typ: CallableType) -> None:
12371237
"""Fails when the return typevar is not defined in arguments."""
1238-
if typ.ret_type in typ.variables:
1239-
arg_type_visitor = CollectArgTypes()
1238+
if isinstance(typ.ret_type, TypeVarType) and typ.ret_type in typ.variables:
1239+
arg_type_visitor = CollectArgTypeVarTypes()
12401240
for argtype in typ.arg_types:
12411241
argtype.accept(arg_type_visitor)
12421242

12431243
if typ.ret_type not in arg_type_visitor.arg_types:
12441244
self.fail(message_registry.UNBOUND_TYPEVAR, typ.ret_type, code=TYPE_VAR)
1245+
upper_bound = get_proper_type(typ.ret_type.upper_bound)
1246+
if not (
1247+
isinstance(upper_bound, Instance)
1248+
and upper_bound.type.fullname == "builtins.object"
1249+
):
1250+
self.note(
1251+
"Consider using the upper bound "
1252+
f"{format_type(typ.ret_type.upper_bound)} instead",
1253+
context=typ.ret_type,
1254+
)
12451255

12461256
def check_default_args(self, item: FuncItem, body_is_trivial: bool) -> None:
12471257
for arg in item.arguments:
@@ -6156,7 +6166,7 @@ class Foo(Enum):
61566166
)
61576167

61586168

6159-
class CollectArgTypes(TypeTraverserVisitor):
6169+
class CollectArgTypeVarTypes(TypeTraverserVisitor):
61606170
"""Collects the non-nested argument types in a set."""
61616171

61626172
def __init__(self) -> None:

test-data/unit/check-classes.test

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3233,6 +3233,7 @@ def error(u_c: Type[U]) -> P: # Error here, see below
32333233
[out]
32343234
main:11: note: Revealed type is "__main__.WizUser"
32353235
main:12: error: A function returning TypeVar should receive at least one argument containing the same Typevar
3236+
main:12: note: Consider using the upper bound "ProUser" instead
32363237
main:13: error: Value of type variable "P" of "new_pro" cannot be "U"
32373238
main:13: error: Incompatible return value type (got "U", expected "P")
32383239

test-data/unit/check-typevar-unbound.test

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
1-
21
[case testUnboundTypeVar]
32
from typing import TypeVar
43

54
T = TypeVar('T')
65

76
def f() -> T: # E: A function returning TypeVar should receive at least one argument containing the same Typevar
87
...
9-
108
f()
119

10+
U = TypeVar('U', bound=int)
11+
12+
def g() -> U: # E: A function returning TypeVar should receive at least one argument containing the same Typevar \
13+
# N: Consider using the upper bound "int" instead
14+
...
15+
16+
V = TypeVar('V', int, str)
17+
18+
# TODO: this should also give an error
19+
def h() -> V:
20+
...
1221

1322
[case testInnerFunctionTypeVar]
1423

@@ -21,15 +30,13 @@ def g(a: T) -> T:
2130
...
2231
return f()
2332

24-
2533
[case testUnboundIterableOfTypeVars]
2634
from typing import Iterable, TypeVar
2735

2836
T = TypeVar('T')
2937

3038
def f() -> Iterable[T]:
3139
...
32-
3340
f()
3441

3542
[case testBoundTypeVar]
@@ -40,7 +47,6 @@ T = TypeVar('T')
4047
def f(a: T, b: T, c: int) -> T:
4148
...
4249

43-
4450
[case testNestedBoundTypeVar]
4551
from typing import Callable, List, Union, Tuple, TypeVar
4652

0 commit comments

Comments
 (0)