mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-03 18:28:24 +00:00
[ty] Raise invalid-exception-caught
even when exception is not captured (#18202)
Some checks are pending
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / Fuzz for new ty panics (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / check playground (push) Blocked by required conditions
CI / benchmarks (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run
Some checks are pending
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / Fuzz for new ty panics (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / check playground (push) Blocked by required conditions
CI / benchmarks (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run
Co-authored-by: Alex Waygood <alex.waygood@gmail.com> Co-authored-by: Carl Meyer <carl@astral.sh>
This commit is contained in:
parent
a2c87c2bc1
commit
8729cb208f
6 changed files with 128 additions and 23 deletions
|
@ -76,6 +76,44 @@ except Error as err:
|
|||
...
|
||||
```
|
||||
|
||||
## Exception with no captured type
|
||||
|
||||
```py
|
||||
try:
|
||||
{}.get("foo")
|
||||
except TypeError:
|
||||
pass
|
||||
```
|
||||
|
||||
## Exception which catches typevar
|
||||
|
||||
```toml
|
||||
[environment]
|
||||
python-version = "3.12"
|
||||
```
|
||||
|
||||
```py
|
||||
from typing import Callable
|
||||
|
||||
def silence[T: type[BaseException]](
|
||||
func: Callable[[], None],
|
||||
exception_type: T,
|
||||
):
|
||||
try:
|
||||
func()
|
||||
except exception_type as e:
|
||||
reveal_type(e) # revealed: T'instance
|
||||
|
||||
def silence2[T: (
|
||||
type[ValueError],
|
||||
type[TypeError],
|
||||
)](func: Callable[[], None], exception_type: T,):
|
||||
try:
|
||||
func()
|
||||
except exception_type as e:
|
||||
reveal_type(e) # revealed: T'instance
|
||||
```
|
||||
|
||||
## Invalid exception handlers
|
||||
|
||||
```py
|
||||
|
@ -108,6 +146,12 @@ def foo(
|
|||
# error: [invalid-exception-caught]
|
||||
except z as g:
|
||||
reveal_type(g) # revealed: Unknown
|
||||
|
||||
try:
|
||||
{}.get("foo")
|
||||
# error: [invalid-exception-caught]
|
||||
except int:
|
||||
pass
|
||||
```
|
||||
|
||||
## Object raised is not an exception
|
||||
|
|
|
@ -43,9 +43,23 @@ except* (KeyboardInterrupt, AttributeError) as e:
|
|||
reveal_type(e) # revealed: BaseExceptionGroup[KeyboardInterrupt | AttributeError]
|
||||
```
|
||||
|
||||
## Invalid `except*` handlers
|
||||
## `except*` with no captured exception type
|
||||
|
||||
```py
|
||||
try:
|
||||
help()
|
||||
except* TypeError:
|
||||
pass
|
||||
```
|
||||
|
||||
## Invalid `except*` handlers with or without a captured exception type
|
||||
|
||||
```py
|
||||
try:
|
||||
help()
|
||||
except* int: # error: [invalid-exception-caught]
|
||||
pass
|
||||
|
||||
try:
|
||||
help()
|
||||
except* 3 as e: # error: [invalid-exception-caught]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue