Skip to content

Fix #996 #997

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

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion mypy/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def visit_instance(self, template: Instance) -> List[Constraint]:
if (self.direction == SUBTYPE_OF and
template.type.has_base(instance.type.fullname())):
mapped = map_instance_to_supertype(template, instance.type)
for i in range(len(instance.args)):
for i in range(min(len(mapped.args), len(instance.args))):
# The constraints for generic type parameters are
# invariant. Include the default constraint and its
# negation to achieve the effect.
Expand Down
18 changes: 18 additions & 0 deletions mypy/test/data/semanal-errors.test
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,24 @@ y = 0 # type: A[A]
main:5: error: "B" expects 1 type argument, but 2 given
main:6: error: "A" expects no type arguments, but 1 given

[case testInvalidNumberOfGenericArgsWithExplicitArgument]
from typing import TypeVar, Generic
t = TypeVar('t')
class A(object):
def __init__(self, a: t) -> None:
self.a = a
x = A(a=1) # type: A[None]
[out]
main:6: error: "A" expects no type arguments, but 1 given

[case testInvalidNumberOfGenericArgsInInstantiatedType]
from typing import TypeVar, Generic
t = TypeVar('t')
class A(Generic[t]): pass
x = A() # type: X[None, None]
[out]
main:4: error: "A" expects 1 type argument, but 2 given

[case testInvalidNumberOfGenericArgsInUndefinedArg]

class A: pass
Expand Down