Skip to content

Commit f1c41c1

Browse files
committed
Suggest using upper bound for unbound tvar
Also don't complain about other TypeVarLikeTypes Implements python#13166 (comment)
1 parent a3a5d73 commit f1c41c1

2 files changed

Lines changed: 23 additions & 8 deletions

File tree

‎mypy/checker.py‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,13 +1231,22 @@ 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+
if not (
1242+
isinstance(typ.ret_type.upper_bound, Instance)
1243+
and typ.ret_type.upper_bound.type.fullname == "builtins.object"
1244+
):
1245+
self.note(
1246+
"Consider using the upper bound "
1247+
f"{format_type(typ.ret_type.upper_bound)} instead",
1248+
context=typ.ret_type,
1249+
)
12411250

12421251
def check_default_args(self, item: FuncItem, body_is_trivial: bool) -> None:
12431252
for arg in item.arguments:
@@ -6375,7 +6384,7 @@ def has_valid_attribute(self, typ: Type, name: str) -> bool:
63756384
return not watcher.has_new_errors()
63766385

63776386

6378-
class CollectArgTypes(TypeTraverserVisitor):
6387+
class CollectArgTypeVarTypes(TypeTraverserVisitor):
63796388
"""Collects the non-nested argument types in a set."""
63806389

63816390
def __init__(self) -> None:

‎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)