Skip to content

attr_getter() uses partial matching, which is error-prone/dangerous #460

@huftis

Description

@huftis

Here’s a bug/misfeature is was bitten by today. Basically, I was trying to extract the label associated with each variable in a tibble, using (bascially) map(df, attr_getter("label")).

However, for some of the variables, the value of the labels (note the s) attribute, a ‘> 1’-length vector, were extracted instead. It turned out that this happens when the label attribute is missing. The reason is partial matching on the attribute name, which unfortunately is the default in attr().

It’s to late to change the base attr() function, but partial matching is dangerous ☠️, and I see no reason that this should be the default in attr_getter() too!

To fix this, one only needs to add exact = TRUE in the call to the attr function in get_attr().

Reprex:

library(purrr)
x = c(0, 1, 1, 0)
attr(x, "labels") = c(`0` = "Female", `1` = "Male")
get_label = attr_getter("label")
get_label(x) # Expected output: NULL
#>        0        1 
#> "Female"   "Male"

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behaviorwipwork in progress

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions