Skip to content

Fix reporting location in aliased types. #12745

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2760,6 +2760,7 @@ def get_proper_types(
TypeTranslator as TypeTranslator,
TypeVisitor as TypeVisitor,
)
from mypy.typetraverser import TypeTraverserVisitor


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


class LocationSetter(TypeTraverserVisitor):
# TODO: Should we update locations of other Type subclasses?
def __init__(self, line: int, column: int) -> None:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a TODO comment along the lines of "Should we update locations of other Type subclasses?".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

self.line = line
self.column = column

def visit_instance(self, typ: Instance) -> None:
typ.line = self.line
typ.column = self.column
super().visit_instance(typ)


def replace_alias_tvars(
tp: Type, vars: List[str], subs: List[Type], newline: int, newcolumn: int
) -> Type:
Expand All @@ -3130,6 +3143,7 @@ def replace_alias_tvars(
"""
replacer = InstantiateAliasVisitor(vars, subs)
new_tp = tp.accept(replacer)
new_tp.accept(LocationSetter(newline, newcolumn))
new_tp.line = newline
new_tp.column = newcolumn
return new_tp
Expand Down
24 changes: 24 additions & 0 deletions test-data/unit/check-generics.test
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,30 @@ reveal_type(f3()) # N: Revealed type is "Union[builtins.int, __main__.Node[built

[builtins fixtures/list.pyi]

[case testGenericTypeAliasesWithNestedArgs]
# flags: --pretty --show-error-codes
import other
a: other.Array[float]
reveal_type(a) # N: Revealed type is "other.array[Any, other.dtype[builtins.float]]"

[out]
main:3: error: Type argument "float" of "dtype" must be a subtype of "generic" [type-var]
a: other.Array[float]
^
[file other.py]
from typing import Any, Generic, TypeVar

DT = TypeVar("DT", covariant=True, bound=dtype[Any])
DTS = TypeVar("DTS", covariant=True, bound=generic)
S = TypeVar("S", bound=Any)
ST = TypeVar("ST", bound=generic, covariant=True)

class common: pass
class generic(common): pass
class dtype(Generic[DTS]): pass
class array(common, Generic[S, DT]): pass
Array = array[Any, dtype[ST]]

[case testGenericTypeAliasesAny]
from typing import TypeVar, Generic
T = TypeVar('T')
Expand Down