Commit eced426
authored
use callable protocols for pytest.skip/exit/fail/xfail instead of _WithException wrapper with __call__ attribute (#13445)
This is a more canonical way of typing generic callbacks/decorators
(see https://mypy.readthedocs.io/en/stable/protocols.html#callback-protocols)
This helps with potential issues/ambiguity with __call__ semanics in
type checkers -- according to python spec, __call__ needs to be present
on the class, rather than as an instance attribute to be considered callable.
This worked in mypy because it didn't handle the spec 100% correctly (in this case this has no negative consequences)
However, ty type checker is stricter/more correct about it and every pytest.skip usage results in:
```
error[call-non-callable]: Object of type _WithException[Unknown, <class 'Skipped'>] is not callable
```
For more context, see:
[ty] Understand classes that inherit from subscripted Protocol[] as generic astral-sh/ruff#17832 (comment)
https://discuss.python.org/t/when-should-we-assume-callable-types-are-method-descriptors/92938
Testing:
Tested with running mypy against the following snippet:
import pytest
reveal_type(pytest.skip)
reveal_type(pytest.skip.Exception)
reveal_type(pytest.skip(reason="whatever"))
Before the change:
```
$ mypy test_pytest_skip.py
test_pytest_skip.py:2: note: Revealed type is "_pytest.outcomes._WithException[def (reason: builtins.str =, *, allow_module_level: builtins.bool =) -> Never, def (msg: Union[builtins.str, None] =, pytrace: builtins.bool =, allow_module_level: builtins.bool =, *, _use_item_location: builtins.bool =) -> _pytest.outcomes.Skipped]"
test_pytest_skip.py:3: note: Revealed type is "def (msg: Union[builtins.str, None] =, pytrace: builtins.bool =, allow_module_level: builtins.bool =, *, _use_item_location: builtins.bool =) -> _pytest.outcomes.Skipped"
test_pytest_skip.py:4: note: Revealed type is "Never"
```
After the change:
```
$ mypy test_pytest_skip.py
test_pytest_skip.py:2: note: Revealed type is "_pytest.outcomes._Skip"
test_pytest_skip.py:3: note: Revealed type is "type[_pytest.outcomes.Skipped]"
test_pytest_skip.py:4: note: Revealed type is "Never"
```
ty type checker is also inferring the same types correctly now.
All types are matching and propagated correctly (most importantly, Never as a result of pytest.skip(...) call).1 parent e6f24ed commit eced426
File tree
4 files changed
+47
-53
lines changed- changelog
- src/_pytest
- testing/python
4 files changed
+47
-53
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
134 | 134 | | |
135 | 135 | | |
136 | 136 | | |
| 137 | + | |
137 | 138 | | |
138 | 139 | | |
139 | 140 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
7 | 6 | | |
8 | 7 | | |
9 | | - | |
| 8 | + | |
10 | 9 | | |
11 | | - | |
12 | | - | |
13 | 10 | | |
14 | 11 | | |
15 | 12 | | |
| |||
77 | 74 | | |
78 | 75 | | |
79 | 76 | | |
80 | | - | |
81 | | - | |
82 | | - | |
83 | | - | |
84 | | - | |
85 | | - | |
86 | | - | |
87 | | - | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | | - | |
101 | | - | |
| 77 | + | |
| 78 | + | |
102 | 79 | | |
103 | 80 | | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | | - | |
| 81 | + | |
109 | 82 | | |
110 | 83 | | |
111 | 84 | | |
112 | 85 | | |
113 | 86 | | |
114 | 87 | | |
115 | 88 | | |
116 | | - | |
| 89 | + | |
| 90 | + | |
117 | 91 | | |
118 | 92 | | |
119 | 93 | | |
120 | 94 | | |
121 | | - | |
122 | | - | |
123 | 95 | | |
| 96 | + | |
124 | 97 | | |
125 | | - | |
126 | | - | |
127 | | - | |
128 | | - | |
129 | | - | |
130 | | - | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
131 | 107 | | |
132 | 108 | | |
133 | 109 | | |
| |||
155 | 131 | | |
156 | 132 | | |
157 | 133 | | |
158 | | - | |
159 | | - | |
160 | 134 | | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
161 | 143 | | |
162 | | - | |
163 | | - | |
| 144 | + | |
| 145 | + | |
164 | 146 | | |
165 | 147 | | |
166 | 148 | | |
| |||
173 | 155 | | |
174 | 156 | | |
175 | 157 | | |
176 | | - | |
177 | | - | |
178 | 158 | | |
| 159 | + | |
179 | 160 | | |
180 | | - | |
181 | | - | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
182 | 164 | | |
183 | 165 | | |
184 | | - | |
185 | | - | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
186 | 170 | | |
187 | 171 | | |
188 | 172 | | |
| |||
201 | 185 | | |
202 | 186 | | |
203 | 187 | | |
204 | | - | |
205 | | - | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
206 | 197 | | |
207 | 198 | | |
208 | 199 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1075 | 1075 | | |
1076 | 1076 | | |
1077 | 1077 | | |
1078 | | - | |
| 1078 | + | |
| 1079 | + | |
1079 | 1080 | | |
1080 | 1081 | | |
1081 | 1082 | | |
| |||
0 commit comments