Skip to content

Commit 13abfec

Browse files
committed
Resolve EncodingWarnings
1 parent 99b04ca commit 13abfec

File tree

5 files changed

+35
-8
lines changed

5 files changed

+35
-8
lines changed

mypy/build.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1194,7 +1194,7 @@ def add_catch_all_gitignore(target_dir: str) -> None:
11941194
"""
11951195
gitignore = os.path.join(target_dir, ".gitignore")
11961196
try:
1197-
with open(gitignore, "x") as f:
1197+
with open(gitignore, "x", encoding="locale") as f:
11981198
print("# Automatically created by mypy", file=f)
11991199
print("*", file=f)
12001200
except FileExistsError:
@@ -1208,7 +1208,7 @@ def exclude_from_backups(target_dir: str) -> None:
12081208
"""
12091209
cachedir_tag = os.path.join(target_dir, "CACHEDIR.TAG")
12101210
try:
1211-
with open(cachedir_tag, "x") as f:
1211+
with open(cachedir_tag, "x", encoding="locale") as f:
12121212
f.write(
12131213
"""Signature: 8a477f597d28d172789f06886806bc55
12141214
# This file is a cache directory tag automatically created by mypy.
@@ -3600,7 +3600,7 @@ def record_missing_stub_packages(cache_dir: str, missing_stub_packages: set[str]
36003600
"""
36013601
fnam = missing_stubs_file(cache_dir)
36023602
if missing_stub_packages:
3603-
with open(fnam, "w") as f:
3603+
with open(fnam, "w", encoding="locale") as f:
36043604
for pkg in sorted(missing_stub_packages):
36053605
f.write(f"{pkg}\n")
36063606
else:

mypy/config_parser.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ def parse_config_file(
236236
parser: MutableMapping[str, Any] = destructure_overrides(toml_data)
237237
config_types = toml_config_types
238238
else:
239-
config_parser.read(config_file)
239+
config_parser.read(config_file, encoding="utf-8")
240240
parser = config_parser
241241
config_types = ini_config_types
242242
except (tomllib.TOMLDecodeError, configparser.Error, ConfigTOMLValueError) as err:

mypy/metastore.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def read(self, name: str) -> str:
9393
if not self.cache_dir_prefix:
9494
raise FileNotFoundError()
9595

96-
with open(os.path.join(self.cache_dir_prefix, name)) as f:
96+
with open(os.path.join(self.cache_dir_prefix, name), encoding="locale") as f:
9797
return f.read()
9898

9999
def write(self, name: str, data: str, mtime: float | None = None) -> bool:
@@ -106,7 +106,7 @@ def write(self, name: str, data: str, mtime: float | None = None) -> bool:
106106
tmp_filename = path + "." + random_string()
107107
try:
108108
os.makedirs(os.path.dirname(path), exist_ok=True)
109-
with open(tmp_filename, "w") as f:
109+
with open(tmp_filename, "w", encoding="locale") as f:
110110
f.write(data)
111111
os.replace(tmp_filename, path)
112112
if mtime is not None:

mypy/modulefinder.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ def _is_compatible_stub_package(self, stub_dir: str) -> bool:
576576
metadata_fnam = os.path.join(stub_dir, "METADATA.toml")
577577
if not os.path.isfile(metadata_fnam):
578578
return True
579-
with open(metadata_fnam, "rb") as f:
579+
with open(metadata_fnam, "rb", encoding="locale") as f:
580580
metadata = tomllib.load(f)
581581
return bool(metadata.get("python3", True))
582582

@@ -866,7 +866,7 @@ def load_stdlib_py_versions(custom_typeshed_dir: str | None) -> StdlibVersions:
866866

867867
versions_path = os.path.join(stdlib_dir, "VERSIONS")
868868
assert os.path.isfile(versions_path), (custom_typeshed_dir, versions_path, __file__)
869-
with open(versions_path) as f:
869+
with open(versions_path, encoding="locale") as f:
870870
for line in f:
871871
line = line.split("#")[0].strip()
872872
if line == "":

mypy/test/testapi.py

+27
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
from __future__ import annotations
22

3+
import shutil
4+
import subprocess
35
import sys
46
from io import StringIO
7+
from pathlib import Path
8+
from tempfile import NamedTemporaryFile, mkdtemp
59

610
import mypy.api
711
from mypy.test.helpers import Suite
@@ -13,12 +17,18 @@ def setUp(self) -> None:
1317
self.sys_stderr = sys.stderr
1418
sys.stdout = self.stdout = StringIO()
1519
sys.stderr = self.stderr = StringIO()
20+
with NamedTemporaryFile(delete=False) as tmp:
21+
tmp.write(b"x: int = 5\n")
22+
self.tmp_path = Path(tmp.name)
23+
self.tmp_cache_dir = Path(mkdtemp())
1624

1725
def tearDown(self) -> None:
1826
sys.stdout = self.sys_stdout
1927
sys.stderr = self.sys_stderr
2028
assert self.stdout.getvalue() == ""
2129
assert self.stderr.getvalue() == ""
30+
self.tmp_path.unlink()
31+
shutil.rmtree(self.tmp_cache_dir)
2232

2333
def test_capture_bad_opt(self) -> None:
2434
"""stderr should be captured when a bad option is passed."""
@@ -43,3 +53,20 @@ def test_capture_version(self) -> None:
4353
stdout, _, _ = mypy.api.run(["--version"])
4454
assert isinstance(stdout, str)
4555
assert stdout != ""
56+
57+
def test_default_encoding_warnings(self) -> None:
58+
"""No EncodingWarnings should be emitted."""
59+
for empty_cache in [True, False]:
60+
res = subprocess.run(
61+
[
62+
sys.executable,
63+
"-c",
64+
"import mypy.api;"
65+
"mypy.api.run("
66+
f"['--cache-dir', '{self.tmp_cache_dir}', '{self.tmp_path}']"
67+
")",
68+
],
69+
capture_output=True,
70+
env={"PYTHONWARNDEFAULTENCODING": "1"},
71+
)
72+
assert b"EncodingWarning" not in res.stderr

0 commit comments

Comments
 (0)