constructive icon indicating copy to clipboard operation
constructive copied to clipboard

Support class "python.builtin.module" with "import" constructor

Open moodymudskipper opened this issue 7 months ago • 0 comments

This is similar to #527

"python.builtin.module" are also "python.builtin.objects" but they don't have R equivalents such as integer vectors or lists. We can however construct then with import(). No exported function from reticulate gives us exactly the name of the module but py_str() is pretty close.

library(reticulate)
use_virtualenv("r-reticulate")
sentence_transformers <- import("sentence_transformers")
sentence_transformers
#> Module(sentence_transformers)
class(sentence_transformers)
#> [1] "python.builtin.module" "python.builtin.object"
py_str(sentence_transformers)
#> [1] "Module(sentence_transformers)"

General steps :

  • Define a template using .cstr_new_class(c("python.builtin.module", "python.builtin.object"), constructor = "import", commented = TRUE) that we save in the R folder as s3-python.builtin.module.R
  • If we document, reload and retry construct(x) should already return something different.
  • Adapt .cstr_construct.python.builtin.module.import() so construct(x) returns import(...), look at other examples in the package, here we can use idiomatic_class = class(x) to avoid class repair
  • document and snapshot tests

moodymudskipper avatar Mar 14 '25 17:03 moodymudskipper