Skip to content

Commit 14e7768

Browse files
Raise errors on unbound TypeVars with values (#15732)
Completes a `TODO` item :) Refs #15724 --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 383137b commit 14e7768

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

mypy/checker.py

+7
Original file line numberDiff line numberDiff line change
@@ -1069,6 +1069,7 @@ def check_func_def(
10691069
"""Type check a function definition."""
10701070
# Expand type variables with value restrictions to ordinary types.
10711071
expanded = self.expand_typevars(defn, typ)
1072+
original_typ = typ
10721073
for item, typ in expanded:
10731074
old_binder = self.binder
10741075
self.binder = ConditionalTypeBinder()
@@ -1126,6 +1127,12 @@ def check_func_def(
11261127
message_registry.RETURN_TYPE_CANNOT_BE_CONTRAVARIANT, typ.ret_type
11271128
)
11281129
self.check_unbound_return_typevar(typ)
1130+
elif (
1131+
isinstance(original_typ.ret_type, TypeVarType) and original_typ.ret_type.values
1132+
):
1133+
# Since type vars with values are expanded, the return type is changed
1134+
# to a raw value. This is a hack to get it back.
1135+
self.check_unbound_return_typevar(original_typ)
11291136

11301137
# Check that Generator functions have the appropriate return type.
11311138
if defn.is_generator:

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ def g() -> U: # E: A function returning TypeVar should receive at least one argu
1515

1616
V = TypeVar('V', int, str)
1717

18-
# TODO: this should also give an error
19-
def h() -> V:
18+
def h() -> V: # E: A function returning TypeVar should receive at least one argument containing the same TypeVar
2019
...
2120

2221
[case testInnerFunctionTypeVar]

test-data/unit/deps-generics.test

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ class D: pass
159159
T = TypeVar('T', A, B)
160160
S = TypeVar('S', C, D)
161161

162-
def f(x: T) -> S:
162+
def f(x: T, y: S) -> S:
163163
pass
164164
[out]
165165
<m.A> -> <m.T>, <m.f>, m, m.A, m.f

0 commit comments

Comments
 (0)