---
source: crates/ruff_linter/src/rules/pyupgrade/mod.rs
snapshot_kind: text
---
UP012.py:2:1: UP012 [*] Unnecessary call to `encode` as UTF-8
  |
1 | # ASCII literals should be replaced by a bytes literal
2 | "foo".encode("utf-8")  # b"foo"
  | ^^^^^^^^^^^^^^^^^^^^^ UP012
3 | "foo".encode("u8")  # b"foo"
4 | "foo".encode()  # b"foo"
  |
  = help: Rewrite as bytes literal

ℹ Safe fix
1 1 | # ASCII literals should be replaced by a bytes literal
2   |-"foo".encode("utf-8")  # b"foo"
  2 |+b"foo"  # b"foo"
3 3 | "foo".encode("u8")  # b"foo"
4 4 | "foo".encode()  # b"foo"
5 5 | "foo".encode("UTF8")  # b"foo"

UP012.py:3:1: UP012 [*] Unnecessary call to `encode` as UTF-8
  |
1 | # ASCII literals should be replaced by a bytes literal
2 | "foo".encode("utf-8")  # b"foo"
3 | "foo".encode("u8")  # b"foo"
  | ^^^^^^^^^^^^^^^^^^ UP012
4 | "foo".encode()  # b"foo"
5 | "foo".encode("UTF8")  # b"foo"
  |
  = help: Rewrite as bytes literal

ℹ Safe fix
1 1 | # ASCII literals should be replaced by a bytes literal
2 2 | "foo".encode("utf-8")  # b"foo"
3   |-"foo".encode("u8")  # b"foo"
  3 |+b"foo"  # b"foo"
4 4 | "foo".encode()  # b"foo"
5 5 | "foo".encode("UTF8")  # b"foo"
6 6 | U"foo".encode("utf-8")  # b"foo"

UP012.py:4:1: UP012 [*] Unnecessary call to `encode` as UTF-8
  |
2 | "foo".encode("utf-8")  # b"foo"
3 | "foo".encode("u8")  # b"foo"
4 | "foo".encode()  # b"foo"
  | ^^^^^^^^^^^^^^ UP012
5 | "foo".encode("UTF8")  # b"foo"
6 | U"foo".encode("utf-8")  # b"foo"
  |
  = help: Rewrite as bytes literal

ℹ Safe fix
1 1 | # ASCII literals should be replaced by a bytes literal
2 2 | "foo".encode("utf-8")  # b"foo"
3 3 | "foo".encode("u8")  # b"foo"
4   |-"foo".encode()  # b"foo"
  4 |+b"foo"  # b"foo"
5 5 | "foo".encode("UTF8")  # b"foo"
6 6 | U"foo".encode("utf-8")  # b"foo"
7 7 | "foo".encode(encoding="utf-8")  # b"foo"

UP012.py:5:1: UP012 [*] Unnecessary call to `encode` as UTF-8
  |
3 | "foo".encode("u8")  # b"foo"
4 | "foo".encode()  # b"foo"
5 | "foo".encode("UTF8")  # b"foo"
  | ^^^^^^^^^^^^^^^^^^^^ UP012
6 | U"foo".encode("utf-8")  # b"foo"
7 | "foo".encode(encoding="utf-8")  # b"foo"
  |
  = help: Rewrite as bytes literal

ℹ Safe fix
2 2 | "foo".encode("utf-8")  # b"foo"
3 3 | "foo".encode("u8")  # b"foo"
4 4 | "foo".encode()  # b"foo"
5   |-"foo".encode("UTF8")  # b"foo"
  5 |+b"foo"  # b"foo"
6 6 | U"foo".encode("utf-8")  # b"foo"
7 7 | "foo".encode(encoding="utf-8")  # b"foo"
8 8 | """

UP012.py:6:1: UP012 [*] Unnecessary call to `encode` as UTF-8
  |
4 | "foo".encode()  # b"foo"
5 | "foo".encode("UTF8")  # b"foo"
6 | U"foo".encode("utf-8")  # b"foo"
  | ^^^^^^^^^^^^^^^^^^^^^^ UP012
7 | "foo".encode(encoding="utf-8")  # b"foo"
8 | """
  |
  = help: Rewrite as bytes literal

ℹ Safe fix
3 3 | "foo".encode("u8")  # b"foo"
4 4 | "foo".encode()  # b"foo"
5 5 | "foo".encode("UTF8")  # b"foo"
6   |-U"foo".encode("utf-8")  # b"foo"
  6 |+b"foo"  # b"foo"
