Skip to content

Commit

Permalink
Use lower-case generics more consistently in error messages (#17035)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
JukkaL authored Mar 15, 2024
1 parent a18a0db commit 1741c16
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
2 changes: 2 additions & 0 deletions mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -1779,6 +1779,8 @@ def need_annotation_for_var(
alias = alias.split(".")[-1]
if alias == "Dict":
type_dec = f"{type_dec}, {type_dec}"
if self.options.use_lowercase_names():
alias = alias.lower()
recommended_type = f"{alias}[{type_dec}]"
if recommended_type is not None:
hint = f' (hint: "{node.name}: {recommended_type} = ...")'
Expand Down
6 changes: 5 additions & 1 deletion mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -3405,7 +3405,11 @@ def visit_ellipsis_type(self, t: EllipsisType) -> str:
return "..."

def visit_type_type(self, t: TypeType) -> str:
return f"Type[{t.item.accept(self)}]"
if self.options.use_lowercase_names():
type_name = "type"
else:
type_name = "Type"
return f"{type_name}[{t.item.accept(self)}]"

def visit_placeholder_type(self, t: PlaceholderType) -> str:
return f"<placeholder {t.fullname}>"
Expand Down
14 changes: 14 additions & 0 deletions test-data/unit/check-lowercase.test
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,17 @@ x: type[type]
y: int

y = x # E: Incompatible types in assignment (expression has type "type[type]", variable has type "int")

[case testLowercaseSettingOnTypeAnnotationHint]
# flags: --python-version 3.9 --no-force-uppercase-builtins
x = [] # E: Need type annotation for "x" (hint: "x: list[<type>] = ...")
y = {} # E: Need type annotation for "y" (hint: "y: dict[<type>, <type>] = ...")
z = set() # E: Need type annotation for "z" (hint: "z: set[<type>] = ...")
[builtins fixtures/primitives.pyi]

[case testLowercaseSettingOnRevealTypeType]
# flags: --python-version 3.9 --no-force-uppercase-builtins
def f(t: type[int]) -> None:
reveal_type(t) # N: Revealed type is "type[builtins.int]"
reveal_type(f) # N: Revealed type is "def (t: type[builtins.int])"
[builtins fixtures/primitives.pyi]

0 comments on commit 1741c16

Please sign in to comment.