Skip to content

Commit 57b2af3

Browse files
committed
Fix internal error on invalid number of type args
Fixes #996. This is a more general alternative to PR #997.
1 parent 081532d commit 57b2af3

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

mypy/test/data/check-semanal-error.test

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,10 @@ class A(X): # E: Invalid type "__main__.X"
5454
x = 1
5555
A().foo(1) # E: "A" has no attribute "foo"
5656
A().x = '' # E: Incompatible types in assignment (expression has type "str", variable has type "int")
57+
58+
[case testInvalidNumberOfTypeArgs]
59+
from typing import TypeVar
60+
T = TypeVar('T')
61+
class C: # Forgot to add type params here
62+
def __init__(self, t: T) -> None: pass
63+
c = C(t=3) # type: C[int] # E: "C" expects no type arguments, but 1 given

mypy/typeanal.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,10 @@ def visit_instance(self, t: Instance) -> None:
301301
act = 'none'
302302
self.fail('"{}" expects {}, but {} given'.format(
303303
info.name(), s, act), t)
304+
# Construct the correct number of type arguments, as
305+
# otherwise the type checker may crash as it expects
306+
# things to be right.
307+
t.args = [AnyType() for _ in info.type_vars]
304308
elif info.defn.type_vars:
305309
# Check type argument values.
306310
for arg, TypeVar in zip(t.args, info.defn.type_vars):

0 commit comments

Comments
 (0)