7 7 | "foo".encode(encoding="utf-8")  # b"foo"
8 8 | """
9 9 | Lorem

UP012.py:7:1: UP012 [*] Unnecessary call to `encode` as UTF-8
  |
5 | "foo".encode("UTF8")  # b"foo"
6 | U"foo".encode("utf-8")  # b"foo"
7 | "foo".encode(encoding="utf-8")  # b"foo"
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ UP012
8 | """
9 | Lorem
  |
  = help: Rewrite as bytes literal

ℹ Safe fix
4 4 | "foo".encode()  # b"foo"
5 5 | "foo".encode("UTF8")  # b"foo"
6 6 | U"foo".encode("utf-8")  # b"foo"
7   |-"foo".encode(encoding="utf-8")  # b"foo"
  7 |+b"foo"  # b"foo"
8 8 | """
9 9 | Lorem
10 10 | 

UP012.py:8:1: UP012 [*] Unnecessary call to `encode` as UTF-8
   |
 6 |   U"foo".encode("utf-8")  # b"foo"
 7 |   "foo".encode(encoding="utf-8")  # b"foo"
 8 | / """
 9 | | Lorem
10 | | 
11 | | Ipsum
12 | | """.encode(
13 | |     "utf-8"
14 | | )
   | |_^ UP012
15 |   (
16 |       "Lorem "
   |
   = help: Rewrite as bytes literal

ℹ Safe fix
5  5  | "foo".encode("UTF8")  # b"foo"
6  6  | U"foo".encode("utf-8")  # b"foo"
7  7  | "foo".encode(encoding="utf-8")  # b"foo"
8     |-"""
   8  |+b"""
9  9  | Lorem
10 10 | 
11 11 | Ipsum
12    |-""".encode(
13    |-    "utf-8"
14    |-)
   12 |+"""
15 13 | (
16 14 |     "Lorem "
17 15 |     "Ipsum".encode()

UP012.py:16:5: UP012 [*] Unnecessary call to `encode` as UTF-8
   |
14 |   )
15 |   (
16 |       "Lorem "
   |  _____^
17 | |     "Ipsum".encode()
   | |____________________^ UP012
18 |   )
19 |   (
   |
   = help: Rewrite as bytes literal

ℹ Safe fix
13 13 |     "utf-8"
14 14 | )
15 15 | (
16    |-    "Lorem "
17    |-    "Ipsum".encode()
   16 |+    b"Lorem "
   17 |+    b"Ipsum"
18 18 | )
19 19 | (
20 20 |     "Lorem "  # Comment

UP012.py:20:5: UP012 [*] Unnecessary call to `encode` as UTF-8
   |
18 |   )
19 |   (
20 |       "Lorem "  # Comment
   |  _____^
21 | |     "Ipsum".encode()  # Comment
   | |____________________^ UP012
22 |   )
23 |   (
   |
   = help: Rewrite as bytes literal

ℹ Safe fix
17 17 |     "Ipsum".encode()
18 18 | )
19 19 | (
20    |-    "Lorem "  # Comment
21    |-    "Ipsum".encode()  # Comment
   20 |+    b"Lorem "  # Comment
   21 |+    b"Ipsum"  # Comment
22 22 | )
23 23 | (
24 24 |     "Lorem " "Ipsum".encode()

UP012.py:24:5: UP012 [*] Unnecessary call to `encode` as UTF-8
   |
22 | )
23 | (
24 |     "Lorem " "Ipsum".encode()
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^ UP012
25 | )
   |
   = help: Rewrite as bytes literal

ℹ Safe fix
21 21 |     "Ipsum".encode()  # Comment
22 22 | )
23 23 | (
24    |-    "Lorem " "Ipsum".encode()
   24 |+    b"Lorem " b"Ipsum"
25 25 | )
26 26 | 
27 27 | # `encode` on variables should not be processed.

UP012.py:32:1: UP012 [*] Unnecessary UTF-8 `encoding` argument to `encode`
   |
31 | bar = "bar"
32 | f"foo{bar}".encode("utf-8")
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ UP012
33 | encoding = "latin"
34 | "foo".encode(encoding)
   |
   = help: Remove unnecessary `encoding` argument

ℹ Safe fix
29 29 | string.encode("utf-8")
30 30 | 
31 31 | bar = "bar"
32    |-f"foo{bar}".encode("utf-8")
   32 |+f"foo{bar}".encode()
33 33 | encoding = "latin"
34 34 | "foo".encode(encoding)
35 35 | f"foo{bar}".encode(encoding)

UP012.py:36:1: UP012 [*] Unnecessary UTF-8 `encoding` argument to `encode`
   |
34 |   "foo".encode(encoding)
35 |   f"foo{bar}".encode(encoding)
36 | / f"{a=} {b=}".encode(
37 | |     "utf-8",
38 | | )
   | |_^ UP012
39 |   
40 |   # `encode` with custom args and kwargs should not be processed.
   |
   = help: Remove unnecessary `encoding` argument

ℹ Safe fix
33 33 | encoding = "latin"
34 34 | "foo".encode(encoding)
35 35 | f"foo{bar}".encode(encoding)
36    |-f"{a=} {b=}".encode(
37    |-    "utf-8",
38    |-)
   36 |+f"{a=} {b=}".encode()
39 37 | 
40 38 | # `encode` with custom args and kwargs should not be processed.
41 39 | "foo".encode("utf-8", errors="replace")

UP012.py:53:1: UP012 [*] Unnecessary UTF-8 `encoding` argument to `encode`
   |
52 | # Unicode literals should only be stripped of default encoding.
53 | "unicode text©".encode("utf-8")  # "unicode text©".encode()
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ UP012
54 | "unicode text©".encode()
55 | "unicode text©".encode(encoding="UTF8")  # "unicode text©".encode()
   |
   = help: Remove unnecessary `encoding` argument

ℹ Safe fix
50 50 | "unicode text©".encode(encoding="utf-8", errors="replace")
51 51 | 
52 52 | # Unicode literals should only be stripped of default encoding.
53    |-"unicode text©".encode("utf-8")  # "unicode text©".encode()
   53 |+"unicode text©".encode()  # "unicode text©".encode()
54 54 | "unicode text©".encode()
55 55 | "unicode text©".encode(encoding="UTF8")  # "unicode text©".encode()
56 56 | 

UP012.py:55:1: UP012 [*] Unnecessary UTF-8 `encoding` argument to `encode`
   |
53 | "unicode text©".encode("utf-8")  # "unicode text©".encode()
54 | "unicode text©".encode()
55 | "unicode text©".encode(encoding="UTF8")  # "unicode text©".encode()
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ UP012
56 | 
57 | r"foo\o".encode("utf-8")  # br"foo\o"
   |
   = help: Remove unnecessary `encoding` argument

ℹ Safe fix
52 52 | # Unicode literals should only be stripped of default encoding.
53 53 | "unicode text©".encode("utf-8")  # "unicode text©".encode()
54 54 | "unicode text©".encode()
55    |-"unicode text©".encode(encoding="UTF8")  # "unicode text©".encode()
   55 |+"unicode text©".encode()  # "unicode text©".encode()
56 56 | 
57 57 | r"foo\o".encode("utf-8")  # br"foo\o"
58 58 | u"foo".encode("utf-8")  # b"foo"

UP012.py:57:1: UP012 [*] Unnecessary call to `encode` as UTF-8
   |
55 | "unicode text©".encode(encoding="UTF8")  # "unicode text©".encode()
56 | 
57 | r"foo\o".encode("utf-8")  # br"foo\o"
   | ^^^^^^^^^^^^^^^^^^^^^^^^ UP012
58 | u"foo".encode("utf-8")  # b"foo"
59 | R"foo\o".encode("utf-8")  # br"foo\o"
   |
   = help: Rewrite as bytes literal

ℹ Safe fix
54 54 | "unicode text©".encode()
55 55 | "unicode text©".encode(encoding="UTF8")  # "unicode text©".encode()
56 56 | 
57    |-r"foo\o".encode("utf-8")  # br"foo\o"
   57 |+br"foo\o"  # br"foo\o"
58 58 | u"foo".encode("utf-8")  # b"foo"
59 59 | R"foo\o".encode("utf-8")  # br"foo\o"
60 60 | U"foo".encode("utf-8")  # b"foo"

UP012.py:58:1: UP012 [*] Unnecessary call to `encode` as UTF-8
   |
57 | r"foo\o".encode("utf-8")  # br"foo\o"
58 | u"foo".encode("utf-8")  # b"foo"
   | ^^^^^^^^^^^^^^^^^^^^^^ UP012
59 | R"foo\o".encode("utf-8")  # br"foo\o"
60 | U"foo".encode("utf-8")  # b"foo"
   |
   = help: Rewrite as bytes literal

ℹ Safe fix
55 55 | "unicode text©".encode(encoding="UTF8")  # "unicode text©".encode()
56 56 | 
57 57 | r"foo\o".encode("utf-8")  # br"foo\o"
58    |-u"foo".encode("utf-8")  # b"foo"
   58 |+b"foo"  # b"foo"
59 59 | R"foo\o".encode("utf-8")  # br"foo\o"
60 60 | U"foo".encode("utf-8")  # b"foo"
61 61 | print("foo".encode())  # print(b"foo")

UP012.py:59:1: UP012 [*] Unnecessary call to `encode` as UTF-8
   |
57 | r"foo\o".encode("utf-8")  # br"foo\o"
58 | u"foo".encode("utf-8")  # b"foo"
59 | R"foo\o".encode("utf-8")  # br"foo\o"
   | ^^^^^^^^^^^^^^^^^^^^^^^^ UP012
60 | U"foo".encode("utf-8")  # b"foo"
61 | print("foo".encode())  # print(b"foo")
   |
   = help: Rewrite as bytes literal

ℹ Safe fix
56 56 | 
57 57 | r"foo\o".encode("utf-8")  # br"foo\o"
58 58 | u"foo".encode("utf-8")  # b"foo"
59    |-R"foo\o".encode("utf-8")  # br"foo\o"
   59 |+bR"foo\o"  # br"foo\o"
60 60 | U"foo".encode("utf-8")  # b"foo"
61 61 | print("foo".encode())  # print(b"foo")
62 62 | 

UP012.py:60:1: UP012 [*] Unnecessary call to `encode` as UTF-8
   |
58 | u"foo".encode("utf-8")  # b"foo"
59 | R"foo\o".encode("utf-8")  # br"foo\o"
60 | U"foo".encode("utf-8")  # b"foo"
   | ^^^^^^^^^^^^^^^^^^^^^^ UP012
61 | print("foo".encode())  # print(b"foo")
   |
   = help: Rewrite as bytes literal

ℹ Safe fix
57 57 | r"foo\o".encode("utf-8")  # br"foo\o"
58 58 | u"foo".encode("utf-8")  # b"foo"
59 59 | R"foo\o".encode("utf-8")  # br"foo\o"
60    |-U"foo".encode("utf-8")  # b"foo"
   60 |+b"foo"  # b"foo"
61 61 | print("foo".encode())  # print(b"foo")
62 62 | 
63 63 | # `encode` on parenthesized strings.

UP012.py:61:7: UP012 [*] Unnecessary call to `encode` as UTF-8
   |
59 | R"foo\o".encode("utf-8")  # br"foo\o"
60 | U"foo".encode("utf-8")  # b"foo"
61 | print("foo".encode())  # print(b"foo")
   |       ^^^^^^^^^^^^^^ UP012
62 | 
63 | # `encode` on parenthesized strings.
   |
   = help: Rewrite as bytes literal

ℹ Safe fix
58 58 | u"foo".encode("utf-8")  # b"foo"
59 59 | R"foo\o".encode("utf-8")  # br"foo\o"
60 60 | U"foo".encode("utf-8")  # b"foo"
61    |-print("foo".encode())  # print(b"foo")
   61 |+print(b"foo")  # print(b"foo")
62 62 | 
63 63 | # `encode` on parenthesized strings.
64 64 | (

UP012.py:64:1: UP012 [*] Unnecessary call to `encode` as UTF-8
   |
63 |   # `encode` on parenthesized strings.
64 | / (
65 | |     "abc"
66 | |     "def"
67 | | ).encode()
   | |__________^ UP012
68 |   
69 |   ((
   |
   = help: Rewrite as bytes literal

ℹ Safe fix
62 62 | 
63 63 | # `encode` on parenthesized strings.
64 64 | (
65    |-    "abc"
66    |-    "def"
67    |-).encode()
   65 |+    b"abc"
   66 |+    b"def"
   67 |+)
68 68 | 
69 69 | ((
70 70 |     "abc"

UP012.py:69:1: UP012 [*] Unnecessary call to `encode` as UTF-8
   |
67 |   ).encode()
68 |   
69 | / ((
70 | |     "abc"
71 | |     "def"
72 | | )).encode()
   | |___________^ UP012
73 |   
74 |   (f"foo{bar}").encode("utf-8")
   |
   = help: Rewrite as bytes literal

ℹ Safe fix
67 67 | ).encode()
68 68 | 
69 69 | ((
70    |-    "abc"
71    |-    "def"
72    |-)).encode()
   70 |+    b"abc"
   71 |+    b"def"
   72 |+))
73 73 | 
74 74 | (f"foo{bar}").encode("utf-8")
75 75 | (f"foo{bar}").encode(encoding="utf-8")

UP012.py:74:1: UP012 [*] Unnecessary UTF-8 `encoding` argument to `encode`
   |
72 | )).encode()
73 | 
74 | (f"foo{bar}").encode("utf-8")
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ UP012
75 | (f"foo{bar}").encode(encoding="utf-8")
76 | ("unicode text©").encode("utf-8")
   |
   = help: Remove unnecessary `encoding` argument

ℹ Safe fix
71 71 |     "def"
72 72 | )).encode()
73 73 | 
74    |-(f"foo{bar}").encode("utf-8")
   74 |+(f"foo{bar}").encode()
75 75 | (f"foo{bar}").encode(encoding="utf-8")
76 76 | ("unicode text©").encode("utf-8")
77 77 | ("unicode text©").encode(encoding="utf-8")

UP012.py:75:1: UP012 [*] Unnecessary UTF-8 `encoding` argument to `encode`
   |
74 | (f"foo{bar}").encode("utf-8")
75 | (f"foo{bar}").encode(encoding="utf-8")
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ UP012
76 | ("unicode text©").encode("utf-8")
77 | ("unicode text©").encode(encoding="utf-8")
   |
   = help: Remove unnecessary `encoding` argument

ℹ Safe fix
72 72 | )).encode()
73 73 | 
74 74 | (f"foo{bar}").encode("utf-8")
75    |-(f"foo{bar}").encode(encoding="utf-8")
   75 |+(f"foo{bar}").encode()
76 76 | ("unicode text©").encode("utf-8")
77 77 | ("unicode text©").encode(encoding="utf-8")
78 78 | 

UP012.py:76:1: UP012 [*] Unnecessary UTF-8 `encoding` argument to `encode`
   |
74 | (f"foo{bar}").encode("utf-8")
75 | (f"foo{bar}").encode(encoding="utf-8")
76 | ("unicode text©").encode("utf-8")
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ UP012
77 | ("unicode text©").encode(encoding="utf-8")
   |
   = help: Remove unnecessary `encoding` argument

ℹ Safe fix
73 73 | 
74 74 | (f"foo{bar}").encode("utf-8")
75 75 | (f"foo{bar}").encode(encoding="utf-8")
76    |-("unicode text©").encode("utf-8")
   76 |+("unicode text©").encode()
77 77 | ("unicode text©").encode(encoding="utf-8")
78 78 | 
79 79 | 

UP012.py:77:1: UP012 [*] Unnecessary UTF-8 `encoding` argument to `encode`
   |
75 | (f"foo{bar}").encode(encoding="utf-8")
76 | ("unicode text©").encode("utf-8")
77 | ("unicode text©").encode(encoding="utf-8")
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ UP012
   |
   = help: Remove unnecessary `encoding` argument

ℹ Safe fix
74 74 | (f"foo{bar}").encode("utf-8")
75 75 | (f"foo{bar}").encode(encoding="utf-8")
76 76 | ("unicode text©").encode("utf-8")
77    |-("unicode text©").encode(encoding="utf-8")
   77 |+("unicode text©").encode()
78 78 | 
79 79 | 
80 80 | # Regression test for: https://github.com/astral-sh/ruff/issues/7455#issuecomment-1722459882

UP012.py:82:17: UP012 [*] Unnecessary call to `encode` as UTF-8
   |
80 | # Regression test for: https://github.com/astral-sh/ruff/issues/7455#issuecomment-1722459882
81 | def _match_ignore(line):
82 |     input=stdin and'\n'.encode()or None
   |                    ^^^^^^^^^^^^^ UP012
83 | 
84 | # Not a valid type annotation but this test shouldn't result in a panic.
   |
   = help: Rewrite as bytes literal

ℹ Safe fix
79 79 | 
80 80 | # Regression test for: https://github.com/astral-sh/ruff/issues/7455#issuecomment-1722459882
81 81 | def _match_ignore(line):
82    |-	input=stdin and'\n'.encode()or None
   82 |+	input=stdin and b'\n' or None
83 83 | 
84 84 | # Not a valid type annotation but this test shouldn't result in a panic.
85 85 | # Refer: https://github.com/astral-sh/ruff/issues/11736

UP012.py:86:5: UP012 [*] Unnecessary call to `encode` as UTF-8
   |
84 | # Not a valid type annotation but this test shouldn't result in a panic.
85 | # Refer: https://github.com/astral-sh/ruff/issues/11736
86 | x: '"foo".encode("utf-8")'
   |     ^^^^^^^^^^^^^^^^^^^^^ UP012
   |
   = help: Rewrite as bytes literal

ℹ Safe fix
83 83 | 
84 84 | # Not a valid type annotation but this test shouldn't result in a panic.
85 85 | # Refer: https://github.com/astral-sh/ruff/issues/11736
86    |-x: '"foo".encode("utf-8")'
   86 |+x: 'b"foo"'
