Skip to content

Commit 73903e3

Browse files
committed
Fix reporting location in aliased types.
1 parent c205528 commit 73903e3

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

mypy/types.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2576,6 +2576,7 @@ def get_proper_types(it: Iterable[Optional[Type]]
25762576
TypeTranslator as TypeTranslator,
25772577
TypeQuery as TypeQuery,
25782578
)
2579+
from mypy.typetraverser import TypeTraverserVisitor
25792580

25802581

25812582
class TypeStrVisitor(SyntheticTypeVisitor[str]):
@@ -2914,13 +2915,26 @@ def visit_type_var(self, typ: TypeVarType) -> Type:
29142915
return typ
29152916

29162917

2918+
class LocationSetter(TypeTraverserVisitor):
2919+
# TODO: Should we update locations of other Type subclasses?
2920+
def __init__(self, line: int, column: int) -> None:
2921+
self.line = line
2922+
self.column = column
2923+
2924+
def visit_instance(self, typ: Instance) -> None:
2925+
typ.line = self.line
2926+
typ.column = self.column
2927+
super().visit_instance(typ)
2928+
2929+
29172930
def replace_alias_tvars(tp: Type, vars: List[str], subs: List[Type],
29182931
newline: int, newcolumn: int) -> Type:
29192932
"""Replace type variables in a generic type alias tp with substitutions subs
29202933
resetting context. Length of subs should be already checked.
29212934
"""
29222935
replacer = InstantiateAliasVisitor(vars, subs)
29232936
new_tp = tp.accept(replacer)
2937+
new_tp.accept(LocationSetter(newline, newcolumn))
29242938
new_tp.line = newline
29252939
new_tp.column = newcolumn
29262940
return new_tp

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)