{"action":"created","issue":{"url":"https://api.github.com/repos/rstudio/reticulate/issues/160","repository_url":"https://api.github.com/repos/rstudio/reticulate","labels_url":"https://api.github.com/repos/rstudio/reticulate/issues/160/labels{/name}","comments_url":"https://api.github.com/repos/rstudio/reticulate/issues/160/comments","events_url":"https://api.github.com/repos/rstudio/reticulate/issues/160/events","html_url":"https://github.com/rstudio/reticulate/issues/160","id":298710543,"number":160,"title":"Should py_to_r() copy over attributes?","user":{"login":"DavisVaughan","id":19150088,"avatar_url":"https://avatars3.githubusercontent.com/u/19150088?v=4","gravatar_id":"","url":"https://api.github.com/users/DavisVaughan","html_url":"https://github.com/DavisVaughan","followers_url":"https://api.github.com/users/DavisVaughan/followers","following_url":"https://api.github.com/users/DavisVaughan/following{/other_user}","gists_url":"https://api.github.com/users/DavisVaughan/gists{/gist_id}","starred_url":"https://api.github.com/users/DavisVaughan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/DavisVaughan/subscriptions","organizations_url":"https://api.github.com/users/DavisVaughan/orgs","repos_url":"https://api.github.com/users/DavisVaughan/repos","events_url":"https://api.github.com/users/DavisVaughan/events{/privacy}","received_events_url":"https://api.github.com/users/DavisVaughan/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":12,"created_at":"2018-02-20T19:02:00Z","updated_at":"2018-03-07T17:29:44Z","closed_at":null,"author_association":"NONE","body":"I'm playing around with the `pendulum` python library, and wanted to write a wrapper for a `pendulum` `Date` that transformed it into an R `Date`.\r\n\r\nI discovered `py_to_r()` and the underlying S3, `py_to_r_wrapper` and tried something like:\r\n\r\n``` r\r\nlibrary(reticulate)\r\nlibrary(magrittr)\r\npend <- import(\"pendulum\")\r\n\r\nd1 <- pend$Date(2013L, 01L, 01L)\r\n\r\nclass(d1)\r\n#> [1] \"pendulum.date.Date\"                       \r\n#> [2] \"pendulum.mixins.default.TranslatableMixin\"\r\n#> [3] \"pendulum.mixins.default.FormattableMixing\"\r\n#> [4] \"pendulum.mixins.default.TestableMixin\"    \r\n#> [5] \"datetime.date\"                            \r\n#> [6] \"python.builtin.object\"\r\n\r\n# Accessors\r\npend_year <- function(x) {\r\n  x$year\r\n}\r\npend_month <- function(x) {\r\n  x$month\r\n}\r\npend_day <- function(x) {\r\n  x$day\r\n}\r\n\r\n# Wrapper for conversion\r\npy_to_r_wrapper.pendulum.date.Date <- function(x) {\r\n  chr_x <- paste(pend_year(x), pend_month(x), pend_day(x), sep = \"-\")\r\n  as.Date(chr_x)\r\n}\r\n\r\n# Both of these nuke RStudio\r\n# py_to_r(d1)\r\n# pend$Date(2013L, 01L, 01L)\r\n```\r\n\r\nWhich nearly works, but RStudio crashes. The reason is because of `py_to_r()` copying over all the attributes from the original python object. It replaces the R `Date` class with all of the python classes. Commenting out the attribute copying line removes the problem. \r\n\r\n```r\r\n# py_to_r() copies over attributes from the original python object. intended?\r\npy_to_r_2 <- function (x) \r\n{\r\n  reticulate:::ensure_python_initialized()\r\n  if (!inherits(x, \"python.builtin.object\")) \r\n    stop(\"Object to convert is not a Python object\")\r\n  x <- reticulate:::py_ref_to_r(x)\r\n  wrapper <- reticulate:::py_to_r_wrapper(x)\r\n  #attributes(wrapper) <- attributes(x)\r\n  wrapper\r\n}\r\n\r\npy_to_r_2(d1)\r\n#> [1] \"2013-01-01\"\r\n```\r\n\r\nI assume this is not intended? Or am I just doing something wrong?\r\n\r\nCreated on 2018-02-20 by the [reprex package](http://reprex.tidyverse.org) (v0.1.1.9000)."},"comment":{"url":"https://api.github.com/repos/rstudio/reticulate/issues/comments/371216751","html_url":"https://github.com/rstudio/reticulate/issues/160#issuecomment-371216751","issue_url":"https://api.github.com/repos/rstudio/reticulate/issues/160","id":371216751,"user":{"login":"kevinushey","id":1976582,"avatar_url":"https://avatars1.githubusercontent.com/u/1976582?v=4","gravatar_id":"","url":"https://api.github.com/users/kevinushey","html_url":"https://github.com/kevinushey","followers_url":"https://api.github.com/users/kevinushey/followers","following_url":"https://api.github.com/users/kevinushey/following{/other_user}","gists_url":"https://api.github.com/users/kevinushey/gists{/gist_id}","starred_url":"https://api.github.com/users/kevinushey/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kevinushey/subscriptions","organizations_url":"https://api.github.com/users/kevinushey/orgs","repos_url":"https://api.github.com/users/kevinushey/repos","events_url":"https://api.github.com/users/kevinushey/events{/privacy}","received_events_url":"https://api.github.com/users/kevinushey/received_events","type":"User","site_admin":false},"created_at":"2018-03-07T17:29:44Z","updated_at":"2018-03-07T17:29:44Z","author_association":"COLLABORATOR","body":"If `feather` goes straight to `datetime64[ns]` then I think we can feel free to do that as well.\r\n\r\nI'll take a look at these today as well -- thanks for the testing!"}}
{"actor":{"display_login":"kevinushey"}}