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

New codemod: use-generator #135

Merged
merged 2 commits into from
Nov 16, 2023
Merged

New codemod: use-generator #135

merged 2 commits into from
Nov 16, 2023

Conversation

drdavella
Copy link
Member

@drdavella drdavella commented Nov 15, 2023

Overview

Implement new codemod: use-generator

Description

  • This was inspired by a pylint rule
  • It is implemented purely in terms of libcst constructs
  • I would like to add some kind of scope check that ensures the functions being checked are actually builtin

Copy link

codecov bot commented Nov 15, 2023

Codecov Report

Merging #135 (97febf7) into main (98dc6c8) will increase coverage by 0.03%.
Report is 1 commits behind head on main.
The diff coverage is 100.00%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #135      +/-   ##
==========================================
+ Coverage   96.29%   96.32%   +0.03%     
==========================================
  Files          64       65       +1     
  Lines        2697     2721      +24     
==========================================
+ Hits         2597     2621      +24     
  Misses        100      100              
Files Coverage Δ
src/codemodder/codemods/api/__init__.py 95.31% <ø> (ø)
src/codemodder/codemods/utils_mixin.py 92.85% <100.00%> (+0.33%) ⬆️
src/core_codemods/__init__.py 100.00% <100.00%> (ø)
src/core_codemods/use_generator.py 100.00% <100.00%> (ø)

@drdavella drdavella marked this pull request as ready for review November 15, 2023 22:17
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.

Looks pretty good, just needs the builtin check.

@@ -0,0 +1,13 @@
Imagine that someone handed you a pile of 100 apples and then asked you to count how many of them were green without putting any of them down. You'd probably find this quite challenging and you'd struggle to hold the pile of apples at all. Now imagine someone handed you the apples one at a time and asked you to just count the green ones. This would be a much easier task.
Copy link
Contributor

Choose a reason for hiding this comment

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

Mostly a nitpick, but this is the kind of language I'd avoid in documentations. Be direct and on point.

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe this is a little too whimsical but it's important to remember that a big part of our vision here involves educating developers and security professionals on best practices. This means we actually need to make our content as engaging as possible. I'm not necessarily saying I've succeeded in that here, but I'm going to leave it as-is and see what feedback we receive.

match original_node.func:
# NOTE: could also support things like `list` and `tuple`
# but it's a less compelling use case
case cst.Name("any" | "all" | "sum" | "min" | "max"):
Copy link
Contributor

Choose a reason for hiding this comment

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

As you have pointed it out, those could use an extra step to check if they are builtin functions. You can do this using the ScopeProvider. Query the metadata for assignments for this Name node and check if it is a BuiltinAssignment.

"""
maybe_assignment = self.find_single_assignment(node)
if maybe_assignment and isinstance(maybe_assignment, BuiltinAssignment):
return matchers.matches(node.func, matchers.Name())
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm 90% sure this is redundant, as in any function that that matches the if predicate will have node.func as Name().

Copy link
Member Author

Choose a reason for hiding this comment

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

That's fair. I'll leave it for now but we could revisit.

@drdavella drdavella added this pull request to the merge queue Nov 16, 2023
Merged via the queue into main with commit 23dd9ab Nov 16, 2023
13 checks passed
@drdavella drdavella deleted the use-a-generator branch November 16, 2023 14:33
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.

2 participants