Skip to content

Commit cc90345

Browse files
committed
PYTHONWARNDEFAULTENCODING = 1
1 parent 839ef35 commit cc90345

File tree

5 files changed

+36
-54
lines changed

5 files changed

+36
-54
lines changed

CHANGES.md

+3
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@
6969

7070
<!-- For example, Docker, GitHub Actions, pre-commit, editors -->
7171

72+
- Black is now tested with
73+
[`PYTHONWARNDEFAULTENCODING = 1`](https://docs.python.org/3/library/io.html#io-encoding-warning)
74+
(#3658)
7275
- Update GitHub Action to display black output in the job summary (#3688)
7376
- Deprecated `set-output` command in CI test to keep up to date with GitHub's
7477
deprecation announcement (#3757)

tests/test_black.py

+26-45
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,7 @@ def test_empty_ff(self) -> None:
149149
tmp_file = Path(black.dump_to_file())
150150
try:
151151
self.assertFalse(ff(tmp_file, write_back=black.WriteBack.YES))
152-
with open(tmp_file, encoding="utf8") as f:
153-
actual = f.read()
152+
actual = tmp_file.read_text(encoding="utf8")
154153
finally:
155154
os.unlink(tmp_file)
156155
self.assertFormatEqual(expected, actual)
@@ -286,8 +285,7 @@ def test_expression_ff(self) -> None:
286285
tmp_file = Path(black.dump_to_file(source))
287286
try:
288287
self.assertTrue(ff(tmp_file, write_back=black.WriteBack.YES))
289-
with open(tmp_file, encoding="utf8") as f:
290-
actual = f.read()
288+
actual = tmp_file.read_text(encoding="utf8")
291289
finally:
292290
os.unlink(tmp_file)
293291
self.assertFormatEqual(expected, actual)
@@ -390,8 +388,7 @@ def test_skip_source_first_line(self) -> None:
390388
black.main, [str(tmp_file), "-x", f"--config={EMPTY_CONFIG}"]
391389
)
392390
self.assertEqual(result.exit_code, 0)
393-
with open(tmp_file, encoding="utf8") as f:
394-
actual = f.read()
391+
actual = tmp_file.read_text(encoding="utf8")
395392
self.assertFormatEqual(source, actual)
396393

397394
def test_skip_source_first_line_when_mixing_newlines(self) -> None:
@@ -1081,7 +1078,7 @@ def test_works_in_mono_process_only_environment(self) -> None:
10811078
(workspace / "one.py").resolve(),
10821079
(workspace / "two.py").resolve(),
10831080
]:
1084-
f.write_text('print("hello")\n')
1081+
f.write_text('print("hello")\n', encoding="utf-8")
10851082
self.invokeBlack([str(workspace)])
10861083

10871084
@event_loop()
@@ -1118,11 +1115,9 @@ def test_single_file_force_pyi(self) -> None:
11181115
contents, expected = read_data("miscellaneous", "force_pyi")
11191116
with cache_dir() as workspace:
11201117
path = (workspace / "file.py").resolve()
1121-
with open(path, "w") as fh:
1122-
fh.write(contents)
1118+
path.write_text(contents, encoding="utf-8")
11231119
self.invokeBlack([str(path), "--pyi"])
1124-
with open(path, "r") as fh:
1125-
actual = fh.read()
1120+
actual = path.read_text(encoding="utf8")
11261121
# verify cache with --pyi is separate
11271122
pyi_cache = black.read_cache(pyi_mode)
11281123
self.assertIn(str(path), pyi_cache)
@@ -1143,12 +1138,10 @@ def test_multi_file_force_pyi(self) -> None:
11431138
(workspace / "file2.py").resolve(),
11441139
]
11451140
for path in paths:
1146-
with open(path, "w") as fh:
1147-
fh.write(contents)
1141+
path.write_text(contents, encoding="utf-8")
11481142
self.invokeBlack([str(p) for p in paths] + ["--pyi"])
11491143
for path in paths:
1150-
with open(path, "r") as fh:
1151-
actual = fh.read()
1144+
actual = path.read_text(encoding="utf8")
11521145
self.assertEqual(actual, expected)
11531146
# verify cache with --pyi is separate
11541147
pyi_cache = black.read_cache(pyi_mode)
@@ -1172,11 +1165,9 @@ def test_single_file_force_py36(self) -> None:
11721165
source, expected = read_data("miscellaneous", "force_py36")
11731166
with cache_dir() as workspace:
11741167
path = (workspace / "file.py").resolve()
1175-
with open(path, "w") as fh:
1176-
fh.write(source)
1168+
path.write_text(source, encoding="utf-8")
11771169
self.invokeBlack([str(path), *PY36_ARGS])
1178-
with open(path, "r") as fh:
1179-
actual = fh.read()
1170+
actual = path.read_text(encoding="utf8")
11801171
# verify cache with --target-version is separate
11811172
py36_cache = black.read_cache(py36_mode)
11821173
self.assertIn(str(path), py36_cache)
@@ -1195,12 +1186,10 @@ def test_multi_file_force_py36(self) -> None:
11951186
(workspace / "file2.py").resolve(),
11961187
]
11971188
for path in paths:
1198-
with open(path, "w") as fh:
1199-
fh.write(source)
1189+
path.write_text(source, encoding="utf-8")
12001190
self.invokeBlack([str(p) for p in paths] + PY36_ARGS)
12011191
for path in paths:
1202-
with open(path, "r") as fh:
1203-
actual = fh.read()
1192+
actual = path.read_text(encoding="utf8")
12041193
self.assertEqual(actual, expected)
12051194
# verify cache with --target-version is separate
12061195
pyi_cache = black.read_cache(py36_mode)
@@ -1631,8 +1620,8 @@ def test_read_pyproject_toml_from_stdin(self) -> None:
16311620
src_pyproject = src_dir / "pyproject.toml"
16321621
src_pyproject.touch()
16331622

