Skip to content

Make TypeVars with only one constraint illegal #2626

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

Merged
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
8 changes: 6 additions & 2 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1544,7 +1544,7 @@ def process_typevar_declaration(self, s: AssignmentStmt) -> None:
res = self.process_typevar_parameters(call.args[1 + n_values:],
call.arg_names[1 + n_values:],
call.arg_kinds[1 + n_values:],
bool(values),
n_values,
s)
if res is None:
return
Expand Down Expand Up @@ -1591,8 +1591,9 @@ def get_typevar_declaration(self, s: AssignmentStmt) -> Optional[CallExpr]:
def process_typevar_parameters(self, args: List[Expression],
names: List[Optional[str]],
kinds: List[int],
has_values: bool,
num_values: int,
context: Context) -> Optional[Tuple[int, Type]]:
has_values = (num_values > 0)
covariant = False
contravariant = False
upper_bound = self.object_type() # type: Type
Expand Down Expand Up @@ -1642,6 +1643,9 @@ def process_typevar_parameters(self, args: List[Expression],
if covariant and contravariant:
self.fail("TypeVar cannot be both covariant and contravariant", context)
return None
elif num_values == 1:
self.fail("TypeVar cannot have only a single constraint", context)
return None
elif covariant:
variance = COVARIANT
elif contravariant:
Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/check-bound.test
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,12 @@ class A(A0):
class B(A):
def baz(self) -> None: pass

T = TypeVar('T', A)
T = TypeVar('T', bound=A)

def f(x: T) -> T:
x.foo()
x.bar()
x.baz() # E: "A" has no attribute "baz"
x.baz() # E: "T" has no attribute "baz"
x.a
x.b
return x
Expand Down
5 changes: 2 additions & 3 deletions test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -1458,13 +1458,12 @@ main:6: error: Signatures of "__radd__" of "B" and "__add__" of "X" are unsafely

[case testUnsafeOverlappingWithLineNo]
from typing import TypeVar
T = TypeVar('T', Real)
class Real:
def __add__(self, other): ...
class Fraction(Real):
def __radd__(self, other: T) -> T: ...
def __radd__(self, other: Real) -> Real: ...
[out]
main:6: error: Signatures of "__radd__" of "Fraction" and "__add__" of "Real" are unsafely overlapping
main:5: error: Signatures of "__radd__" of "Fraction" and "__add__" of "Real" are unsafely overlapping

[case testOverlappingNormalAndInplaceOperatorMethod]
import typing
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-typevar-values.test
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ def outer(x: T) -> T:
[case testClassMemberTypeVarInFunctionBody]
from typing import TypeVar
class C:
T = TypeVar('T', int)
T = TypeVar('T', bound=int)
def f(self, x: T) -> T:
A = C.T
return x
9 changes: 5 additions & 4 deletions test-data/unit/semanal-errors.test
Original file line number Diff line number Diff line change
Expand Up @@ -968,10 +968,11 @@ a = TypeVar() # E: Too few arguments for TypeVar()
b = TypeVar(x='b') # E: TypeVar() expects a string literal as first argument
c = TypeVar(1) # E: TypeVar() expects a string literal as first argument
d = TypeVar('D') # E: String argument 1 'D' to TypeVar(...) does not match variable name 'd'
e = TypeVar('e', int, str, x=1) # E: Unexpected argument to TypeVar(): x
f = TypeVar('f', (int, str)) # E: Type expected
g = TypeVar('g', x=(int, str)) # E: Unexpected argument to TypeVar(): x
h = TypeVar('h', bound=1) # E: TypeVar 'bound' must be a type
e = TypeVar('e', int, str, x=1) # E: Unexpected argument to TypeVar(): x
f = TypeVar('f', (int, str), int) # E: Type expected
g = TypeVar('g', int) # E: TypeVar cannot have only a single constraint
h = TypeVar('h', x=(int, str)) # E: Unexpected argument to TypeVar(): x
i = TypeVar('i', bound=1) # E: TypeVar 'bound' must be a type
[out]

[case testMoreInvalidTypevarArguments]
Expand Down