Skip to content

Resolve EncodingWarnings #14676

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions mypy/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import errno
import gc
import json
import locale
import os
import platform
import re
Expand Down Expand Up @@ -1194,7 +1195,7 @@ def add_catch_all_gitignore(target_dir: str) -> None:
"""
gitignore = os.path.join(target_dir, ".gitignore")
try:
with open(gitignore, "x") as f:
with open(gitignore, "x", encoding=locale.getpreferredencoding(False)) as f:
print("# Automatically created by mypy", file=f)
print("*", file=f)
except FileExistsError:
Expand All @@ -1208,7 +1209,7 @@ def exclude_from_backups(target_dir: str) -> None:
"""
cachedir_tag = os.path.join(target_dir, "CACHEDIR.TAG")
try:
with open(cachedir_tag, "x") as f:
with open(cachedir_tag, "x", encoding=locale.getpreferredencoding(False)) as f:
f.write(
"""Signature: 8a477f597d28d172789f06886806bc55
# This file is a cache directory tag automatically created by mypy.
Expand Down Expand Up @@ -3600,7 +3601,7 @@ def record_missing_stub_packages(cache_dir: str, missing_stub_packages: set[str]
"""
fnam = missing_stubs_file(cache_dir)
if missing_stub_packages:
with open(fnam, "w") as f:
with open(fnam, "w", encoding=locale.getpreferredencoding(False)) as f:
for pkg in sorted(missing_stub_packages):
f.write(f"{pkg}\n")
else:
Expand Down
3 changes: 2 additions & 1 deletion mypy/config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import argparse
import configparser
import glob as fileglob
import locale
import os
import re
import sys
Expand Down Expand Up @@ -236,7 +237,7 @@ def parse_config_file(
parser: MutableMapping[str, Any] = destructure_overrides(toml_data)
config_types = toml_config_types
else:
config_parser.read(config_file)
config_parser.read(config_file, encoding=locale.getpreferredencoding(False))
parser = config_parser
config_types = ini_config_types
except (tomllib.TOMLDecodeError, configparser.Error, ConfigTOMLValueError) as err:
Expand Down
7 changes: 5 additions & 2 deletions mypy/metastore.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from __future__ import annotations

import binascii
import locale
import os
import time
from abc import abstractmethod
Expand Down Expand Up @@ -93,7 +94,9 @@ def read(self, name: str) -> str:
if not self.cache_dir_prefix:
raise FileNotFoundError()

with open(os.path.join(self.cache_dir_prefix, name)) as f:
with open(
os.path.join(self.cache_dir_prefix, name), encoding=locale.getpreferredencoding(False)
) as f:
return f.read()

def write(self, name: str, data: str, mtime: float | None = None) -> bool:
Expand All @@ -106,7 +109,7 @@ def write(self, name: str, data: str, mtime: float | None = None) -> bool:
tmp_filename = path + "." + random_string()
try:
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(tmp_filename, "w") as f:
with open(tmp_filename, "w", encoding=locale.getpreferredencoding(False)) as f:
f.write(data)
os.replace(tmp_filename, path)
if mtime is not None:
Expand Down
3 changes: 2 additions & 1 deletion mypy/modulefinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import ast
import collections
import functools
import locale
import os
import re
import subprocess
Expand Down Expand Up @@ -866,7 +867,7 @@ def load_stdlib_py_versions(custom_typeshed_dir: str | None) -> StdlibVersions:

versions_path = os.path.join(stdlib_dir, "VERSIONS")
assert os.path.isfile(versions_path), (custom_typeshed_dir, versions_path, __file__)
with open(versions_path) as f:
with open(versions_path, encoding=locale.getpreferredencoding(False)) as f:
for line in f:
line = line.split("#")[0].strip()
if line == "":
Expand Down
27 changes: 27 additions & 0 deletions mypy/test/testapi.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
from __future__ import annotations

import shutil
import subprocess
import sys
from io import StringIO
from pathlib import Path
from tempfile import NamedTemporaryFile, mkdtemp

import mypy.api
from mypy.test.helpers import Suite
Expand All @@ -13,12 +17,18 @@ def setUp(self) -> None:
self.sys_stderr = sys.stderr
sys.stdout = self.stdout = StringIO()
sys.stderr = self.stderr = StringIO()
with NamedTemporaryFile(delete=False) as tmp:
tmp.write(b"x: int = 5\n")
self.tmp_path = Path(tmp.name)
self.tmp_cache_dir = Path(mkdtemp())
Comment on lines +20 to +23
Copy link
Collaborator

@A5rocks A5rocks Feb 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will get run before every test in this test suite. Should this get moved into test_default_encoding_warnings?


def tearDown(self) -> None:
sys.stdout = self.sys_stdout
sys.stderr = self.sys_stderr
assert self.stdout.getvalue() == ""
assert self.stderr.getvalue() == ""
self.tmp_path.unlink()
shutil.rmtree(self.tmp_cache_dir)

def test_capture_bad_opt(self) -> None:
"""stderr should be captured when a bad option is passed."""
Expand All @@ -43,3 +53,20 @@ def test_capture_version(self) -> None:
stdout, _, _ = mypy.api.run(["--version"])
assert isinstance(stdout, str)
assert stdout != ""

def test_default_encoding_warnings(self) -> None:
"""No EncodingWarnings should be emitted."""
for empty_cache in [True, False]:
res = subprocess.run(
[
sys.executable,
"-c",
"import mypy.api;"
"mypy.api.run("
f"['--cache-dir', '{self.tmp_cache_dir}', '{self.tmp_path}']"
")",
],
capture_output=True,
env={"PYTHONWARNDEFAULTENCODING": "1"},
)
assert b"EncodingWarning" not in res.stderr