1634-
test_toml_file = THIS_DIR / "test.toml"
1635-
src_pyproject.write_text(test_toml_file.read_text())
1623+
test_toml_content = (THIS_DIR / "test.toml").read_text(encoding="utf-8")
1624+
src_pyproject.write_text(test_toml_content, encoding="utf-8")
16361625

16371626
src_python = src_dir / "foo.py"
16381627
src_python.touch()
@@ -1985,10 +1974,10 @@ def test_cache_broken_file(self) -> None:
19851974
mode = DEFAULT_MODE
19861975
with cache_dir() as workspace:
19871976
cache_file = get_cache_file(mode)
1988-
cache_file.write_text("this is not a pickle")
1977+
cache_file.write_text("this is not a pickle", encoding="utf-8")
19891978
assert black.read_cache(mode) == {}
19901979
src = (workspace / "test.py").resolve()
1991-
src.write_text("print('hello')")
1980+
src.write_text("print('hello')", encoding="utf-8")
19921981
invokeBlack([str(src)])
19931982
cache = black.read_cache(mode)
19941983
assert str(src) in cache
@@ -1997,10 +1986,10 @@ def test_cache_single_file_already_cached(self) -> None:
19971986
mode = DEFAULT_MODE
19981987
with cache_dir() as workspace:
19991988
src = (workspace / "test.py").resolve()
2000-
src.write_text("print('hello')")
1989+
src.write_text("print('hello')", encoding="utf-8")
20011990
black.write_cache({}, [src], mode)
20021991
invokeBlack([str(src)])
2003-
assert src.read_text() == "print('hello')"
1992+
assert src.read_text(encoding="utf-8") == "print('hello')"
20041993

20051994
@event_loop()
20061995
def test_cache_multiple_files(self) -> None:
@@ -2009,17 +1998,13 @@ def test_cache_multiple_files(self) -> None:
20091998
"concurrent.futures.ProcessPoolExecutor", new=ThreadPoolExecutor
20101999
):
20112000
one = (workspace / "one.py").resolve()
2012-
with one.open("w") as fobj:
2013-
fobj.write("print('hello')")
2001+
one.write_text("print('hello')", encoding="utf-8")
20142002
two = (workspace / "two.py").resolve()
2015-
with two.open("w") as fobj:
2016-
fobj.write("print('hello')")
2003+
two.write_text("print('hello')", encoding="utf-8")
20172004
black.write_cache({}, [one], mode)
20182005
invokeBlack([str(workspace)])
2019-
with one.open("r") as fobj:
2020-
assert fobj.read() == "print('hello')"
2021-
with two.open("r") as fobj:
2022-
assert fobj.read() == 'print("hello")\n'
2006+
assert one.read_text(encoding="utf-8") == "print('hello')"
2007+
assert two.read_text(encoding="utf-8") == 'print("hello")\n'
20232008
cache = black.read_cache(mode)
20242009
assert str(one) in cache
20252010
assert str(two) in cache
@@ -2029,8 +2014,7 @@ def test_no_cache_when_writeback_diff(self, color: bool) -> None:
20292014
mode = DEFAULT_MODE
20302015
with cache_dir() as workspace:
20312016
src = (workspace / "test.py").resolve()
2032-
with src.open("w") as fobj:
2033-
fobj.write("print('hello')")
2017+
src.write_text("print('hello')", encoding="utf-8")
20342018
with patch("black.read_cache") as read_cache, patch(
20352019
"black.write_cache"
20362020
) as write_cache:
@@ -2049,8 +2033,7 @@ def test_output_locking_when_writeback_diff(self, color: bool) -> None:
20492033
with cache_dir() as workspace:
20502034
for tag in range(0, 4):
20512035
src = (workspace / f"test{tag}.py").resolve()
2052-
with src.open("w") as fobj:
2053-
fobj.write("print('hello')")
2036+
src.write_text("print('hello')", encoding="utf-8")
20542037
with patch(
20552038
"black.concurrency.Manager", wraps=multiprocessing.Manager
20562039
) as mgr:
@@ -2120,11 +2103,9 @@ def test_failed_formatting_does_not_get_cached(self) -> None:
21202103
"concurrent.futures.ProcessPoolExecutor", new=ThreadPoolExecutor
21212104
):
21222105
failing = (workspace / "failing.py").resolve()
2123-
with failing.open("w") as fobj:
2124-
fobj.write("not actually python")
2106+
failing.write_text("not actually python", encoding="utf-8")
21252107
clean = (workspace / "clean.py").resolve()
2126-
with clean.open("w") as fobj:
2127-
fobj.write('print("hello")\n')
2108+
clean.write_text('print("hello")\n', encoding="utf-8")
21282109
invokeBlack([str(workspace)], exit_code=123)
21292110
cache = black.read_cache(mode)
21302111
assert str(failing) not in cache

