Skip to content

Commit c11f99c

Browse files
committed
Fix reporting location in aliased types.
1 parent 03638dd commit c11f99c

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

mypy/types.py

+14
Original file line numberDiff line numberDiff line change
@@ -2760,6 +2760,7 @@ def get_proper_types(
27602760
TypeTranslator as TypeTranslator,
27612761
TypeVisitor as TypeVisitor,
27622762
)
2763+
from mypy.typetraverser import TypeTraverserVisitor
27632764

27642765

27652766
class TypeStrVisitor(SyntheticTypeVisitor[str]):
@@ -3122,6 +3123,18 @@ def visit_type_var(self, typ: TypeVarType) -> Type:
31223123
return typ
31233124

31243125

3126+
class LocationSetter(TypeTraverserVisitor):
3127+
# TODO: Should we update locations of other Type subclasses?
3128+
def __init__(self, line: int, column: int) -> None:
3129+
self.line = line
3130+
self.column = column
3131+
3132+
def visit_instance(self, typ: Instance) -> None:
3133+
typ.line = self.line
3134+
typ.column = self.column
3135+
super().visit_instance(typ)
3136+
3137+
31253138
def replace_alias_tvars(
31263139
tp: Type, vars: List[str], subs: List[Type], newline: int, newcolumn: int
31273140
) -> Type:
@@ -3130,6 +3143,7 @@ def replace_alias_tvars(
31303143
"""
31313144
replacer = InstantiateAliasVisitor(vars, subs)
31323145
new_tp = tp.accept(replacer)
3146+
new_tp.accept(LocationSetter(newline, newcolumn))
31333147
new_tp.line = newline
31343148
new_tp.column = newcolumn
31353149
return new_tp

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)