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

UP009 fix changes a file from UTF-8 to a different declared encoding #14704

Closed
dscorbett opened this issue Dec 1, 2024 · 2 comments · Fixed by #14728
Closed

UP009 fix changes a file from UTF-8 to a different declared encoding #14704

dscorbett opened this issue Dec 1, 2024 · 2 comments · Fixed by #14728
Labels
bug Something isn't working help wanted Contributions especially welcome rule Implementing or modifying a lint rule

Comments

@dscorbett
Copy link

The fix for utf8-encoding-declaration (UP009) in Ruff 0.8.1 changes the file’s declared encoding when the redundant UTF-8 encoding declaration is followed by a non-UTF-8 encoding declaration. In that case, the UTF-8 declaration is not completely redundant, because it blocks the following declaration from having an effect. The fix should insert up to 2 blank lines so the other declarations have no effect, or the fix should delete the other declarations, or the check should not report a violation.

Example of a syntax error:

$ printf '\xef\xbb\xbf# coding: utf-8\n# coding: ascii\nprint("success")\n' >up009_1.py
$ python up009_1.py
success
$ ruff check --isolated --select UP009 up009_1.py --fix
Found 1 error (1 fixed, 0 remaining).
$ python up009_1.py
SyntaxError: encoding problem: ascii with BOM

Example of changed behavior:

$ printf '# coding: utf-8\n# coding: latin-1\nprint("\xc3\xa5")\n' >up009_2.py
$ python up009_2.py
å
$ ruff check --isolated --select UP009 up009_2.py --fix
Found 1 error (1 fixed, 0 remaining).
$ python up009_2.py
Ã¥
@AlexWaygood AlexWaygood added bug Something isn't working fixes Related to suggested fixes for violations rule Implementing or modifying a lint rule and removed fixes Related to suggested fixes for violations labels Dec 1, 2024
@AlexWaygood
Copy link
Member

or the check should not report a violation.

I'd vote for this option. The rule's purpose is to flag unnecessary UTF-8 encoding declarations. Clearly in some cases it's erroneously flagging encodings which are, in fact, necessary :-)

@AlexWaygood AlexWaygood added the help wanted Contributions especially welcome label Dec 1, 2024
@MichaReiser
Copy link
Member

Somewhat related to #6791

MichaReiser added a commit that referenced this issue Dec 11, 2024
…-UTF-8 one (`UP009`) (#14728)

## Summary

Resolves #14704.

## Test Plan

`cargo nextest run` and `cargo insta test`.

---------

Co-authored-by: Micha Reiser <micha@reiser.io>
TheBits pushed a commit to TheBits/ruff that referenced this issue Dec 11, 2024
…-UTF-8 one (`UP009`) (astral-sh#14728)

## Summary

Resolves astral-sh#14704.

## Test Plan

`cargo nextest run` and `cargo insta test`.

---------

Co-authored-by: Micha Reiser <micha@reiser.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Contributions especially welcome rule Implementing or modifying a lint rule
Projects
None yet
3 participants