diff --git a/crates/ruff_linter/src/rules/pylint/mod.rs b/crates/ruff_linter/src/rules/pylint/mod.rs index a7b3ded6f8c81..a5fcc7deeed2d 100644 --- a/crates/ruff_linter/src/rules/pylint/mod.rs +++ b/crates/ruff_linter/src/rules/pylint/mod.rs @@ -396,15 +396,4 @@ mod tests { assert_messages!(diagnostics); Ok(()) } - - #[test] - fn unspecified_encoding_python39_or_lower() -> Result<()> { - let diagnostics = test_path( - Path::new("pylint/unspecified_encoding.py"), - &LinterSettings::for_rule(Rule::UnspecifiedEncoding) - .with_target_version(PythonVersion::Py39), - )?; - assert_messages!(diagnostics); - Ok(()) - } } diff --git a/crates/ruff_linter/src/rules/pylint/rules/unspecified_encoding.rs b/crates/ruff_linter/src/rules/pylint/rules/unspecified_encoding.rs index c0be9c0939c7e..a8a5ac69f61dd 100644 --- a/crates/ruff_linter/src/rules/pylint/rules/unspecified_encoding.rs +++ b/crates/ruff_linter/src/rules/pylint/rules/unspecified_encoding.rs @@ -1,7 +1,5 @@ use std::fmt::{Display, Formatter}; -use anyhow::Result; - use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Fix}; use ruff_macros::{derive_message_formats, violation}; use ruff_python_ast::name::QualifiedName; @@ -11,8 +9,6 @@ use ruff_text_size::{Ranged, TextRange}; use crate::checkers::ast::Checker; use crate::fix::edits::add_argument; -use crate::importer::ImportRequest; -use crate::settings::types::PythonVersion; /// ## What it does /// Checks for uses of `open` and related calls without an explicit `encoding` @@ -20,12 +16,17 @@ use crate::settings::types::PythonVersion; /// /// ## Why is this bad? /// Using `open` in text mode without an explicit encoding can lead to -/// non-portable code, with differing behavior across platforms. +/// non-portable code, with differing behavior across platforms. While readers +/// may assume that UTF-8 is the default encoding, in reality, the default +/// is locale-specific. /// /// Instead, consider using the `encoding` parameter to enforce a specific -/// encoding. [PEP 597] recommends using `locale.getpreferredencoding(False)` -/// as the default encoding on versions earlier than Python 3.10, and -/// `encoding="locale"` on Python 3.10 and later. +/// encoding. [PEP 597] recommends the use of `encoding="utf-8"` as a default, +/// and suggests that it may become the default in future versions of Python. +/// +/// If a local-specific encoding is intended, use `encoding="local"` on +/// Python 3.10 and later, or `locale.getpreferredencoding()` on earlier versions, +/// to make the encoding explicit. /// /// ## Example /// ```python @@ -86,13 +87,7 @@ pub(crate) fn unspecified_encoding(checker: &mut Checker, call: &ast::ExprCall) }, call.func.range(), ); - - if checker.settings.target_version >= PythonVersion::Py310 { - diagnostic.set_fix(generate_keyword_fix(checker, call)); - } else { - diagnostic.try_set_fix(|| generate_import_fix(checker, call)); - } - + diagnostic.set_fix(generate_keyword_fix(checker, call)); checker.diagnostics.push(diagnostic); } @@ -158,7 +153,7 @@ impl Display for Callee<'_> { } } -/// Generate an [`Edit`] for Python 3.10 and later. +/// Generate an [`Edit`] to set `encoding="utf-8"`. fn generate_keyword_fix(checker: &Checker, call: &ast::ExprCall) -> Fix { Fix::unsafe_edit(add_argument( &format!( @@ -167,7 +162,7 @@ fn generate_keyword_fix(checker: &Checker, call: &ast::ExprCall) -> Fix { .generator() .expr(&Expr::StringLiteral(ast::ExprStringLiteral { value: ast::StringLiteralValue::single(ast::StringLiteral { - value: "locale".to_string().into_boxed_str(), + value: "utf-8".to_string().into_boxed_str(), flags: StringLiteralFlags::default(), range: TextRange::default(), }), @@ -180,22 +175,6 @@ fn generate_keyword_fix(checker: &Checker, call: &ast::ExprCall) -> Fix { )) } -/// Generate an [`Edit`] for Python 3.9 and earlier. -fn generate_import_fix(checker: &Checker, call: &ast::ExprCall) -> Result { - let (import_edit, binding) = checker.importer().get_or_import_symbol( - &ImportRequest::import("locale", "getpreferredencoding"), - call.start(), - checker.semantic(), - )?; - let argument_edit = add_argument( - &format!("encoding={binding}(False)"), - &call.arguments, - checker.comment_ranges(), - checker.locator().contents(), - ); - Ok(Fix::unsafe_edits(import_edit, [argument_edit])) -} - /// Returns `true` if the given expression is a string literal containing a `b` character. fn is_binary_mode(expr: &Expr) -> Option { Some( diff --git a/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLW1514_unspecified_encoding.py.snap b/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLW1514_unspecified_encoding.py.snap index 200c1e380cdf6..9b6ebb6c85814 100644 --- a/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLW1514_unspecified_encoding.py.snap +++ b/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLW1514_unspecified_encoding.py.snap @@ -16,7 +16,7 @@ unspecified_encoding.py:8:1: PLW1514 [*] `open` in text mode without explicit `e 6 6 | 7 7 | # Errors. 8 |-open("test.txt") - 8 |+open("test.txt", encoding="locale") + 8 |+open("test.txt", encoding="utf-8") 9 9 | io.TextIOWrapper(io.FileIO("test.txt")) 10 10 | hugo.TextIOWrapper(hugo.FileIO("test.txt")) 11 11 | tempfile.NamedTemporaryFile("w") @@ -37,7 +37,7 @@ unspecified_encoding.py:9:1: PLW1514 [*] `io.TextIOWrapper` without explicit `en 7 7 | # Errors. 8 8 | open("test.txt") 9 |-io.TextIOWrapper(io.FileIO("test.txt")) - 9 |+io.TextIOWrapper(io.FileIO("test.txt"), encoding="locale") + 9 |+io.TextIOWrapper(io.FileIO("test.txt"), encoding="utf-8") 10 10 | hugo.TextIOWrapper(hugo.FileIO("test.txt")) 11 11 | tempfile.NamedTemporaryFile("w") 12 12 | tempfile.TemporaryFile("w") @@ -58,7 +58,7 @@ unspecified_encoding.py:10:1: PLW1514 [*] `io.TextIOWrapper` without explicit `e 8 8 | open("test.txt") 9 9 | io.TextIOWrapper(io.FileIO("test.txt")) 10 |-hugo.TextIOWrapper(hugo.FileIO("test.txt")) - 10 |+hugo.TextIOWrapper(hugo.FileIO("test.txt"), encoding="locale") + 10 |+hugo.TextIOWrapper(hugo.FileIO("test.txt"), encoding="utf-8") 11 11 | tempfile.NamedTemporaryFile("w") 12 12 | tempfile.TemporaryFile("w") 13 13 | codecs.open("test.txt") @@ -79,7 +79,7 @@ unspecified_encoding.py:11:1: PLW1514 [*] `tempfile.NamedTemporaryFile` in text 9 9 | io.TextIOWrapper(io.FileIO("test.txt")) 10 10 | hugo.TextIOWrapper(hugo.FileIO("test.txt")) 11 |-tempfile.NamedTemporaryFile("w") - 11 |+tempfile.NamedTemporaryFile("w", encoding="locale") + 11 |+tempfile.NamedTemporaryFile("w", encoding="utf-8") 12 12 | tempfile.TemporaryFile("w") 13 13 | codecs.open("test.txt") 14 14 | tempfile.SpooledTemporaryFile(0, "w") @@ -100,7 +100,7 @@ unspecified_encoding.py:12:1: PLW1514 [*] `tempfile.TemporaryFile` in text mode 10 10 | hugo.TextIOWrapper(hugo.FileIO("test.txt")) 11 11 | tempfile.NamedTemporaryFile("w") 12 |-tempfile.TemporaryFile("w") - 12 |+tempfile.TemporaryFile("w", encoding="locale") + 12 |+tempfile.TemporaryFile("w", encoding="utf-8") 13 13 | codecs.open("test.txt") 14 14 | tempfile.SpooledTemporaryFile(0, "w") 15 15 | @@ -120,7 +120,7 @@ unspecified_encoding.py:13:1: PLW1514 [*] `codecs.open` in text mode without exp 11 11 | tempfile.NamedTemporaryFile("w") 12 12 | tempfile.TemporaryFile("w") 13 |-codecs.open("test.txt") - 13 |+codecs.open("test.txt", encoding="locale") + 13 |+codecs.open("test.txt", encoding="utf-8") 14 14 | tempfile.SpooledTemporaryFile(0, "w") 15 15 | 16 16 | # Non-errors. @@ -141,7 +141,7 @@ unspecified_encoding.py:14:1: PLW1514 [*] `tempfile.SpooledTemporaryFile` in tex 12 12 | tempfile.TemporaryFile("w") 13 13 | codecs.open("test.txt") 14 |-tempfile.SpooledTemporaryFile(0, "w") - 14 |+tempfile.SpooledTemporaryFile(0, "w", encoding="locale") + 14 |+tempfile.SpooledTemporaryFile(0, "w", encoding="utf-8") 15 15 | 16 16 | # Non-errors. 17 17 | open("test.txt", encoding="utf-8") @@ -162,7 +162,7 @@ unspecified_encoding.py:46:1: PLW1514 [*] `open` in text mode without explicit ` 44 44 | tempfile.SpooledTemporaryFile(0, ) 45 45 | 46 |-open("test.txt",) - 46 |+open("test.txt", encoding="locale",) + 46 |+open("test.txt", encoding="utf-8",) 47 47 | open() 48 48 | open( 49 49 | "test.txt", # comment @@ -182,7 +182,7 @@ unspecified_encoding.py:47:1: PLW1514 [*] `open` in text mode without explicit ` 45 45 | 46 46 | open("test.txt",) 47 |-open() - 47 |+open(encoding="locale") + 47 |+open(encoding="utf-8") 48 48 | open( 49 49 | "test.txt", # comment 50 50 | ) @@ -203,7 +203,7 @@ unspecified_encoding.py:48:1: PLW1514 [*] `open` in text mode without explicit ` 47 47 | open() 48 48 | open( 49 |- "test.txt", # comment - 49 |+ "test.txt", encoding="locale", # comment + 49 |+ "test.txt", encoding="utf-8", # comment 50 50 | ) 51 51 | open( 52 52 | "test.txt", @@ -224,7 +224,7 @@ unspecified_encoding.py:51:1: PLW1514 [*] `open` in text mode without explicit ` 50 50 | ) 51 51 | open( 52 |- "test.txt", - 52 |+ "test.txt", encoding="locale", + 52 |+ "test.txt", encoding="utf-8", 53 53 | # comment 54 54 | ) 55 55 | open(("test.txt"),) @@ -245,7 +245,7 @@ unspecified_encoding.py:55:1: PLW1514 [*] `open` in text mode without explicit ` 53 53 | # comment 54 54 | ) 55 |-open(("test.txt"),) - 55 |+open(("test.txt"), encoding="locale",) + 55 |+open(("test.txt"), encoding="utf-8",) 56 56 | open( 57 57 | ("test.txt"), # comment 58 58 | ) @@ -266,7 +266,7 @@ unspecified_encoding.py:56:1: PLW1514 [*] `open` in text mode without explicit ` 55 55 | open(("test.txt"),) 56 56 | open( 57 |- ("test.txt"), # comment - 57 |+ ("test.txt"), encoding="locale", # comment + 57 |+ ("test.txt"), encoding="utf-8", # comment 58 58 | ) 59 59 | open( 60 60 | ("test.txt"), @@ -287,7 +287,7 @@ unspecified_encoding.py:59:1: PLW1514 [*] `open` in text mode without explicit ` 58 58 | ) 59 59 | open( 60 |- ("test.txt"), - 60 |+ ("test.txt"), encoding="locale", + 60 |+ ("test.txt"), encoding="utf-8", 61 61 | # comment 62 62 | ) 63 63 | @@ -308,7 +308,7 @@ unspecified_encoding.py:64:1: PLW1514 [*] `open` in text mode without explicit ` 62 62 | ) 63 63 | 64 |-open((("test.txt")),) - 64 |+open((("test.txt")), encoding="locale",) + 64 |+open((("test.txt")), encoding="utf-8",) 65 65 | open( 66 66 | (("test.txt")), # comment 67 67 | ) @@ -328,7 +328,7 @@ unspecified_encoding.py:65:1: PLW1514 [*] `open` in text mode without explicit ` 64 64 | open((("test.txt")),) 65 65 | open( 66 |- (("test.txt")), # comment - 66 |+ (("test.txt")), encoding="locale", # comment + 66 |+ (("test.txt")), encoding="utf-8", # comment 67 67 | ) 68 68 | open( 69 69 | (("test.txt")), @@ -349,7 +349,7 @@ unspecified_encoding.py:68:1: PLW1514 [*] `open` in text mode without explicit ` 67 67 | ) 68 68 | open( 69 |- (("test.txt")), - 69 |+ (("test.txt")), encoding="locale", + 69 |+ (("test.txt")), encoding="utf-8", 70 70 | # comment 71 71 | ) 72 72 | @@ -369,7 +369,7 @@ unspecified_encoding.py:77:1: PLW1514 [*] `pathlib.Path(...).open` in text mode 75 75 | 76 76 | # Errors. 77 |-Path("foo.txt").open() - 77 |+Path("foo.txt").open(encoding="locale") + 77 |+Path("foo.txt").open(encoding="utf-8") 78 78 | Path("foo.txt").open("w") 79 79 | text = Path("foo.txt").read_text() 80 80 | Path("foo.txt").write_text(text) @@ -390,7 +390,7 @@ unspecified_encoding.py:78:1: PLW1514 [*] `pathlib.Path(...).open` in text mode 76 76 | # Errors. 77 77 | Path("foo.txt").open() 78 |-Path("foo.txt").open("w") - 78 |+Path("foo.txt").open("w", encoding="locale") + 78 |+Path("foo.txt").open("w", encoding="utf-8") 79 79 | text = Path("foo.txt").read_text() 80 80 | Path("foo.txt").write_text(text) 81 81 | @@ -410,7 +410,7 @@ unspecified_encoding.py:79:8: PLW1514 [*] `pathlib.Path(...).read_text` without 77 77 | Path("foo.txt").open() 78 78 | Path("foo.txt").open("w") 79 |-text = Path("foo.txt").read_text() - 79 |+text = Path("foo.txt").read_text(encoding="locale") + 79 |+text = Path("foo.txt").read_text(encoding="utf-8") 80 80 | Path("foo.txt").write_text(text) 81 81 | 82 82 | # Non-errors. @@ -431,7 +431,7 @@ unspecified_encoding.py:80:1: PLW1514 [*] `pathlib.Path(...).write_text` without 78 78 | Path("foo.txt").open("w") 79 79 | text = Path("foo.txt").read_text() 80 |-Path("foo.txt").write_text(text) - 80 |+Path("foo.txt").write_text(text, encoding="locale") + 80 |+Path("foo.txt").write_text(text, encoding="utf-8") 81 81 | 82 82 | # Non-errors. 83 83 | Path("foo.txt").open(encoding="utf-8") diff --git a/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__unspecified_encoding_python39_or_lower.snap b/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__unspecified_encoding_python39_or_lower.snap deleted file mode 100644 index f58de408517eb..0000000000000 --- a/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__unspecified_encoding_python39_or_lower.snap +++ /dev/null @@ -1,576 +0,0 @@ ---- -source: crates/ruff_linter/src/rules/pylint/mod.rs ---- -unspecified_encoding.py:8:1: PLW1514 [*] `open` in text mode without explicit `encoding` argument - | - 7 | # Errors. - 8 | open("test.txt") - | ^^^^ PLW1514 - 9 | io.TextIOWrapper(io.FileIO("test.txt")) -10 | hugo.TextIOWrapper(hugo.FileIO("test.txt")) - | - = help: Add explicit `encoding` argument - -ℹ Unsafe fix -3 3 | import tempfile -4 4 | import io as hugo -5 5 | import codecs - 6 |+import locale -6 7 | -7 8 | # Errors. -8 |-open("test.txt") - 9 |+open("test.txt", encoding=locale.getpreferredencoding(False)) -9 10 | io.TextIOWrapper(io.FileIO("test.txt")) -10 11 | hugo.TextIOWrapper(hugo.FileIO("test.txt")) -11 12 | tempfile.NamedTemporaryFile("w") - -unspecified_encoding.py:9:1: PLW1514 [*] `io.TextIOWrapper` without explicit `encoding` argument - | - 7 | # Errors. - 8 | open("test.txt") - 9 | io.TextIOWrapper(io.FileIO("test.txt")) - | ^^^^^^^^^^^^^^^^ PLW1514 -10 | hugo.TextIOWrapper(hugo.FileIO("test.txt")) -11 | tempfile.NamedTemporaryFile("w") - | - = help: Add explicit `encoding` argument - -ℹ Unsafe fix -3 3 | import tempfile -4 4 | import io as hugo -5 5 | import codecs - 6 |+import locale -6 7 | -7 8 | # Errors. -8 9 | open("test.txt") -9 |-io.TextIOWrapper(io.FileIO("test.txt")) - 10 |+io.TextIOWrapper(io.FileIO("test.txt"), encoding=locale.getpreferredencoding(False)) -10 11 | hugo.TextIOWrapper(hugo.FileIO("test.txt")) -11 12 | tempfile.NamedTemporaryFile("w") -12 13 | tempfile.TemporaryFile("w") - -unspecified_encoding.py:10:1: PLW1514 [*] `io.TextIOWrapper` without explicit `encoding` argument - | - 8 | open("test.txt") - 9 | io.TextIOWrapper(io.FileIO("test.txt")) -10 | hugo.TextIOWrapper(hugo.FileIO("test.txt")) - | ^^^^^^^^^^^^^^^^^^ PLW1514 -11 | tempfile.NamedTemporaryFile("w") -12 | tempfile.TemporaryFile("w") - | - = help: Add explicit `encoding` argument - -ℹ Unsafe fix -3 3 | import tempfile -4 4 | import io as hugo -5 5 | import codecs - 6 |+import locale -6 7 | -7 8 | # Errors. -8 9 | open("test.txt") -9 10 | io.TextIOWrapper(io.FileIO("test.txt")) -10 |-hugo.TextIOWrapper(hugo.FileIO("test.txt")) - 11 |+hugo.TextIOWrapper(hugo.FileIO("test.txt"), encoding=locale.getpreferredencoding(False)) -11 12 | tempfile.NamedTemporaryFile("w") -12 13 | tempfile.TemporaryFile("w") -13 14 | codecs.open("test.txt") - -unspecified_encoding.py:11:1: PLW1514 [*] `tempfile.NamedTemporaryFile` in text mode without explicit `encoding` argument - | - 9 | io.TextIOWrapper(io.FileIO("test.txt")) -10 | hugo.TextIOWrapper(hugo.FileIO("test.txt")) -11 | tempfile.NamedTemporaryFile("w") - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ PLW1514 -12 | tempfile.TemporaryFile("w") -13 | codecs.open("test.txt") - | - = help: Add explicit `encoding` argument - -ℹ Unsafe fix -3 3 | import tempfile -4 4 | import io as hugo -5 5 | import codecs - 6 |+import locale -6 7 | -7 8 | # Errors. -8 9 | open("test.txt") -9 10 | io.TextIOWrapper(io.FileIO("test.txt")) -10 11 | hugo.TextIOWrapper(hugo.FileIO("test.txt")) -11 |-tempfile.NamedTemporaryFile("w") - 12 |+tempfile.NamedTemporaryFile("w", encoding=locale.getpreferredencoding(False)) -12 13 | tempfile.TemporaryFile("w") -13 14 | codecs.open("test.txt") -14 15 | tempfile.SpooledTemporaryFile(0, "w") - -unspecified_encoding.py:12:1: PLW1514 [*] `tempfile.TemporaryFile` in text mode without explicit `encoding` argument - | -10 | hugo.TextIOWrapper(hugo.FileIO("test.txt")) -11 | tempfile.NamedTemporaryFile("w") -12 | tempfile.TemporaryFile("w") - | ^^^^^^^^^^^^^^^^^^^^^^ PLW1514 -13 | codecs.open("test.txt") -14 | tempfile.SpooledTemporaryFile(0, "w") - | - = help: Add explicit `encoding` argument - -ℹ Unsafe fix -3 3 | import tempfile -4 4 | import io as hugo -5 5 | import codecs - 6 |+import locale -6 7 | -7 8 | # Errors. -8 9 | open("test.txt") -9 10 | io.TextIOWrapper(io.FileIO("test.txt")) -10 11 | hugo.TextIOWrapper(hugo.FileIO("test.txt")) -11 12 | tempfile.NamedTemporaryFile("w") -12 |-tempfile.TemporaryFile("w") - 13 |+tempfile.TemporaryFile("w", encoding=locale.getpreferredencoding(False)) -13 14 | codecs.open("test.txt") -14 15 | tempfile.SpooledTemporaryFile(0, "w") -15 16 | - -unspecified_encoding.py:13:1: PLW1514 [*] `codecs.open` in text mode without explicit `encoding` argument - | -11 | tempfile.NamedTemporaryFile("w") -12 | tempfile.TemporaryFile("w") -13 | codecs.open("test.txt") - | ^^^^^^^^^^^ PLW1514 -14 | tempfile.SpooledTemporaryFile(0, "w") - | - = help: Add explicit `encoding` argument - -ℹ Unsafe fix -3 3 | import tempfile -4 4 | import io as hugo -5 5 | import codecs - 6 |+import locale -6 7 | -7 8 | # Errors. -8 9 | open("test.txt") --------------------------------------------------------------------------------- -10 11 | hugo.TextIOWrapper(hugo.FileIO("test.txt")) -11 12 | tempfile.NamedTemporaryFile("w") -12 13 | tempfile.TemporaryFile("w") -13 |-codecs.open("test.txt") - 14 |+codecs.open("test.txt", encoding=locale.getpreferredencoding(False)) -14 15 | tempfile.SpooledTemporaryFile(0, "w") -15 16 | -16 17 | # Non-errors. - -unspecified_encoding.py:14:1: PLW1514 [*] `tempfile.SpooledTemporaryFile` in text mode without explicit `encoding` argument - | -12 | tempfile.TemporaryFile("w") -13 | codecs.open("test.txt") -14 | tempfile.SpooledTemporaryFile(0, "w") - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PLW1514 -15 | -16 | # Non-errors. - | - = help: Add explicit `encoding` argument - -ℹ Unsafe fix -3 3 | import tempfile -4 4 | import io as hugo -5 5 | import codecs - 6 |+import locale -6 7 | -7 8 | # Errors. -8 9 | open("test.txt") --------------------------------------------------------------------------------- -11 12 | tempfile.NamedTemporaryFile("w") -12 13 | tempfile.TemporaryFile("w") -13 14 | codecs.open("test.txt") -14 |-tempfile.SpooledTemporaryFile(0, "w") - 15 |+tempfile.SpooledTemporaryFile(0, "w", encoding=locale.getpreferredencoding(False)) -15 16 | -16 17 | # Non-errors. -17 18 | open("test.txt", encoding="utf-8") - -unspecified_encoding.py:46:1: PLW1514 [*] `open` in text mode without explicit `encoding` argument - | -44 | tempfile.SpooledTemporaryFile(0, ) -45 | -46 | open("test.txt",) - | ^^^^ PLW1514 -47 | open() -48 | open( - | - = help: Add explicit `encoding` argument - -ℹ Unsafe fix -3 3 | import tempfile -4 4 | import io as hugo -5 5 | import codecs - 6 |+import locale -6 7 | -7 8 | # Errors. -8 9 | open("test.txt") --------------------------------------------------------------------------------- -43 44 | tempfile.SpooledTemporaryFile(0, "wb") -44 45 | tempfile.SpooledTemporaryFile(0, ) -45 46 | -46 |-open("test.txt",) - 47 |+open("test.txt", encoding=locale.getpreferredencoding(False),) -47 48 | open() -48 49 | open( -49 50 | "test.txt", # comment - -unspecified_encoding.py:47:1: PLW1514 [*] `open` in text mode without explicit `encoding` argument - | -46 | open("test.txt",) -47 | open() - | ^^^^ PLW1514 -48 | open( -49 | "test.txt", # comment - | - = help: Add explicit `encoding` argument - -ℹ Unsafe fix -3 3 | import tempfile -4 4 | import io as hugo -5 5 | import codecs - 6 |+import locale -6 7 | -7 8 | # Errors. -8 9 | open("test.txt") --------------------------------------------------------------------------------- -44 45 | tempfile.SpooledTemporaryFile(0, ) -45 46 | -46 47 | open("test.txt",) -47 |-open() - 48 |+open(encoding=locale.getpreferredencoding(False)) -48 49 | open( -49 50 | "test.txt", # comment -50 51 | ) - -unspecified_encoding.py:48:1: PLW1514 [*] `open` in text mode without explicit `encoding` argument - | -46 | open("test.txt",) -47 | open() -48 | open( - | ^^^^ PLW1514 -49 | "test.txt", # comment -50 | ) - | - = help: Add explicit `encoding` argument - -ℹ Unsafe fix -3 3 | import tempfile -4 4 | import io as hugo -5 5 | import codecs - 6 |+import locale -6 7 | -7 8 | # Errors. -8 9 | open("test.txt") --------------------------------------------------------------------------------- -46 47 | open("test.txt",) -47 48 | open() -48 49 | open( -49 |- "test.txt", # comment - 50 |+ "test.txt", encoding=locale.getpreferredencoding(False), # comment -50 51 | ) -51 52 | open( -52 53 | "test.txt", - -unspecified_encoding.py:51:1: PLW1514 [*] `open` in text mode without explicit `encoding` argument - | -49 | "test.txt", # comment -50 | ) -51 | open( - | ^^^^ PLW1514 -52 | "test.txt", -53 | # comment - | - = help: Add explicit `encoding` argument - -ℹ Unsafe fix -3 3 | import tempfile -4 4 | import io as hugo -5 5 | import codecs - 6 |+import locale -6 7 | -7 8 | # Errors. -8 9 | open("test.txt") --------------------------------------------------------------------------------- -49 50 | "test.txt", # comment -50 51 | ) -51 52 | open( -52 |- "test.txt", - 53 |+ "test.txt", encoding=locale.getpreferredencoding(False), -53 54 | # comment -54 55 | ) -55 56 | open(("test.txt"),) - -unspecified_encoding.py:55:1: PLW1514 [*] `open` in text mode without explicit `encoding` argument - | -53 | # comment -54 | ) -55 | open(("test.txt"),) - | ^^^^ PLW1514 -56 | open( -57 | ("test.txt"), # comment - | - = help: Add explicit `encoding` argument - -ℹ Unsafe fix -3 3 | import tempfile -4 4 | import io as hugo -5 5 | import codecs - 6 |+import locale -6 7 | -7 8 | # Errors. -8 9 | open("test.txt") --------------------------------------------------------------------------------- -52 53 | "test.txt", -53 54 | # comment -54 55 | ) -55 |-open(("test.txt"),) - 56 |+open(("test.txt"), encoding=locale.getpreferredencoding(False),) -56 57 | open( -57 58 | ("test.txt"), # comment -58 59 | ) - -unspecified_encoding.py:56:1: PLW1514 [*] `open` in text mode without explicit `encoding` argument - | -54 | ) -55 | open(("test.txt"),) -56 | open( - | ^^^^ PLW1514 -57 | ("test.txt"), # comment -58 | ) - | - = help: Add explicit `encoding` argument - -ℹ Unsafe fix -3 3 | import tempfile -4 4 | import io as hugo -5 5 | import codecs - 6 |+import locale -6 7 | -7 8 | # Errors. -8 9 | open("test.txt") --------------------------------------------------------------------------------- -54 55 | ) -55 56 | open(("test.txt"),) -56 57 | open( -57 |- ("test.txt"), # comment - 58 |+ ("test.txt"), encoding=locale.getpreferredencoding(False), # comment -58 59 | ) -59 60 | open( -60 61 | ("test.txt"), - -unspecified_encoding.py:59:1: PLW1514 [*] `open` in text mode without explicit `encoding` argument - | -57 | ("test.txt"), # comment -58 | ) -59 | open( - | ^^^^ PLW1514 -60 | ("test.txt"), -61 | # comment - | - = help: Add explicit `encoding` argument - -ℹ Unsafe fix -3 3 | import tempfile -4 4 | import io as hugo -5 5 | import codecs - 6 |+import locale -6 7 | -7 8 | # Errors. -8 9 | open("test.txt") --------------------------------------------------------------------------------- -57 58 | ("test.txt"), # comment -58 59 | ) -59 60 | open( -60 |- ("test.txt"), - 61 |+ ("test.txt"), encoding=locale.getpreferredencoding(False), -61 62 | # comment -62 63 | ) -63 64 | - -unspecified_encoding.py:64:1: PLW1514 [*] `open` in text mode without explicit `encoding` argument - | -62 | ) -63 | -64 | open((("test.txt")),) - | ^^^^ PLW1514 -65 | open( -66 | (("test.txt")), # comment - | - = help: Add explicit `encoding` argument - -ℹ Unsafe fix -3 3 | import tempfile -4 4 | import io as hugo -5 5 | import codecs - 6 |+import locale -6 7 | -7 8 | # Errors. -8 9 | open("test.txt") --------------------------------------------------------------------------------- -61 62 | # comment -62 63 | ) -63 64 | -64 |-open((("test.txt")),) - 65 |+open((("test.txt")), encoding=locale.getpreferredencoding(False),) -65 66 | open( -66 67 | (("test.txt")), # comment -67 68 | ) - -unspecified_encoding.py:65:1: PLW1514 [*] `open` in text mode without explicit `encoding` argument - | -64 | open((("test.txt")),) -65 | open( - | ^^^^ PLW1514 -66 | (("test.txt")), # comment -67 | ) - | - = help: Add explicit `encoding` argument - -ℹ Unsafe fix -3 3 | import tempfile -4 4 | import io as hugo -5 5 | import codecs - 6 |+import locale -6 7 | -7 8 | # Errors. -8 9 | open("test.txt") --------------------------------------------------------------------------------- -63 64 | -64 65 | open((("test.txt")),) -65 66 | open( -66 |- (("test.txt")), # comment - 67 |+ (("test.txt")), encoding=locale.getpreferredencoding(False), # comment -67 68 | ) -68 69 | open( -69 70 | (("test.txt")), - -unspecified_encoding.py:68:1: PLW1514 [*] `open` in text mode without explicit `encoding` argument - | -66 | (("test.txt")), # comment -67 | ) -68 | open( - | ^^^^ PLW1514 -69 | (("test.txt")), -70 | # comment - | - = help: Add explicit `encoding` argument - -ℹ Unsafe fix -3 3 | import tempfile -4 4 | import io as hugo -5 5 | import codecs - 6 |+import locale -6 7 | -7 8 | # Errors. -8 9 | open("test.txt") --------------------------------------------------------------------------------- -66 67 | (("test.txt")), # comment -67 68 | ) -68 69 | open( -69 |- (("test.txt")), - 70 |+ (("test.txt")), encoding=locale.getpreferredencoding(False), -70 71 | # comment -71 72 | ) -72 73 | - -unspecified_encoding.py:77:1: PLW1514 [*] `pathlib.Path(...).open` in text mode without explicit `encoding` argument - | -76 | # Errors. -77 | Path("foo.txt").open() - | ^^^^^^^^^^^^^^^^^^^^ PLW1514 -78 | Path("foo.txt").open("w") -79 | text = Path("foo.txt").read_text() - | - = help: Add explicit `encoding` argument - -ℹ Unsafe fix -72 72 | -73 73 | # pathlib -74 74 | from pathlib import Path - 75 |+import locale -75 76 | -76 77 | # Errors. -77 |-Path("foo.txt").open() - 78 |+Path("foo.txt").open(encoding=locale.getpreferredencoding(False)) -78 79 | Path("foo.txt").open("w") -79 80 | text = Path("foo.txt").read_text() -80 81 | Path("foo.txt").write_text(text) - -unspecified_encoding.py:78:1: PLW1514 [*] `pathlib.Path(...).open` in text mode without explicit `encoding` argument - | -76 | # Errors. -77 | Path("foo.txt").open() -78 | Path("foo.txt").open("w") - | ^^^^^^^^^^^^^^^^^^^^ PLW1514 -79 | text = Path("foo.txt").read_text() -80 | Path("foo.txt").write_text(text) - | - = help: Add explicit `encoding` argument - -ℹ Unsafe fix -72 72 | -73 73 | # pathlib -74 74 | from pathlib import Path - 75 |+import locale -75 76 | -76 77 | # Errors. -77 78 | Path("foo.txt").open() -78 |-Path("foo.txt").open("w") - 79 |+Path("foo.txt").open("w", encoding=locale.getpreferredencoding(False)) -79 80 | text = Path("foo.txt").read_text() -80 81 | Path("foo.txt").write_text(text) -81 82 | - -unspecified_encoding.py:79:8: PLW1514 [*] `pathlib.Path(...).read_text` without explicit `encoding` argument - | -77 | Path("foo.txt").open() -78 | Path("foo.txt").open("w") -79 | text = Path("foo.txt").read_text() - | ^^^^^^^^^^^^^^^^^^^^^^^^^ PLW1514 -80 | Path("foo.txt").write_text(text) - | - = help: Add explicit `encoding` argument - -ℹ Unsafe fix -72 72 | -73 73 | # pathlib -74 74 | from pathlib import Path - 75 |+import locale -75 76 | -76 77 | # Errors. -77 78 | Path("foo.txt").open() -78 79 | Path("foo.txt").open("w") -79 |-text = Path("foo.txt").read_text() - 80 |+text = Path("foo.txt").read_text(encoding=locale.getpreferredencoding(False)) -80 81 | Path("foo.txt").write_text(text) -81 82 | -82 83 | # Non-errors. - -unspecified_encoding.py:80:1: PLW1514 [*] `pathlib.Path(...).write_text` without explicit `encoding` argument - | -78 | Path("foo.txt").open("w") -79 | text = Path("foo.txt").read_text() -80 | Path("foo.txt").write_text(text) - | ^^^^^^^^^^^^^^^^^^^^^^^^^^ PLW1514 -81 | -82 | # Non-errors. - | - = help: Add explicit `encoding` argument - -ℹ Unsafe fix -72 72 | -73 73 | # pathlib -74 74 | from pathlib import Path - 75 |+import locale -75 76 | -76 77 | # Errors. -77 78 | Path("foo.txt").open() -78 79 | Path("foo.txt").open("w") -79 80 | text = Path("foo.txt").read_text() -80 |-Path("foo.txt").write_text(text) - 81 |+Path("foo.txt").write_text(text, encoding=locale.getpreferredencoding(False)) -81 82 | -82 83 | # Non-errors. -83 84 | Path("foo.txt").open(encoding="utf-8")