Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise errors on unbound TypeVars with values #15732

Merged
merged 4 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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