-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PanicException
doesn't inherit from base Exception
#5606
Comments
I think this discussion should be upstream in pyo3. https://pyo3.rs/main/doc/pyo3/panic/struct.panicexception A panic should not be handled as something that can be caught. We should treat it as a bug in polars or a severe problem in your code we cannot recover from. |
Ok, will open an issue in However, I am not so sure that |
As per the comments in PyO3/pyo3#2783 it sounds like both polars and PyO3 authors are in agreement that panics are bugs in libraries rather than something users are expected to catch. @david-cortes perhaps you can provide examples of panics (maybe as separate issues) so that the polars team can investigate and fix them? |
Example: import numpy as np, polars as pl
(
pl.DataFrame({"col1" : np.arange(10)})
.lazy()
.select("col2")
.rename({"col2" : "col3"})
) or the example from the other thread linked here: pl.DataFrame({'x':['Some text']}) / 0 Panics happen a lot when dealing with lazy frame operations. |
Just to illustate how catching a panic exception would be good: try:
# {fast polars implementation}
except Exception as e:
logger.warning(f'Using slow implementation as a result of {e}')
df_tmp = df.to_pandad()
# {slow pandas implementation}
df = df_ans.pipe(pl.from_pandas) I often want to catch generic things (if only so I can try something else), but not keyboard interrupts |
You can catch panic exceptions specificly, if you really need that option: import numpy as np
import polars as pl
In [9]: try:
...:
...: (
...: pl.DataFrame({"col1" : np.arange(10)})
...: .lazy()
...: .select("col2")
...: .rename({"col2" : "col3"})
...: )
...: except pl.PanicException as pe:
...: print("Catched PanicException:", pe)
...:
thread '<unnamed>' panicked at 'called `Result::unwrap()` on an `Err` value: NotFound(Owned("col2"))', /home/luna.kuleuven.be/u0079808/software/polars/polars/polars-lazy/src/frame/mod.rs:418:40
Catched PanicException: called `Result::unwrap()` on an `Err` value: NotFound(Owned("col2")) |
I will close this as I think we have explained the situation. |
Polars version checks
I have checked that this issue has not already been reported.
I have confirmed this bug exists on the latest version of Polars.
Issue description
polars
tends to throwPanicException
for user-caused errors, such as supplying invalid column names or similar. This exception however doesn't inherit from Python's baseException
, which makes it problematic to catch errors.Reproducible example
Expected behavior
Should inherit from
Exception
, asBaseException
is reserved for special errors likeKeyboardInterrupt
and one usually wants to catch things derived fromException
but not fromBaseException
.Installed versions
The text was updated successfully, but these errors were encountered: