microsoft / pyright

Static Type Checker for Python
Other
13.37k stars 1.46k forks source link

False negative `raise None` #9423

Open BeefaloKing opened 7 hours ago

BeefaloKing commented 7 hours ago

Note: if you are reporting a wrong signature of a function or a class in the standard library, then the typeshed tracker is better suited for this report: https://github.com/python/typeshed/issues.

If you have a question about typing or a behavior that you’re seeing in Pyright (as opposed to a bug report or enhancement request), consider posting to the Pyright discussion forum.

Describe the bug raise None reports no errors in pyright, but python considers it a type error.

Code or Screenshots If possible, provide a minimal, self-contained code sample (surrounded by triple back ticks) to demonstrate the issue. The code should define or import all referenced symbols.

raise None
>>> raise None
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: exceptions must derive from BaseException

mypy catches this with the following error:

error: Exception must be derived from BaseException  [misc]

If your code relies on symbols that are imported from a third-party library, include the associated import statements and specify which versions of those libraries you have installed.

VS Code extension or command-line Are you running pyright as a VS Code extension, a language server in another editor, integrated into Pylance, or the command-line tool? Which version?

Running VS Code with Pylance v2024.11.1.

erictraut commented 6 hours ago

Thanks for the issue. Pyright currently has a special case for None, but this shouldn't apply to the main operand of a raise statement. By contrast, None is allowed in the optional from clause, as in raise ValueError from None.

I've updated pyright's logic to distinguish between these two cases. This will be addressed in the next release.