Skip to content
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

Implement codemod for walrus-if #40

Merged
merged 4 commits into from
Sep 21, 2023
Merged

Implement codemod for walrus-if #40

merged 4 commits into from
Sep 21, 2023

Conversation

drdavella
Copy link
Member

@drdavella drdavella commented Sep 19, 2023

Overview

Implement codemod to use walrus (:=) operator for simple cases

Description

  • This codemod makes changes of the following form:
- x = foo()
- if x is not None:
+ if (x := foo()) is not None:
    print(x)
  • It looks for simple single-variable assignments that are immediately followed by an if statement that checks the value of the assignment. Right now we are prioritizing accuracy and safety over breadth
  • Semgrep does most of the heavy lifting for detection but we also need some additional validation using libCST to avoid some false negatives
  • The codemod attempts to preserve whitespace and comments where possible
  • Future codemods will try to find other cases where the walrus can be used

@drdavella drdavella force-pushed the walrus-if branch 2 times, most recently from 4db5bf8 to da4a212 Compare September 20, 2023 15:31
@codecov-commenter
Copy link

codecov-commenter commented Sep 20, 2023

Codecov Report

Merging #40 (a91e24d) into main (5bc8065) will increase coverage by 0.09%.
The diff coverage is 98.18%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main      #40      +/-   ##
==========================================
+ Coverage   95.69%   95.78%   +0.09%     
==========================================
  Files          37       38       +1     
  Lines        1346     1399      +53     
==========================================
+ Hits         1288     1340      +52     
- Misses         58       59       +1     
Files Changed Coverage Δ
src/codemodder/codemods/use_walrus_if.py 98.00% <98.00%> (ø)
src/codemodder/codemods/__init__.py 100.00% <100.00%> (ø)
src/codemodder/codemods/api/__init__.py 95.52% <100.00%> (-0.07%) ⬇️
src/codemodder/codemods/base_visitor.py 97.14% <100.00%> (+0.26%) ⬆️

@drdavella drdavella marked this pull request as ready for review September 20, 2023 15:43
Copy link
Contributor

@andrecsilva andrecsilva left a comment

Choose a reason for hiding this comment

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

Lacking integration tests.

src/codemodder/codemods/use_walrus_if.py Show resolved Hide resolved
@drdavella drdavella merged commit e4a7835 into main Sep 21, 2023
@drdavella drdavella deleted the walrus-if branch September 21, 2023 14:16
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.

3 participants