Skip to content

Commit

Permalink
Raise errors on unbound TypeVars with values (#15732)
Browse files Browse the repository at this point in the history
Completes a `TODO` item :)

Refs #15724

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
sobolevn and pre-commit-ci[bot] committed Jul 21, 2023
1 parent 383137b commit 14e7768
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
7 changes: 7 additions & 0 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1069,6 +1069,7 @@ def check_func_def(
"""Type check a function definition."""
# Expand type variables with value restrictions to ordinary types.
expanded = self.expand_typevars(defn, typ)
original_typ = typ
for item, typ in expanded:
old_binder = self.binder
self.binder = ConditionalTypeBinder()
Expand Down Expand Up @@ -1126,6 +1127,12 @@ def check_func_def(
message_registry.RETURN_TYPE_CANNOT_BE_CONTRAVARIANT, typ.ret_type
)
self.check_unbound_return_typevar(typ)
elif (
isinstance(original_typ.ret_type, TypeVarType) and original_typ.ret_type.values
):
# Since type vars with values are expanded, the return type is changed
# to a raw value. This is a hack to get it back.
self.check_unbound_return_typevar(original_typ)

# Check that Generator functions have the appropriate return type.
if defn.is_generator:
Expand Down
3 changes: 1 addition & 2 deletions test-data/unit/check-typevar-unbound.test
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ def g() -> U: # E: A function returning TypeVar should receive at least one argu

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

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

[case testInnerFunctionTypeVar]
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/deps-generics.test
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ class D: pass
T = TypeVar('T', A, B)
S = TypeVar('S', C, D)

def f(x: T) -> S:
def f(x: T, y: S) -> S:
pass
[out]
<m.A> -> <m.T>, <m.f>, m, m.A, m.f
Expand Down

0 comments on commit 14e7768

Please sign in to comment.