Skip to content

Commit 63297a1

Browse files
committed
Fix reporting location in aliased types.
1 parent 8faf44a commit 63297a1

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

mypy/types.py

+15-4
Original file line numberDiff line numberDiff line change
@@ -2577,6 +2577,7 @@ def get_proper_types(it: Iterable[Optional[Type]]
25772577
TypeTranslator as TypeTranslator,
25782578
TypeQuery as TypeQuery,
25792579
)
2580+
from mypy.typetraverser import TypeTraverserVisitor
25802581

25812582

25822583
class TypeStrVisitor(SyntheticTypeVisitor[str]):
@@ -2925,16 +2926,26 @@ def visit_type_var(self, typ: TypeVarType) -> Type:
29252926
return typ
29262927

29272928

2929+
class LocationSetter(TypeTraverserVisitor):
2930+
def __init__(self, line: int, column: int) -> None:
2931+
self.line = line
2932+
self.column = column
2933+
2934+
def visit_instance(self, typ: Instance) -> None:
2935+
typ.line = self.line
2936+
typ.column = self.column
2937+
super().visit_instance(typ)
2938+
2939+
29282940
def replace_alias_tvars(tp: Type, vars: List[str], subs: List[Type],
29292941
newline: int, newcolumn: int) -> Type:
29302942
"""Replace type variables in a generic type alias tp with substitutions subs
29312943
resetting context. Length of subs should be already checked.
29322944
"""
29332945
replacer = InstantiateAliasVisitor(vars, subs)
2934-
new_tp = tp.accept(replacer)
2935-
new_tp.line = newline
2936-
new_tp.column = newcolumn
2937-
return new_tp
2946+
newtp = tp.accept(replacer)
2947+
newtp.accept(LocationSetter(newline, newcolumn))
2948+
return newtp
29382949

29392950

29402951
class HasTypeVars(TypeQuery[bool]):

test-data/unit/check-generics.test

+24
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)