Skip to content

False positive in extraction_operator_linter() with R6 class method calls #1485

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

Closed
IndrajeetPatil opened this issue Jul 29, 2022 · 7 comments · Fixed by #2409
Closed

False positive in extraction_operator_linter() with R6 class method calls #1485

IndrajeetPatil opened this issue Jul 29, 2022 · 7 comments · Fixed by #2409
Labels
false-positive code that shouldn't lint, but does

Comments

@IndrajeetPatil
Copy link
Collaborator

This shouldn't produce a lint:

library(lintr)

lint(
  text = "Person <- R6Class('Person', list(
          name = NULL,
          initialize = function(name) self$name <- name
          ))
  
  Bob <- Person$new('Bob')",
  linters = extraction_operator_linter()
)
#> <text>:6:16: warning: [extraction_operator_linter] Use `[[` instead of `$` to extract an element.
#>   Bob <- Person$new('Bob')
#>                ^

Created on 2022-07-29 by the reprex package (v2.0.1.9000)

And, although the suggested replacement would work here, this would be an extremely unusual way of calling R6 class methods:

library(R6)

Person <- R6Class('Person', list(
  name = NULL,
  initialize = function(name) self$name <- name
))

Person[["new"]]('Bob')
#> <Person>
#>   Public:
#>     clone: function (deep = FALSE) 
#>     initialize: function (name) 
#>     name: Bob

Created on 2022-07-29 by the reprex package (v2.0.1.9000)

@IndrajeetPatil IndrajeetPatil added the false-positive code that shouldn't lint, but does label Jul 29, 2022
@MichaelChirico
Copy link
Collaborator

IMO this linter is just fundamentally problematic -- it's why we disabled it for {lintr}:

lintr/.lintr

Line 17 in 6d2b327

extraction_operator_linter = NULL,

If it were up to me I'd deprecate this linter. But if you do use it, then yes, the awkward Person[["new"]]("Bob") is indeed the correct usage. I'm not even sure how we'd parameterize our way out of this (just allow $new?)...

@IndrajeetPatil
Copy link
Collaborator Author

Is deprecating this linter an option?! Please let's do that then.

This is problematic for R6, Shiny, and in so many other contexts. I haven't encountered a single codebase where I have run all linters and then decided to turn off extraction linter because it had produced so many false positives that attending to other, more relevant lints became a difficult task.

@AshesITR
Copy link
Collaborator

AshesITR commented Dec 2, 2023

Maybe allow all extractions that contain a call?
The utility of this linter is questionable at the moment, I agree.

I would also support deprecation if we don't find relevant usages in the wild.

@MichaelChirico
Copy link
Collaborator

cc original author @fangly for whether we're missing something / he can see a good path forward for the linter. I'll also do a Mastodon post.

@IndrajeetPatil
Copy link
Collaborator Author

FWIW, maybe codegrip will provide a convenient way to do this conversion ($ -> [[) where relevant.

@Bisaloo
Copy link
Collaborator

Bisaloo commented Dec 2, 2023

Maybe allow all extractions that contain a call?

Another common awkward case that would be handled by excluding calls:

knitr::opts_chunk$set(
  echo = TRUE
)

That said, I've disabled this linter in our organization as well because I find the [[ syntax more difficult to read in many cases, so deprecating it sounds good to me as well.

@IndrajeetPatil
Copy link
Collaborator Author

Ditto!

I have turned off this linter in all personal repos and every organization I am part of.

IndrajeetPatil added a commit that referenced this issue Dec 10, 2023
IndrajeetPatil added a commit that referenced this issue Dec 10, 2023
* Deprecate `extraction_operator_linter()`

closes #1485

* Update NEWS.md

* move it to deprecated linter file and re-document

* Update NEWS.md

Co-authored-by: Michael Chirico <michaelchirico4@gmail.com>

* Update NEWS.md

* Remove extraction_operator_linter from .lintr

---------

Co-authored-by: Michael Chirico <michaelchirico4@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
false-positive code that shouldn't lint, but does
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants