Skip to content

Commit 6274218

Browse files
authored
Suggest typing.Literal for exit-return error messages (#18541)
`typing.Literal` was added to the stdlib in Python 3.8.
1 parent 0451880 commit 6274218

File tree

3 files changed

+5
-5
lines changed

3 files changed

+5
-5
lines changed

mypy/messages.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1992,7 +1992,7 @@ def incorrect__exit__return(self, context: Context) -> None:
19921992
code=codes.EXIT_RETURN,
19931993
)
19941994
self.note(
1995-
'Use "typing_extensions.Literal[False]" as the return type or change it to "None"',
1995+
'Use "typing.Literal[False]" as the return type or change it to "None"',
19961996
context,
19971997
code=codes.EXIT_RETURN,
19981998
)

test-data/unit/check-errorcodes.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ main:2: error: Syntax error in type comment "int" [syntax]
729729
[case testErrorCode__exit__Return]
730730
class InvalidReturn:
731731
def __exit__(self, x, y, z) -> bool: # E: "bool" is invalid as return type for "__exit__" that always returns False [exit-return] \
732-
# N: Use "typing_extensions.Literal[False]" as the return type or change it to "None" \
732+
# N: Use "typing.Literal[False]" as the return type or change it to "None" \
733733
# N: If return type of "__exit__" implies that it may return True, the context manager may swallow exceptions
734734
return False
735735
[builtins fixtures/bool.pyi]

test-data/unit/check-statements.test

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,13 +1527,13 @@ from typing import Optional
15271527

15281528
class InvalidReturn1:
15291529
def __exit__(self, x, y, z) -> bool: # E: "bool" is invalid as return type for "__exit__" that always returns False \
1530-
# N: Use "typing_extensions.Literal[False]" as the return type or change it to "None" \
1530+
# N: Use "typing.Literal[False]" as the return type or change it to "None" \
15311531
# N: If return type of "__exit__" implies that it may return True, the context manager may swallow exceptions
15321532
return False
15331533

15341534
class InvalidReturn2:
15351535
def __exit__(self, x, y, z) -> Optional[bool]: # E: "bool" is invalid as return type for "__exit__" that always returns False \
1536-
# N: Use "typing_extensions.Literal[False]" as the return type or change it to "None" \
1536+
# N: Use "typing.Literal[False]" as the return type or change it to "None" \
15371537
# N: If return type of "__exit__" implies that it may return True, the context manager may swallow exceptions
15381538
if int():
15391539
return False
@@ -1542,7 +1542,7 @@ class InvalidReturn2:
15421542

15431543
class InvalidReturn3:
15441544
def __exit__(self, x, y, z) -> bool: # E: "bool" is invalid as return type for "__exit__" that always returns False \
1545-
# N: Use "typing_extensions.Literal[False]" as the return type or change it to "None" \
1545+
# N: Use "typing.Literal[False]" as the return type or change it to "None" \
15461546
# N: If return type of "__exit__" implies that it may return True, the context manager may swallow exceptions
15471547
def nested() -> bool:
15481548
return True

0 commit comments

Comments
 (0)