Skip to content

Add potential sequence() lints to seq_linter() - #2618

Merged
MichaelChirico merged 17 commits into
r-lib:mainfrom
Bisaloo:sequence-linter
Mar 4, 2025
Merged

MichaelChirico merged 17 commits into
r-lib:mainfrom
Bisaloo:sequence-linter

Conversation

@Bisaloo

@Bisaloo Bisaloo commented Jun 21, 2024

Copy link
Copy Markdown
Collaborator

Fix #2595

Comment thread R/seq_linter.R Resolved
Comment thread NEWS.md Resolved
Comment thread R/seq_linter.R Resolved
Comment thread tests/testthat/test-seq_linter.R Outdated
lint_msg <- rex::rex("Use sequence()")

expect_lint(
"unlist(lapply(x, seq_len))",

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The lint now only covers unlist(lapply(x, seq_len)).

What about

  • unlist(lapply(x, seq))
  • unlist(lapply(x, seq, by = 2)), and other variants equivalent to using the from= and/or by= arguments of sequence()?
  • I don't think it's worth trying to find examples like lapply(x, \(xi) seq(xi)) which would be covered by unnecessary_lambda_linter(). But are there any examples that do require looking into a lambda to replicate a sequence()-able call?

(it would be nice to get a sense of how many hits these are getting before investing too much time -- and also feel free to earmark some of this as a TODO in a follow-up issue, though lapply(x,seq) is trivial enough to be done here)

@Bisaloo Bisaloo Jun 22, 2024 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unlist(lapply(x, seq, by = 2)), and other variants equivalent to using the from= and/or by= arguments of sequence()?

I don't think there is a direct equivalence:

  • from has a different behaviour in seq() and sequence()

    unlist(lapply(1:5, seq, from = 2))
    #>  [1] 2 1 2 2 3 2 3 4 2 3 4 5
    
    sequence(1:5, from = 2)
    #>  [1] 2 2 3 2 3 4 2 3 4 5 2 3 4 5 6

    Created on 2024-06-22 with reprex v2.1.0

  • As far as I can tell, by= cannot be used on its own without from=, which brings us back to the previous case

    seq(20, by = 2)
    #> Error in seq.default(20, by = 2): wrong sign in 'by' argument

    Created on 2024-06-22 with reprex v2.1.0

I have edited the xpath to make sure this type of call doesn't lint.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From ?sequence, I think there is another case, I just don't know if we'll ever see it in the wild :)

unlist(mapply(\(nvec, from, by) seq(from, by=by, length.out=nvec), nvces, froms, bys))

There are a few possibilities here, anyway I don't think we need to include those yet:

https://github.com/search?q=lang%3AR+%2Funlist%5B%28%5D%5B+%5Cn%5D*map%2F+%2Fseq%5B%28%5D%2F&type=code

More worthy of consideration is unlist(lapply(i, "seq")), but again can be done later.

@codecov

codecov Bot commented Jun 22, 2024 •

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.64%. Comparing base (500a38c) to head (57fac7a).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2618   +/-   ##
=======================================
  Coverage   99.63%   99.64%           
=======================================
  Files         126      126           
  Lines        6936     6953   +17     
=======================================
+ Hits         6911     6928   +17     
  Misses         25       25           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

IndrajeetPatil
IndrajeetPatil previously approved these changes Jun 22, 2024

@IndrajeetPatil IndrajeetPatil left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's also wait for MC's approval.

@MichaelChirico Can we add @Bisaloo to the repo as a contributor? Of course, only if he wishes to. 😅

@MichaelChirico

Copy link
Copy Markdown
Collaborator

@MichaelChirico Can we add @Bisaloo to the repo as a contributor? Of course, only if he wishes to. 😅

Invited!

@MichaelChirico

Copy link
Copy Markdown
Collaborator

ignoring failed check, it's a container timeout & unlikely to fail when the others have passed

@MichaelChirico
MichaelChirico merged commit cf9758a into r-lib:main Mar 4, 2025
@Bisaloo
Bisaloo deleted the sequence-linter branch July 19, 2025 09:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Extend seq_linter() to recommend sequence() base R function

3 participants