Skip to content

Use more lower case builtins in error messages #19177

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
May 30, 2025
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
2 changes: 1 addition & 1 deletion mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -1784,7 +1784,7 @@ def reveal_locals(self, type_map: dict[str, Type | None], context: Context) -> N

def unsupported_type_type(self, item: Type, context: Context) -> None:
self.fail(
f'Cannot instantiate type "Type[{format_type_bare(item, self.options)}]"', context
f'Cannot instantiate type "type[{format_type_bare(item, self.options)}]"', context
)

def redundant_cast(self, typ: Type, context: Context) -> None:
Expand Down
2 changes: 1 addition & 1 deletion mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -3985,7 +3985,7 @@ def check_and_set_up_type_alias(self, s: AssignmentStmt) -> bool:
if isinstance(existing.node, TypeAlias) and not s.is_alias_def:
self.fail(
'Cannot assign multiple types to name "{}"'
' without an explicit "Type[...]" annotation'.format(lvalue.name),
' without an explicit "type[...]" annotation'.format(lvalue.name),
lvalue,
)
return False
Expand Down
3 changes: 0 additions & 3 deletions mypy/suggestions.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
SymbolTable,
TypeInfo,
Var,
reverse_builtin_aliases,
)
from mypy.options import Options
from mypy.plugin import FunctionContext, MethodContext, Plugin
Expand Down Expand Up @@ -830,8 +829,6 @@ def visit_instance(self, t: Instance) -> str:
s = t.type.fullname or t.type.name or None
if s is None:
return "<???>"
if s in reverse_builtin_aliases:
s = reverse_builtin_aliases[s]

mod_obj = split_target(self.graph, s)
assert mod_obj
Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -3688,7 +3688,7 @@ def process(cls: Type[U]):
[case testTypeUsingTypeCErrorUnsupportedType]
from typing import Type, Tuple
def foo(arg: Type[Tuple[int]]):
arg() # E: Cannot instantiate type "Type[tuple[int]]"
arg() # E: Cannot instantiate type "type[tuple[int]]"
[builtins fixtures/tuple.pyi]

[case testTypeUsingTypeCOverloadedClass]
Expand Down Expand Up @@ -3732,7 +3732,7 @@ def f(a: T): pass
[case testTypeUsingTypeCTuple]
from typing import Type, Tuple
def f(a: Type[Tuple[int, int]]):
a() # E: Cannot instantiate type "Type[tuple[int, int]]"
a() # E: Cannot instantiate type "type[tuple[int, int]]"
[builtins fixtures/tuple.pyi]

[case testTypeUsingTypeCNamedTuple]
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-generics.test
Original file line number Diff line number Diff line change
Expand Up @@ -1019,7 +1019,7 @@ class C:
if int():
a = B
if int():
b = int # E: Cannot assign multiple types to name "b" without an explicit "Type[...]" annotation
b = int # E: Cannot assign multiple types to name "b" without an explicit "type[...]" annotation
if int():
c = int
def f(self, x: a) -> None: pass # E: Variable "__main__.C.a" is not valid as a type \
Expand Down
14 changes: 7 additions & 7 deletions test-data/unit/check-type-aliases.test
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ U = Union[int, str]
[case testProhibitReassigningAliases]
A = float
if int():
A = int # E: Cannot assign multiple types to name "A" without an explicit "Type[...]" annotation
A = int # E: Cannot assign multiple types to name "A" without an explicit "type[...]" annotation
[out]

[case testProhibitReassigningSubscriptedAliases]
from typing import Callable
A = Callable[[], float]
if int():
A = Callable[[], int] \
# E: Cannot assign multiple types to name "A" without an explicit "Type[...]" annotation \
# E: Cannot assign multiple types to name "A" without an explicit "type[...]" annotation \
# E: Value of type "int" is not indexable
# the second error is because of `Callable = 0` in lib-stub/typing.pyi
[builtins fixtures/list.pyi]
Expand All @@ -93,7 +93,7 @@ T = TypeVar('T')

A = Tuple[T, T]
if int():
A = Union[T, int] # E: Cannot assign multiple types to name "A" without an explicit "Type[...]" annotation
A = Union[T, int] # E: Cannot assign multiple types to name "A" without an explicit "type[...]" annotation
[builtins fixtures/tuple.pyi]
[typing fixtures/typing-full.pyi]

