Skip to content

Commit 6a4c2c8

Browse files
authored
Suggest using upper bound for unbound tvar (#13730)
Also don't complain about other TypeVarLikeTypes Implements #13166 (comment)
1 parent f56a071 commit 6a4c2c8

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
@@ -1231,13 +1231,23 @@ def check_func_def(self, defn: FuncItem, typ: CallableType, name: str | None) ->
12311231

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

12391239
if typ.ret_type not in arg_type_visitor.arg_types:
12401240
self.fail(message_registry.UNBOUND_TYPEVAR, typ.ret_type, code=TYPE_VAR)
1241+
upper_bound = get_proper_type(typ.ret_type.upper_bound)
1242+
if not (
1243+
isinstance(upper_bound, Instance)
1244+
and upper_bound.type.fullname == "builtins.object"
1245+
):
1246+
self.note(
1247+
"Consider using the upper bound "
1248+
f"{format_type(typ.ret_type.upper_bound)} instead",
1249+
context=typ.ret_type,
1250+
)
12411251

12421252
def check_default_args(self, item: FuncItem, body_is_trivial: bool) -> None:
12431253
for arg in item.arguments:
@@ -6375,7 +6385,7 @@ def has_valid_attribute(self, typ: Type, name: str) -> bool:
63756385
return not watcher.has_new_errors()
63766386

63776387

6378-
class CollectArgTypes(TypeTraverserVisitor):
6388+
class CollectArgTypeVarTypes(TypeTraverserVisitor):
63796389
"""Collects the non-nested argument types in a set."""
63806390

63816391
def __init__(self) -> None:

test-data/unit/check-classes.test

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

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)