Skip to content

Exception for setter calls in implicit_assignment_linter? #2267

Open

Description

@MichaelChirico

Should we except the following (perhaps optionally)?

expect_error(
  MySetter(obj) <- foo(bar),
  "Expected error"
)

This is a common class of linted usage I'm coming across that seems inappropriate to introduce {.

Similar is setter methods for [:

expect_error(
  MyCustomClass[invalid] <- invalid,
  "Expected error"
)

Activity

  1. added
    feature
    on Nov 9, 2023
  2. AshesITR commented on Nov 10, 2023

    @AshesITR
    Collaborator

    Also for [[<-, $<- and @<-?

  3. olivroy commented on Jun 20, 2024

    @olivroy
    Collaborator

    I think this would be great not to lint these expect_*() calls as this is the recommended (and only?) way to test for message + output in testthat 3. (the alternative is expect_snapshot()

    expect_message(out <- f_message(x))
    expect_equal(out, 1)

    expect_snapshot() seems like overkill in this situation?

    https://www.tidyverse.org/blog/2021/10/testthat-3-1/#breaking-changes

  4. MichaelChirico commented on Jun 20, 2024

    @MichaelChirico
    CollaboratorAuthor

    I thought

    expect_message(
      expect_equal(out, 1)
    )

    (or with pipes) is the recommendation

  5. olivroy commented on Jun 20, 2024

    @olivroy
    Collaborator

    Yeah, I finally found the documentation I was looking for in the testthat 3.1 blog post

    expect_equal(expect_warning(f(), "warning"), "value")
    expect_warning(expect_equal(f(), "value"), "warning")
    # Or use the pipe:
    f() %>% 
      expect_equal("value") %>% 
      expect_warning("warning")
    # intermediate object
    expect_warning(value <- f(), "warning")
    expect_equal(value, "value")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions