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

Autofix B006 (mutable-argument-default) #4693

Closed
auscompgeek opened this issue May 28, 2023 · 1 comment · Fixed by #6131
Closed

Autofix B006 (mutable-argument-default) #4693

auscompgeek opened this issue May 28, 2023 · 1 comment · Fixed by #6131
Assignees
Labels
fixes Related to suggested fixes for violations

Comments

@auscompgeek
Copy link

B006 could have an autofixer to instead make the default None and check if it is None within the function.

pybetter includes a fixer for this:

Default values for kwargs are mutable.

As described in Common Gotchas section of "The Hitchhiker's Guide to Python", mutable arguments can be a tricky thing. This fixer replaces any default values that happen to be lists or dicts with None value, moving initialization from function definition into function body.

# BEFORE
def p(a=[]):
    print(a)

# AFTER
def p(a=None):
    if a is None:
        a = []
    
    print(a)

Be warned, that this fix may break code which intentionally uses mutable default arguments (e.g. caching).

This may want to also autofix the type hint to be Optional if there is one.

@charliermarsh charliermarsh added the fixes Related to suggested fixes for violations label May 29, 2023
@qdegraaf
Copy link
Contributor

qdegraaf commented Jun 4, 2023

I'll give this one a go

konstin added a commit that referenced this issue Aug 10, 2023
## Summary

Reopening of #4880 

One open TODO as described in:
#4880 (comment)

FYI @charliermarsh seeing as you commented you wanted to do final review
and merge. @konstin @dhruvmanila @MichaReiser as previous reviewers.

# Old Description
## Summary

Adds an autofix for B006 turning mutable argument defaults into None and
setting their original value back in the function body if still `None`
at runtime like so:
```python
def before(x=[]):
    pass
    
def after(x=None):
    if x is None:
        x = []
    pass
```

## Test Plan

Added an extra test case to existing fixture with more indentation.
Checked results for all old examples.

NOTE: Also adapted the jupyter notebook test as this checked for B006 as
well.

## Issue link

Closes: #4693

---------

Co-authored-by: konstin <[email protected]>
durumu pushed a commit to durumu/ruff that referenced this issue Aug 12, 2023
## Summary

Reopening of astral-sh#4880 

One open TODO as described in:
astral-sh#4880 (comment)

FYI @charliermarsh seeing as you commented you wanted to do final review
and merge. @konstin @dhruvmanila @MichaReiser as previous reviewers.

# Old Description
## Summary

Adds an autofix for B006 turning mutable argument defaults into None and
setting their original value back in the function body if still `None`
at runtime like so:
```python
def before(x=[]):
    pass
    
def after(x=None):
    if x is None:
        x = []
    pass
```

## Test Plan

Added an extra test case to existing fixture with more indentation.
Checked results for all old examples.

NOTE: Also adapted the jupyter notebook test as this checked for B006 as
well.

## Issue link

Closes: astral-sh#4693

---------

Co-authored-by: konstin <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixes Related to suggested fixes for violations
Projects
None yet
3 participants