Expand Down Expand Up @@ -926,12 +926,12 @@ class Child(Parent): pass
p = Parent()
c = Child()

NormalImplicit = 4 # E: Cannot assign multiple types to name "NormalImplicit" without an explicit "Type[...]" annotation \
NormalImplicit = 4 # E: Cannot assign multiple types to name "NormalImplicit" without an explicit "type[...]" annotation \
# E: Incompatible types in assignment (expression has type "int", variable has type "type[Foo]")
NormalExplicit = 4 # E: Cannot assign multiple types to name "NormalExplicit" without an explicit "Type[...]" annotation \
NormalExplicit = 4 # E: Cannot assign multiple types to name "NormalExplicit" without an explicit "type[...]" annotation \
# E: Incompatible types in assignment (expression has type "int", variable has type "type[Foo]")
SpecialImplicit = 4 # E: Cannot assign multiple types to name "SpecialImplicit" without an explicit "Type[...]" annotation
SpecialExplicit = 4 # E: Cannot assign multiple types to name "SpecialExplicit" without an explicit "Type[...]" annotation
SpecialImplicit = 4 # E: Cannot assign multiple types to name "SpecialImplicit" without an explicit "type[...]" annotation
SpecialExplicit = 4 # E: Cannot assign multiple types to name "SpecialExplicit" without an explicit "type[...]" annotation

Parent.NormalImplicit = 4 # E: Incompatible types in assignment (expression has type "int", variable has type "type[Foo]")
Parent.NormalExplicit = 4 # E: Incompatible types in assignment (expression has type "int", variable has type "type[Foo]")
Expand Down
34 changes: 17 additions & 17 deletions test-data/unit/fine-grained-suggest.test
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ def bar() -> None:
[out]
bar.py:3: (str)
bar.py:4: (arg=str)
bar.py:6: (*typing.List[str])
bar.py:8: (**typing.Dict[str, str])
bar.py:6: (*list[str])
bar.py:8: (**dict[str, str])
==

[case testSuggestCallsitesStep2]
Expand All @@ -41,8 +41,8 @@ def bar() -> None:
==
bar.py:3: (str)
bar.py:4: (arg=str)
bar.py:6: (*typing.List[str])
bar.py:8: (**typing.Dict[str, str])
bar.py:6: (*list[str])
bar.py:8: (**dict[str, str])

[case testMaxGuesses]
# suggest: foo.foo
Expand Down Expand Up @@ -691,8 +691,8 @@ No guesses that match criteria!
(int, int) -> Any
No guesses that match criteria!
==
(typing.List[Any]) -> int
(typing.List[Any]) -> int
(list[Any]) -> int
(list[Any]) -> int


[case testSuggestFlexAny2]
Expand Down Expand Up @@ -965,7 +965,7 @@ def g(): ...
z = foo(f(), g())
[builtins fixtures/isinstancelist.pyi]
[out]
(foo.List[Any], UNKNOWN) -> Tuple[foo.List[Any], Any]
(list[Any], UNKNOWN) -> Tuple[list[Any], Any]
==

[case testSuggestBadImport]
Expand Down Expand Up @@ -1007,11 +1007,11 @@ spam({'x': 5})

[builtins fixtures/dict.pyi]
[out]
() -> typing.Dict[str, int]
() -> typing.Dict[Any, Any]
() -> foo:List[typing.Dict[str, int]]
() -> foo.List[int]
(typing.Dict[str, int]) -> None
() -> dict[str, int]
() -> dict[Any, Any]
() -> list[dict[str, int]]
() -> list[int]
(dict[str, int]) -> None
==

[case testSuggestWithErrors]
Expand Down Expand Up @@ -1161,18 +1161,18 @@ tuple1(t)
[out]
(int, int) -> int
(int, int) -> int
(int) -> foo.List[int]
(foo.List[int]) -> int
(int) -> list[int]
(list[int]) -> int
(Union[int, str]) -> None
(Callable[[int], int]) -> int
(Callable[[float], int]) -> int
(Optional[int]) -> None
(Union[None, int, str]) -> None
(Optional[foo.List[int]]) -> int
(Union[foo.Set[int], foo.List[int]]) -> None
(Optional[list[int]]) -> int
(Union[set[int], list[int]]) -> None
(Optional[int]) -> None
(Optional[Any]) -> None
(foo.Dict[int, int]) -> None
(dict[int, int]) -> None
(Tuple[int, int]) -> None
==

Expand Down