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

The .encode("utf-8") fixer generates wrong code #989

Closed
svenpanne opened this issue Dec 2, 2024 · 0 comments · Fixed by #994
Closed

The .encode("utf-8") fixer generates wrong code #989

svenpanne opened this issue Dec 2, 2024 · 0 comments · Fixed by #994

Comments

@svenpanne
Copy link

Repro: Add the following test:

diff --git a/tests/features/default_encoding_test.py b/tests/features/default_encoding_test.py
index f039fdc..6fb1277 100644
--- a/tests/features/default_encoding_test.py
+++ b/tests/features/default_encoding_test.py
@@ -38,6 +38,11 @@ from pyupgrade._main import _fix_plugins
             'f"{x}(".encode()',
             id='3.12+ handle open brace in fstring',
         ),
+        pytest.param(
+            'f"{foo(bar)}(".encode("utf-8")',
+            'f"{foo(bar)}(".encode()',
+            id='f-string with function call',
+        ),
     ),
 )
 def test_fix_encode(s, expected):

The corresponding test will then fail with:

...
>       assert ret == expected
E       assert 'f"{foo()}(".encode("utf-8")' == 'f"{foo(bar)}(".encode()'
E         
E         - f"{foo(bar)}(".encode()
E         ?        ---
E         + f"{foo()}(".encode("utf-8")
E         ?                    +++++++

tests/features/default_encoding_test.py:50: AssertionError

i.e. the fixer doesn't remove the "utf-8" literal, it removes the argument of another function call-

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 a pull request may close this issue.

1 participant