Skip to content

Commit

Permalink
Merge pull request #485 from MarcoGorelli/fstring-encoding
Browse files Browse the repository at this point in the history
f"wat".encode("utf-8") can also be rewritten to remove the encoding
  • Loading branch information
asottile committed Jul 10, 2021
2 parents 2702276 + 6dc0619 commit df6fb5a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pyupgrade/_plugins/default_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def visit_Call(
if (
state.settings.min_version >= (3,) and
isinstance(node.func, ast.Attribute) and
isinstance(node.func.value, ast.Str) and
isinstance(node.func.value, (ast.Str, ast.JoinedStr)) and
node.func.attr == 'encode' and
not has_starargs(node) and
len(node.args) == 1 and
Expand Down
4 changes: 4 additions & 0 deletions tests/features/default_encoding_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
('s', 'expected'),
(
('"asd".encode("utf-8")', '"asd".encode()'),
('f"asd".encode("utf-8")', 'f"asd".encode()'),
('f"{3}asd".encode("utf-8")', 'f"{3}asd".encode()'),
('fr"asd".encode("utf-8")', 'fr"asd".encode()'),
('r"asd".encode("utf-8")', 'r"asd".encode()'),
('"asd".encode("utf8")', '"asd".encode()'),
('"asd".encode("UTF-8")', '"asd".encode()'),
pytest.param(
Expand Down

0 comments on commit df6fb5a

Please sign in to comment.