-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
ruff does not honor declaration of character coding #6791
Comments
Thanks for reporting this issue. Yes, your observation is correct. Ruff currently has no support for the From an implementation standpoint. I rather don't add support for non-UTF-8 strings in the lexer/parser. We should rather normalize the string to UTF-8 as early as possible, ideally when, or after reading the file. The main challenge that I see comes with fixes. We would need to write the string back using the original encoding. So we would need to keep that information around. |
…gnarly to plumb this through everywhere, and given the lack of uproar over ruff's complete lack of handling of non-utf8 files astral-sh/ruff#6791, we can just warn about it instead. the only potential negative effects are that the bytes on disk in the python file for something like a LocalState that takes it's key from the member name, might not match exactly what's in the TEAL file (and thus on chain). - refactor to slightly reduce mypy dependencies in ParseResult - pull out component orderding and checking to the parse level - construct read_source directly and add caching at the top level
I believe that linters in general should reject at least some source encodings. I agree that support for encoding declarations doesn't need to be a priority, but if Ruff can't handle them, maybe there should be a linter rule for them? (For Ruff users, “you shouldn't do this if you want to use Ruff” is basically equivalent to “you shouldn't do this”, right?) |
According to PEP263, a character encoding can be declared in a Python program file. This is done with a specially formatted comment placed in the first or second line of the program:
It seems that Ruff (0.0.285) does not honor the coding declaration. Ruff seems to assume that input files are always encoded with utf8. The following Python program demonstrates the problem. It first generates three short Python program files with different encodings and than runs ruff and python3 on them.
The first file with utf8 encoding runs flawlessly with ruff and with python3. This is the expected behaviour.
The second file comprises characters in utf8 encoding, but wrongly declares us-ascii encoding. This file throws an error when run with python3, but successfully passes ruff. I would expect that ruff complains on this file.
The third file comprises characters in latin1 encoding and correctly declares its encoding. This program runs successfully with python3, but throws an error when checked with ruff. I would expect that ruff does not complain on this file.
The text was updated successfully, but these errors were encountered: