Skip to content
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

Raising python errors from Julia functions #357

Closed
lassepe opened this issue Sep 15, 2023 · 2 comments
Closed

Raising python errors from Julia functions #357

lassepe opened this issue Sep 15, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@lassepe
Copy link
Contributor

lassepe commented Sep 15, 2023

Some python package apis require the user the raise specific errors in certain scenarios. For example, Optuna requires the user to conditionally TrialPruned() error. In the current implementation of PythonCall, raising such errors from julia functions does not seem to be possible.

@pyeval `raise optuna.TrialPruned()`

fails with Python: SyntaxError: invalid syntax.

It would be handy to have a function like pyraise to throw such errors manually.

@lassepe lassepe added the enhancement New feature or request label Sep 15, 2023
@cjdoris
Copy link
Collaborator

cjdoris commented Sep 16, 2023

You want @pyexec (for executing arbitrary python code) not @pyeval (which evaluates a single expression):

julia> using PythonCall

julia> @pyeval `raise ValueError("hello")`
ERROR: Python: SyntaxError: invalid syntax (REPL[2]:1, line 1)

julia> @pyexec `raise ValueError("hello")`
ERROR: Python: ValueError: hello

@lassepe
Copy link
Contributor Author

lassepe commented Sep 17, 2023

Nice, this works like charm! Thank you :)

@lassepe lassepe closed this as completed Sep 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants