You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Red-knot currently doesn't emit a diagnostic on this snippet:
raise42
But we should, since 42 is not a valid object to be used in a raise statement:
>>> raise42
Traceback (most recent call last):
File "<python-input-0>", line 1, in <module>
raise 42
TypeError: exceptions must derive from BaseException
The rule is that the object used in a raise statement must have a type that is assignable to the type BaseException | type[BaseException], since both of the following work:
>>> raiseValueError
Traceback (most recent call last):
File "<python-input-1>", line 1, in <module>
raise ValueError
ValueError
>>> raiseValueError()
Traceback (most recent call last):
File "<python-input-2>", line 1, in <module>
raise ValueError()
ValueError
We need to add some extra logic to this branch here:
The alternative might be to make the existing INVALID_EXCEPTION_CAUGHT rule broader (and rename it).
That was also my first reaction. It's kind of the same problem.
The same genre of problem, for sure. Though there's a subtle difference between the rules for raising exceptions and the rules for catching exceptions:
If you're raising an exception, the type must be assignable to BaseException | type[BaseException]
but if you're catching an exception the type must be assignable to type[BaseException] | tuple[type[BaseException], ...]
If we bundle them together into one rule, it might be harder to explain these details in the rule's documentation.
Red-knot currently doesn't emit a diagnostic on this snippet:
But we should, since
42
is not a valid object to be used in araise
statement:The rule is that the object used in a
raise
statement must have a type that is assignable to the typeBaseException | type[BaseException]
, since both of the following work:We need to add some extra logic to this branch here:
ruff/crates/red_knot_python_semantic/src/types/infer.rs
Lines 2193 to 2201 in c9fdb1f
I think this will probably need to be a new rule added to https://github.com/astral-sh/ruff/blob/main/crates/red_knot_python_semantic/src/types/diagnostic.rs ? I don't think it fits neatly into any other rule. The alternative might be to make the existing
INVALID_EXCEPTION_CAUGHT
rule broader (and rename it).The text was updated successfully, but these errors were encountered: