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 to add missing self/cls #355

Merged
merged 8 commits into from
Mar 18, 2024
Merged

New codemod to add missing self/cls #355

merged 8 commits into from
Mar 18, 2024

Conversation

clavedeluna
Copy link
Contributor

@clavedeluna clavedeluna commented Mar 11, 2024

Overview

A codemod that checks if instance/class methods without any args need self/cls

Description

  • This codemod follows the sonar rule exactly, without adding any other features other than the edge cases for __new__ and __init_subclass__ sonar mentions
  • However, while working on this I thought about additional steps we could take, either in this codemod or as a separate codemod:
  1. As is, we're only checking methods/class methods with no args at all. However, what about those which have args but none are self / cls?
  2. As is, when a method has no args, we just add self. However, another valid change would be to make it a @staticmethod. For example:
def A(): 
	print("instance_method")
	# `self` is never used in the method, we could make this a staticmethod

def B(): 
	self.hi = 1
        # `self` is used, so this should have `self` as an arg

Right now we handle both cases the same: just add self. However, we could decide to handle for case A by checking if self is ever used. Could be a bit tricky. It is less risky to just add self than it is to incorrectly assume self is never used and making it a staticmethod.

Closes #297

@clavedeluna clavedeluna force-pushed the missing-self-cls branch 3 times, most recently from 87800a0 to 068c2f7 Compare March 12, 2024 11:42
@clavedeluna clavedeluna marked this pull request as ready for review March 12, 2024 11:48
Copy link
Member

@drdavella drdavella left a comment

Choose a reason for hiding this comment

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

Looks really nice overall, just a few minor comments.

To address your points:

  • I think it's okay to use the lack of @staticmethod to infer intent here, but obviously there might be a small set of cases where we'd be wrong
  • Doing this when other arguments are present would be really cool but it does seem challenging. Unfortunately since self and cls are not actually keywords it becomes pretty hard to tell what was actually intended in those cases, although probably in 99% of cases our fix would be okay.

tests/codemods/test_fix_missing_self_or_cls.py Outdated Show resolved Hide resolved
src/core_codemods/fix_missing_self_or_cls.py Outdated Show resolved Hide resolved
src/core_codemods/fix_missing_self_or_cls.py Outdated Show resolved Hide resolved
@clavedeluna
Copy link
Contributor Author

Reviewers: note the change in implementation to correctly handle nested functions.

@clavedeluna clavedeluna dismissed drdavella’s stale review March 18, 2024 11:48

addressed all requested changes from Dan

@clavedeluna clavedeluna enabled auto-merge March 18, 2024 11:48
Copy link

Quality Gate Passed Quality Gate passed

Issues
1 New issue
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

@clavedeluna clavedeluna added this pull request to the merge queue Mar 18, 2024
Merged via the queue into main with commit 4a48a20 Mar 18, 2024
12 checks passed
@clavedeluna clavedeluna deleted the missing-self-cls branch March 18, 2024 12:32
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.

Add missing self or cls parameter to method
3 participants