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

RET504 invalid autofix #5909

Closed
tylerlaprade opened this issue Jul 20, 2023 · 8 comments
Closed

RET504 invalid autofix #5909

tylerlaprade opened this issue Jul 20, 2023 · 8 comments
Labels
bug Something isn't working

Comments

@tylerlaprade
Copy link
Contributor

def foo(data):
    with contextlib.suppress(UnicodeDecodeError):
        data = data.decode()
    return data

is autofixed to

def foo(data):
    with contextlib.suppress(UnicodeDecodeError):
        return data.decode()

These are not equivalent. The first one will always return data, either decoded or the original. The second will return the decoded data if there's no exception, otherwise, it will return None. This introduced a bug into my code that I only caught from some miscellaneous Pyright type-checking far away from the autofix.

@zanieb
Copy link
Member

zanieb commented Jul 20, 2023

Thanks for the report @tylerlaprade. It looks like perhaps we should not raise RET504 inside context managers?

@zanieb zanieb added the bug Something isn't working label Jul 20, 2023
@dhruvmanila
Copy link
Member

I think this is a special case as data is defined outside (before) the context manager and/or that the contextlib.suppress context manager is used here.

@zanieb
Copy link
Member

zanieb commented Jul 20, 2023

@dhruvmanila the problem is that any context manager could suppress exceptions, we can't really know. I agree that data is defined before the context is important as well though.

@charliermarsh
Copy link
Member

Oh jeez what an interesting case.

@tylerlaprade
Copy link
Contributor Author

Sourcery has been autofixing all my try-catch-pass blocks to the more concise contextlib.suppress (which I'm happy about!), but it opens up potential opportunities for this bug.

@charliermarsh
Copy link
Member

Shameless plug that Ruff can do it too 😂 https://beta.ruff.rs/docs/rules/suppressible-exception/

@tylerlaprade
Copy link
Contributor Author

Ah, I do have SIM enabled already, so I guess I have two sources of the autofixes!

charliermarsh pushed a commit that referenced this issue Jan 29, 2024
#9673)

## Summary

This review contains a fix for
[RET504](https://docs.astral.sh/ruff/rules/unnecessary-assign/)
(unnecessary-assign)

The problem is that Ruff suggests combining a return statement inside
contextlib.suppress. Even though it is an unsafe fix it might lead to an
invalid code that is not equivalent to the original one.

See: #5909

## Test Plan

```bash
cargo test
```
zanieb pushed a commit that referenced this issue Jan 29, 2024
#9673)

## Summary

This review contains a fix for
[RET504](https://docs.astral.sh/ruff/rules/unnecessary-assign/)
(unnecessary-assign)

The problem is that Ruff suggests combining a return statement inside
contextlib.suppress. Even though it is an unsafe fix it might lead to an
invalid code that is not equivalent to the original one.

See: #5909

## Test Plan

```bash
cargo test
```
@tylerlaprade
Copy link
Contributor Author

This appears to be fixed in 0.1.15 so I will close this, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants