Commit 62a971c
authored
Emit error for "raise NotImplemented" (#17890)
Refs #5710
Adds special-case handling for raising `NotImplemented`:
```python
raise NotImplemented # E: Exception must be derived from BaseException; did you mean "NotImplementedError"?
```
Per the linked issue, there's some debate as to how to best handle
`NotImplemented`. This PR special-cases its behavior in `raise`
statements, whereas the leaning in the issue (at the time it was opened)
was towards updating its definition in typeshed and possibly
special-casing its use in the relevant dunder methods.
Going the typeshed/special-dunder route may still happen, but it hasn't
in the six years since the issue was opened. It would be nice to at
least catch errors from raising `NotImplemented` in the interim.
Making this change also uncovered a regression introduced in
python/typeshed#4222. Previously, `NotImplemented` was annotated as
`Any` in typeshed, so returning `NotImplemented` from a non-dunder would
emit an error when `--warn-return-any` was used:
```python
class A:
def some(self) -> bool: return NotImplemented # E: Returning Any from function declared to return "bool"
```
However, in python/typeshed#4222, the type of `NotImplemented` was
updated to be a subclass of `Any`. This broke the handling of
`--warn-return-any`, but it wasn't caught since the definition of
`NotImplemented` in `fixtures/notimplemented.pyi` wasn't updated along
with the definition in typeshed. As a result, current mypy doesn't emit
an error here
([playground](https://mypy-play.net/?mypy=1.11.2&python=3.12&flags=strict%2Cwarn-return-any&gist=8a78e3eb68b0b738f73fdd326f0bfca1)),
despite having a test case saying that it should. As a bandaid, this PR
add special handling for `NotImplemented` in return statements to treat
it like an explicit `Any`, restoring the pre- python/typeshed#4222
behavior.1 parent a1fa1c4 commit 62a971c
3 files changed
Lines changed: 29 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4581 | 4581 | | |
4582 | 4582 | | |
4583 | 4583 | | |
| 4584 | + | |
| 4585 | + | |
| 4586 | + | |
| 4587 | + | |
| 4588 | + | |
| 4589 | + | |
| 4590 | + | |
4584 | 4591 | | |
4585 | 4592 | | |
4586 | 4593 | | |
| |||
4746 | 4753 | | |
4747 | 4754 | | |
4748 | 4755 | | |
| 4756 | + | |
| 4757 | + | |
| 4758 | + | |
| 4759 | + | |
| 4760 | + | |
| 4761 | + | |
| 4762 | + | |
| 4763 | + | |
4749 | 4764 | | |
4750 | 4765 | | |
4751 | 4766 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
519 | 519 | | |
520 | 520 | | |
521 | 521 | | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
522 | 529 | | |
523 | 530 | | |
524 | 531 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
3 | | - | |
| 2 | + | |
4 | 3 | | |
5 | 4 | | |
6 | 5 | | |
| |||
10 | 9 | | |
11 | 10 | | |
12 | 11 | | |
13 | | - | |
14 | 12 | | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
0 commit comments