Skip to content

Commit

Permalink
Merge pull request #1607 from rstudio/format-callables-signature
Browse files Browse the repository at this point in the history
Update print method for Python callables
  • Loading branch information
t-kalinowski committed May 8, 2024
2 parents c2a21eb + 369bccd commit cf6aeda
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 21 deletions.
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

- Interrupting Python no longer leads to segfaults (#1601, fixed in #1602).

- Print method for Python callables now includes the callable’s signature (#1605)
- Print method for Python callables now includes the callable’s signature (#1605, #1607)

- `virtualenv_starter()` no longer warns when encountering broken symlinks (#1598).

Expand Down
34 changes: 14 additions & 20 deletions R/python.R
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@

#' @export
print.python.builtin.object <- function(x, ...) {
formatted <- if (py_is_callable(x))
py_format_callable(x, ...)
else
py_repr(x)
formatted <- c(
py_repr(x),
py_format_signature(x)
)

writeLines(formatted)
invisible(x)
}


py_format_callable <- function(x, ...) {

type <- py_to_r(py_get_attr(py_get_attr(x, "__class__"), "__name__"))

name <- py_to_r(py_get_attr(x, "__qualname__", TRUE) %||%
py_get_attr(x, "__name__", TRUE))

module <- py_to_r(py_get_attr(x, "__module__", TRUE))
if (!is.null(module))
name <- paste0(c(module, name), collapse = ".")
py_format_signature <- function(x, ...) {
if (!py_is_callable(x))
return(NULL)

inspect <- import("inspect")

get_formatted_signature <- function(x, drop_first = FALSE) {
tryCatch({
sig <- inspect$signature(x)
Expand All @@ -43,14 +35,15 @@ py_format_callable <- function(x, ...) {
formatted <- py_str_impl(sig)

# split long signatures across multiple lines, so they're readable
if (py_len(sig$parameters) >= 4L) {
# if (py_len(sig$parameters) > 5L) {
if (nchar(formatted) > 60) {
for (formatted_arg in iterate(sig$parameters$values(), py_str_impl))
formatted <- sub(formatted_arg,
paste0("\n ", formatted_arg),
paste0("\n ", formatted_arg),
formatted, fixed = TRUE)

formatted <- sub(", /,", ",\n /,", formatted, fixed = TRUE) # positional only separator
formatted <- sub(", *,", ",\n *,", formatted, fixed = TRUE) # kw-only separator
formatted <- sub(", /,", ",\n /,", formatted, fixed = TRUE) # positional only separator
formatted <- sub(", *,", ",\n *,", formatted, fixed = TRUE) # kw-only separator
formatted <- sub("\\)($| ->)", "\n)\\1", formatted) # final closing parens )
}
formatted
Expand All @@ -63,10 +56,11 @@ py_format_callable <- function(x, ...) {
get_formatted_signature(py_get_attr(x, "__new__", TRUE), TRUE) %||%
"(?)"

sprintf("<%s %s%s>", type, name, formatted_sig)
sprintf(" signature: %s", formatted_sig)
}



#' @importFrom utils str
#' @export
str.python.builtin.object <- function(object, ...) {
Expand Down

0 comments on commit cf6aeda

Please sign in to comment.