Skip to content

expect_s4_class() may fail where expect_true(is(...)) succeeds #2987

@etiennebacher

Description

@etiennebacher

expect_s4_class_linter() recommends using expect_s4_class() instead of expect_true(is(...)) but sometimes expect_s4_class() fails.

lintr::lint("expect_true(is(mtcars, 'data.frame'))\n", lintr::expect_s4_class_linter())
#> <text>:1:1: warning: [expect_s4_class_linter] expect_s4_class(x, k) is better than expect_true(is(x, k)). Note also expect_s3_class() available for testing S3 objects.
#> expect_true(is(mtcars, 'data.frame'))
#> ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

testthat::expect_true(is(mtcars, 'data.frame'))
testthat::expect_s4_class(mtcars, 'data.frame')
#> Error: Expected `mtcars` to be an S4 object.
#> Actual OO type: S3.

The second part of the message suggests also trying expect_s3_class() but the main recommendation is expect_s4_class(). Maybe the message should be tweaked or "data.frame" (and possibly other classes) should be skipped?


The example used above isn't so rare (although I don't really know the number of matches required for a pattern to be "not rare"): https://github.com/search?q=lang%3AR+%2Fexpect_true%5C%28is%5C%28%5Ba-zA-Z0-9%5D%2B%2C+%22data%5C.frame%22%5C%29%5C%29%2F&type=code

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions