Skip to content

Commit f6e06a1

Browse files
committed
Fix reporting location in aliased types.
1 parent 4f07c79 commit f6e06a1

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

mypy/types.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2920,16 +2920,23 @@ def visit_type_var(self, typ: TypeVarType) -> Type:
29202920
return typ
29212921

29222922

2923+
def replace_alias_location(tp: Type, newline: int, newcolumn: int) -> Type:
2924+
"""Set line and column attributes in a generic type and arguments recursively.
2925+
"""
2926+
tp.line = newline
2927+
tp.column = newcolumn
2928+
for ta in getattr(tp, 'args', []):
2929+
replace_alias_location(ta, newline, newcolumn)
2930+
return tp
2931+
2932+
29232933
def replace_alias_tvars(tp: Type, vars: List[str], subs: List[Type],
29242934
newline: int, newcolumn: int) -> Type:
29252935
"""Replace type variables in a generic type alias tp with substitutions subs
29262936
resetting context. Length of subs should be already checked.
29272937
"""
29282938
replacer = InstantiateAliasVisitor(vars, subs)
2929-
new_tp = tp.accept(replacer)
2930-
new_tp.line = newline
2931-
new_tp.column = newcolumn
2932-
return new_tp
2939+
return replace_alias_location(tp.accept(replacer), newline, newcolumn)
29332940

29342941

29352942
class HasTypeVars(TypeQuery[bool]):

test-data/unit/check-generics.test

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,30 @@ reveal_type(f3()) # N: Revealed type is "Union[builtins.int, __main__.Node[built
648648

649649
[builtins fixtures/list.pyi]
650650

651+
[case testGenericTypeAliasesWithNestedArgs]
652+
# flags: --pretty --show-error-codes
653+
import other
654+
a: other.Array[float]
655+
reveal_type(a) # N: Revealed type is "other.array[Any, other.dtype[builtins.float]]"
656+
657+
[out]
658+
main:3: error: Type argument "float" of "dtype" must be a subtype of "generic" [type-var]
659+
a: other.Array[float]
660+
^
661+
[file other.py]
662+
from typing import Any, Generic, TypeVar
663+
664+
DT = TypeVar("DT", covariant=True, bound=dtype[Any])
665+
DTS = TypeVar("DTS", covariant=True, bound=generic)
666+
S = TypeVar("S", bound=Any)
667+
ST = TypeVar("ST", bound=generic, covariant=True)
668+
669+
class common: pass
670+
class generic(common): pass
671+
class dtype(Generic[DTS]): pass
672+
class array(common, Generic[S, DT]): pass
673+
Array = array[Any, dtype[ST]]
674+
651675
[case testGenericTypeAliasesAny]
652676
from typing import TypeVar, Generic
653677
T = TypeVar('T')

0 commit comments

Comments
 (0)