mllg / checkmate

Fast and versatile argument checks
https://mllg.github.io/checkmate/
Other
261 stars 30 forks source link

`check_vector(x)==TRUE` when `class(x)=="lm"` #196

Closed vincentarelbundock closed 4 years ago

vincentarelbundock commented 4 years ago

Thanks for the amazing package.

This behavior was very unexpected for me. Is this a bug or am I missing the logic?

mod <- lm(mpg ~ hp, mtcars)
checkmate::check_vector(mod)
#> [1] TRUE
tdeenes commented 4 years ago

Probably you are after checkmate::checkAtomicVector, or you forgot to set the strict argument of check_vector to TRUE. In R, a list is a vector, see e.g. base::is.vector(list()). So the behaviour you described is absolutely expected. (Note that what is a vector in R can be a complex issue with some inconsistencies in edge cases - but those details are not relevant here.)

tdeenes commented 4 years ago

Second note: never use class(x) == "something". Use inherits(x, "something") instead (or checkmate::testClass, checkmate::testMultiClass). See also here.

Similarly, do not use foo == TRUE. Use isTRUE(something).

vincentarelbundock commented 4 years ago

Probably you are after checkmate::checkAtomicVector, or you forgot to set the strict argument of check_vector to TRUE. In R, a list is a vector, see e.g. base::is.vector(list()).

Thanks! This was exactly the information I needed.

Second note: never use class(x) == "something". Use inherits(x, "something") instead (or checkmate::testClass, checkmate::testMultiClass). See also here.

Similarly, do not use foo == TRUE. Use isTRUE(something).

Yes, I do (usually) follow these recommendations in my code. I just used this to describe the problem concisely in the title.