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

Improve error message + tweak function signature respect error_arg for allow_rename = FALSE #358

Closed
wants to merge 6 commits into from

Conversation

olivroy
Copy link
Contributor

@olivroy olivroy commented Oct 23, 2024

I split the PR up

No rush, but I wondered if you had a date in mind for a cran release?

@olivroy
Copy link
Contributor Author

olivroy commented Oct 23, 2024

With 9938eb5, I may have a solution for #327, #336.

I will test it with dplyr / tidyr to see if it works well.

@olivroy olivroy changed the title Improve error message + tweak function signature Improve error message + tweak function signature respect error_arg for allow_rename = FALSE Oct 23, 2024
… easier to remove if they are judged as not useful for some tests, with a cleaner diff (i.e. no comma to remove)
@DavisVaughan
Copy link
Member

DavisVaughan commented Oct 23, 2024

Would it be possible to make this into multiple much smaller PRs that do exactly 1 thing?
https://code-review.tidyverse.org/author/focused.html

It is much easier for us to confidently review a PR when it does 1 thing. In particular this is 3-4 PRs:

  • The i = "{.arg {error_arg}} can't be renamed." error bullet
  • Updating to use_standalone() helpers, and updating type checker usage
  • Modernizing snapshot tests
  • Changing a function signature

These each deserve their own PR where we can have detailed but focused conversation about each idea! I do love the enthusiasm though, and we really appreciate these contributions, they look great!

Comment on lines +113 to +127
if (is.null(error_arg)) {
cli::cli_abort(
"Can't rename variables in this context.",
class = "tidyselect:::error_disallowed_rename",
call = call
)
} else {
cli::cli_abort(c(
"Can't rename variables in this context.",
i = "{.arg {error_arg}} can't be renamed."
),
class = "tidyselect:::error_disallowed_rename",
call = call
)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In #336 I think I was hoping that we could somehow mention the expression where the rename happens in the error message.

ensure_named() is only called by vars_select_eval() and that does have the expr. So you could pass expr through to ensure_named() and use as_label(expr) on it to get something nice to include in the error message. Like

      expr <- as_label(expr)
      cli::cli_abort(c(
        "Can't rename variables during selection.",
        i = "Rename detected in {.arg {error_arg}}, with expression {.code {expr}}."
        ),
        class = "tidyselect:::error_disallowed_rename",
        call = call
      )

For something like c(all_of(x), y) if the rename happens in the all_of(x), it would still report the full c(all_of(x), y) as the place where the rename happened, but I think that is ok, it is hard to be more precise. I still think this would be a big improvement.


I think adding the argument name is nice, but maybe not quite as good as it could be on its own. For example this dplyr error from tidyverse/dplyr#6745 would have been:

#> Error in `rename_with()`:
#> ! Can't rename variables in this context.
#> i `.cols` can't be renamed.

But the user typed in mtcars %>% rename_with(toupper, all_of(v)) so .cols isn't immediately meaningful to them. But this:

#> Error in `rename_with()`:
#> ! Can't rename variables in this context.
#> i Rename detected in `.cols`, with expression `all_of(v)`.

is probably enough for them to figure out where to look next, because it refers to something in their code

WDYT @lionel- ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could use the "In argument" pattern for consistency.

#> Error in `rename_with()`:
#> ! Can't rename variables in this tidyselect context.
#> i In argument: `.cols = all_of(v)`.

@lionel-
Copy link
Member

lionel- commented Oct 24, 2024

Closing this since it was split into new PRs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants