reticulate 1.29
Exceptions and Errors:
-
R error information (call, message, other attributes) is now
preserved as an R error condition traverses the R <-> Python boundary. -
Python Exceptions now inherit from
errorandcondition, and can be
passed directly tobase::stop()to signal an error in R and raise an
exception in Python. -
Raised Python Exceptions are now used directly to signal an R error.
For example, in the following code,eis now an object that
inherits frompython.builtin.Exceptionas well aserrorandcondition:e <- tryCatch(py_func_that_raises_exception(), error = function(e) e)
Use
base::conditionCall()andbase::conditionMessage()to access
the original R call and error message. -
py_last_error()return object containsr_call,r_traceand/or
r_classif the Python Exception was raised by an R function called
from Python. -
The hint to run
reticulate::py_last_error()after an exception
is now clickable in the RStudio IDE. -
Filepaths to Python files in the print output from
py_last_error()are
now clickable links in the RStudio IDE. -
Python exceptions encountered in
repl_python()are now printed with the
full Python traceback by default. In the RStudio IDE, filepaths in the tracebacks
are rendered as clickable links. (#1240)
Language:
-
Converted Python callables gain support for dynamic dots from the rlang package.
New features:- splicing (unpacking) arguments:
fn(!!!kwargs) - dynamic names:
nm <- "key"; fn("{nm}" := value) - trailing commas ignored (matching Python syntax):
fn(a, )identical tofn(a)
- splicing (unpacking) arguments:
-
New Ops group generics for Python objects:
+,-,*,/,^,%%,%/%,&,|,!,%*%.
Methods for all the Ops group generics are now defined for Python objects. (#1187, #1363)
E.g., this now works:np <- reticulate::import("numpy", convert = FALSE) x <- np$array(1:5) y <- np$array(6:10) x + y
-
Fixed two issues with R comparison operator methods
(==,!=,<,<=,>=,>):- The operators no longer error on Python objects that define "rich comparison"
Python methods that don't return a single bool. (e.g., numpy arrays). - The operators now respect the 'convert' value of the supplied Python objects.
Note, this may be a breaking change as, e.g,==, may now no long return
an R scalar logical if one of the Python object being compared was created
withconvert = FALSE. Wrap the result of the comparison withpy_bool()to
restore the previous behavior.
(#1187, #1363)
- The operators no longer error on Python objects that define "rich comparison"
-
R functions wrapping Python callables now have formals matching
those of the Python callable signature, enabling better
autocompletion in more contexts (#1361). -
new
nameOfClass()S3 method for Python types, enabling usage:
base::inherits(x, <python-type-object>)(requires R >= 4.3.0) -
py_run_file()andsource_python()now prepend the script directory to
the Python module search path,sys.path, while the requested script is executing.
This allows the Python scripts to resolve imports of modules defined in the
script directory, matching the behavior ofpython <script>at the command line.
(#1347)
knitr:
-
The knitr engine now suppresses warnings from Python code if
warning=FALSEis set in the chunk options. (quarto-dev/quarto#125, #1358) -
Fixed issue where reticulate's knitr engine would attach comments in a
code chunk to the wrong code chunk (requires Python>=3.8) (#1223). -
The knitr Python engine now respects the
strip.whiteoption (#1273). -
Fixed issue where the knitr engine would show an additional plot from a chunk
if the user calledmatplotlib.pyplot.show()(#1380, #1383)
Misc:
-
py_to_r()now succeeds when converting subtypes of the built-in
types (e.g.list,dict,str). (#1352, #1348, #1226, #1354, #1366) -
New
pillar::type_sum()method now exported for Python objects. That ensures
the full object class name is printing in R tracebacks and tibbles
containing Python objects. -
py_load_object()gains aconvertargument. Ifconvert = FALSE,
the returned Python object will not be converted to an R object. -
Fixed error
r_to_py()with Pandas>=2.0 and R data.frames with a
factor column containing levels withNA. -
r_to_py()now succeeds for many additional types of R objects.
Objects that reticulate doesn't know how to convert are presented to
the Python runtime as a pycapsule (an opaque pointer to the underlying
R object). Previously this would error.
This allows for R code to pass R objects that cannot be safely
converted to Python through the Python runtime to other R code.
(e.g, to an R function called by Python code). (#1304) -
reticulate gains the ability to bind to micromamba Python installations
(#1378, #1176, #1382, #1379, thanks to Zia Khan, @zia1138) -
Default Python version used by
install_miniconda()and friends
is now 3.9 (was 3.8).