tests/test_ipynb.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -439,8 +439,7 @@ def test_cache_isnt_written_if_no_jupyter_deps_single(
439439
jupyter_dependencies_are_installed.cache_clear()
440440
nb = get_case_path("jupyter", "notebook_trailing_newline.ipynb")
441441
tmp_nb = tmp_path / "notebook.ipynb"
442-
with open(nb) as src, open(tmp_nb, "w") as dst:
443-
dst.write(src.read())
442+
tmp_nb.write_bytes(nb.read_bytes())
444443
monkeypatch.setattr(
445444
"black.jupyter_dependencies_are_installed", lambda verbose, quiet: False
446445
)
@@ -465,8 +464,7 @@ def test_cache_isnt_written_if_no_jupyter_deps_dir(
465464
jupyter_dependencies_are_installed.cache_clear()
466465
nb = get_case_path("jupyter", "notebook_trailing_newline.ipynb")
467466
tmp_nb = tmp_path / "notebook.ipynb"
468-
with open(nb) as src, open(tmp_nb, "w") as dst:
469-
dst.write(src.read())
467+
tmp_nb.write_bytes(nb.read_bytes())
470468
monkeypatch.setattr(
471469
"black.files.jupyter_dependencies_are_installed", lambda verbose, quiet: False
472470
)
@@ -483,8 +481,7 @@ def test_cache_isnt_written_if_no_jupyter_deps_dir(
483481
def test_ipynb_flag(tmp_path: pathlib.Path) -> None:
484482
nb = get_case_path("jupyter", "notebook_trailing_newline.ipynb")
485483
tmp_nb = tmp_path / "notebook.a_file_extension_which_is_definitely_not_ipynb"
486-
with open(nb) as src, open(tmp_nb, "w") as dst:
487-
dst.write(src.read())
484+
tmp_nb.write_bytes(nb.read_bytes())
488485
result = runner.invoke(
489486
main,
490487
[

tests/test_no_ipynb.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ def test_ipynb_diff_with_no_change_dir(tmp_path: pathlib.Path) -> None:
2727
runner = CliRunner()
2828
nb = get_case_path("jupyter", "notebook_trailing_newline.ipynb")
2929
tmp_nb = tmp_path / "notebook.ipynb"
30-
with open(nb) as src, open(tmp_nb, "w") as dst:
31-
dst.write(src.read())
30+
tmp_nb.write_bytes(nb.read_bytes())
3231
result = runner.invoke(main, [str(tmp_path)])
3332
expected_output = (
3433
"Skipping .ipynb files as Jupyter dependencies are not installed.\n"

tox.ini

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ isolated_build = true
33
envlist = {,ci-}py{37,38,39,310,311,py3},fuzz,run_self
44

55
[testenv]
6-
setenv = PYTHONPATH = {toxinidir}/src
6+
setenv =
7+
PYTHONPATH = {toxinidir}/src
8+
PYTHONWARNDEFAULTENCODING = 1
79
skip_install = True
810
# We use `recreate=True` because otherwise, on the second run of `tox -e py`,
911
# the `no_jupyter` tests would run with the jupyter extra dependencies installed.

0 commit comments

Comments
 (0)