Skip to content

Commit 1741c16

Browse files
authored
Use lower-case generics more consistently in error messages (#17035)
Suggest `list[x]` instead of `List[x]` on Python 3.9 and later in hints. We already suggest `x | None` instead of `Optional[x]` on 3.10+, so this makes the error messages more consistent. Use lower-case `type[x]` when using `reveal_type` on Python 3.9 and later.
1 parent a18a0db commit 1741c16

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

mypy/messages.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1779,6 +1779,8 @@ def need_annotation_for_var(
17791779
alias = alias.split(".")[-1]
17801780
if alias == "Dict":
17811781
type_dec = f"{type_dec}, {type_dec}"
1782+
if self.options.use_lowercase_names():
1783+
alias = alias.lower()
17821784
recommended_type = f"{alias}[{type_dec}]"
17831785
if recommended_type is not None:
17841786
hint = f' (hint: "{node.name}: {recommended_type} = ...")'

mypy/types.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3405,7 +3405,11 @@ def visit_ellipsis_type(self, t: EllipsisType) -> str:
34053405
return "..."
34063406

34073407
def visit_type_type(self, t: TypeType) -> str:
3408-
return f"Type[{t.item.accept(self)}]"
3408+
if self.options.use_lowercase_names():
3409+
type_name = "type"
3410+
else:
3411+
type_name = "Type"
3412+
return f"{type_name}[{t.item.accept(self)}]"
34093413

34103414
def visit_placeholder_type(self, t: PlaceholderType) -> str:
34113415
return f"<placeholder {t.fullname}>"

test-data/unit/check-lowercase.test

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,17 @@ x: type[type]
4949
y: int
5050

5151
y = x # E: Incompatible types in assignment (expression has type "type[type]", variable has type "int")
52+
53+
[case testLowercaseSettingOnTypeAnnotationHint]
54+
# flags: --python-version 3.9 --no-force-uppercase-builtins
55+
x = [] # E: Need type annotation for "x" (hint: "x: list[<type>] = ...")
56+
y = {} # E: Need type annotation for "y" (hint: "y: dict[<type>, <type>] = ...")
57+
z = set() # E: Need type annotation for "z" (hint: "z: set[<type>] = ...")
58+
[builtins fixtures/primitives.pyi]
59+
60+
[case testLowercaseSettingOnRevealTypeType]
61+
# flags: --python-version 3.9 --no-force-uppercase-builtins
62+
def f(t: type[int]) -> None:
63+
reveal_type(t) # N: Revealed type is "type[builtins.int]"
64+
reveal_type(f) # N: Revealed type is "def (t: type[builtins.int])"
65+
[builtins fixtures/primitives.pyi]

0 commit comments

Comments
 (0)