forked from pylint-dev/pylint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inconsistent_returns_noreturn.py
117 lines (98 loc) · 3.22 KB
/
inconsistent_returns_noreturn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
"""Testing inconsistent returns involving typing.NoReturn annotations."""
# pylint: disable=missing-docstring, invalid-name
import sys
import typing
import typing_extensions
def parser_error(msg) -> typing.NoReturn: # pylint: disable=unused-argument
sys.exit(1)
def parser_error_nortype(msg): # pylint: disable=unused-argument
sys.exit(2)
from typing import NoReturn # pylint: disable=wrong-import-position,wrong-import-order
def parser_error_name(msg) -> NoReturn: # pylint: disable=unused-argument
sys.exit(3)
def bug_pylint_4122(s):
"""
Every returns is consistent because parser_error has type hints
indicating it never returns
"""
try:
n = int(s)
if n < 1:
raise ValueError()
return n
except ValueError:
parser_error('parser error')
def bug_pylint_4122_wrong(s): # [inconsistent-return-statements]
"""
Every returns is not consistent because parser_error_nortype has no type hints
"""
try:
n = int(s)
if n < 1:
raise ValueError()
return n
except ValueError:
parser_error_nortype('parser error')
def bug_pylint_4122_bis(s):
"""
Every returns is consistent because parser_error has type hints
indicating it never returns
"""
try:
n = int(s)
if n < 1:
raise ValueError()
return n
except ValueError:
parser_error_name('parser error')
class ClassUnderTest:
def _no_return_method(self) -> typing.NoReturn:
sys.exit(1)
def _falsely_no_return_method(self) -> typing.NoReturn:
return 1
def _does_return_method(self) -> int:
return 1
def bug_pylint_8747(self, s: str) -> int:
"""Every return is consistent because self._no_return_method hints NoReturn"""
try:
n = int(s)
if n < 1:
raise ValueError
return n
except ValueError:
self._no_return_method()
def bug_pylint_8747_wrong(self, s: str) -> int: # [inconsistent-return-statements]
"""Every return is not consistent because self._does_return_method() returns a value"""
try:
n = int(s)
if n < 1:
raise ValueError
return n
except ValueError:
self._does_return_method()
def bug_pylint_8747_incorrect_annotation(self, s: str) -> int:
"""Every return is consistent since pylint does not attempt to detect that the
NoReturn annotation is incorrect and the function actually returns
"""
try:
n = int(s)
if n < 1:
raise ValueError
return n
except ValueError:
self._falsely_no_return_method()
# https://github.com/pylint-dev/pylint/issues/7565
def never_is_handled_like_noreturn(arg: typing.Union[int, str]) -> int:
if isinstance(arg, int):
return 1
if isinstance(arg, str):
return 2
typing_extensions.assert_never(arg)
def declared_to_not_return() -> None:
return
def config_takes_precedence_over_inference(arg: typing.Union[int, str]) -> int:
if isinstance(arg, int):
return 1
if isinstance(arg, str):
return 2
declared_to_not_return()