Skip to content

Commit ed1ddef

Browse files
ilevkivskyigvanrossum
authored andcommitted
Fix the problem with ignored potential type alias (#2419)
Fixes #2416. This is a minimalistic fix (basically just restoring status quo for invalid aliases).
1 parent 4f7dbfc commit ed1ddef

File tree

4 files changed

+12
-0
lines changed

4 files changed

+12
-0
lines changed

mypy/checkexpr.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,6 +1378,9 @@ def visit_type_application(self, tapp: TypeApplication) -> Type:
13781378

13791379
def visit_type_alias_expr(self, alias: TypeAliasExpr) -> Type:
13801380
"""Get type of a type alias (could be generic) in a runtime expression."""
1381+
if isinstance(alias.type, Instance) and alias.type.invalid:
1382+
# An invalid alias, error already has been reported
1383+
return AnyType()
13811384
item = alias.type
13821385
if not alias.in_runtime:
13831386
# We don't replace TypeVar's with Any for alias used as Alias[T](42).

mypy/typeanal.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ def visit_instance(self, t: Instance) -> None:
416416
# otherwise the type checker may crash as it expects
417417
# things to be right.
418418
t.args = [AnyType() for _ in info.type_vars]
419+
t.invalid = True
419420
elif info.defn.type_vars:
420421
# Check type argument values.
421422
for (i, arg), TypeVar in zip(enumerate(t.args), info.defn.type_vars):

mypy/types.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ class Instance(Type):
416416
type = None # type: mypy.nodes.TypeInfo
417417
args = None # type: List[Type]
418418
erased = False # True if result of type variable substitution
419+
invalid = False # True if recovered after incorrect number of type arguments error
419420

420421
def __init__(self, typ: mypy.nodes.TypeInfo, args: List[Type],
421422
line: int = -1, column: int = -1, erased: bool = False) -> None:

test-data/unit/check-generics.test

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -970,6 +970,13 @@ main:9:7: error: Type argument 1 of "A" has incompatible value "str"
970970
main:13: error: Type argument 1 of "A" has incompatible value "str"
971971
main:13: error: Type argument 2 of "A" has incompatible value "str"
972972

973+
[case testGenericTypeAliasesIgnoredPotentialAlias]
974+
class A: ...
975+
Bad = A[int] # type: ignore
976+
977+
reveal_type(Bad) # E: Revealed type is 'Any'
978+
[out]
979+
973980

974981
-- Multiple assignment with lists
975982
-- ------------------------------

0 commit comments

Comments
 (0)