Skip to content

Commit 573ee94

Browse files
authored
All scripts/tests: always specify file encoding in calls to open() (#8882)
1 parent 5ca2d77 commit 573ee94

File tree

9 files changed

+18
-18
lines changed

9 files changed

+18
-18
lines changed

scripts/create_baseline_stubs.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def create_metadata(stub_dir: str, version: str) -> None:
7070
if os.path.exists(filename):
7171
return
7272
print(f"Writing {filename}")
73-
with open(filename, "w") as file:
73+
with open(filename, "w", encoding="UTF-8") as file:
7474
file.write(
7575
f"""\
7676
version = "{version}.*"
@@ -83,7 +83,7 @@ def create_metadata(stub_dir: str, version: str) -> None:
8383

8484
def add_pyright_exclusion(stub_dir: str) -> None:
8585
"""Exclude stub_dir from strict pyright checks."""
86-
with open(PYRIGHT_CONFIG) as f:
86+
with open(PYRIGHT_CONFIG, encoding="UTF-8") as f:
8787
lines = f.readlines()
8888
i = 0
8989
while i < len(lines) and not lines[i].strip().startswith('"exclude": ['):
@@ -105,7 +105,7 @@ def add_pyright_exclusion(stub_dir: str) -> None:
105105
lines[i] = lines[i].rstrip() + ",\n"
106106
lines.insert(i + 1, line_to_add + "\n")
107107
print(f"Updating {PYRIGHT_CONFIG}")
108-
with open(PYRIGHT_CONFIG, "w") as f:
108+
with open(PYRIGHT_CONFIG, "w", encoding="UTF-8") as f:
109109
f.writelines(lines)
110110

111111

scripts/runtests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def _parse_jsonc(json_text: str) -> str:
3333

3434

3535
def _get_strict_params(stub_path: str) -> list[str]:
36-
with open(_STRICTER_CONFIG_FILE) as file:
36+
with open(_STRICTER_CONFIG_FILE, encoding="UTF-8") as file:
3737
data = json.loads(_parse_jsonc(file.read()))
3838
if stub_path in data["exclude"]:
3939
return []

scripts/stubsabot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ async def suggest_typeshed_update(update: Update, session: aiohttp.ClientSession
610610
with open(update.stub_path / "METADATA.toml", "rb") as f:
611611
meta = tomlkit.load(f)
612612
meta["version"] = update.new_version_spec
613-
with open(update.stub_path / "METADATA.toml", "w") as f:
613+
with open(update.stub_path / "METADATA.toml", "w", encoding="UTF-8") as f:
614614
tomlkit.dump(meta, f)
615615
body = get_update_pr_body(update, meta)
616616
subprocess.check_call(["git", "commit", "--all", "-m", f"{title}\n\n{body}"])
@@ -638,7 +638,7 @@ async def suggest_typeshed_obsolete(obsolete: Obsolete, session: aiohttp.ClientS
638638
obs_string = tomlkit.string(obsolete.obsolete_since_version)
639639
obs_string.comment(f"Released on {obsolete.obsolete_since_date.date().isoformat()}")
640640
meta["obsolete_since"] = obs_string
641-
with open(obsolete.stub_path / "METADATA.toml", "w") as f:
641+
with open(obsolete.stub_path / "METADATA.toml", "w", encoding="UTF-8") as f:
642642
tomlkit.dump(meta, f)
643643
body = "\n".join(f"{k}: {v}" for k, v in obsolete.links.items())
644644
subprocess.check_call(["git", "commit", "--all", "-m", f"{title}\n\n{body}"])

stubs/requests/@tests/test_cases/check_post.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ def gen() -> Iterable[bytes]:
4444
requests.post("http://httpbin.org/anything", data="foobar").json()["data"]
4545

4646
# Files
47-
requests.post("http://httpbin.org/anything", data=open("/tmp/foobar", "rb")).json()["data"]
48-
requests.post("http://httpbin.org/anything", data=open("/tmp/foobar", "r")).json()["data"]
47+
requests.post("http://httpbin.org/anything", data=open("/tmp/foobar", "rb", encoding="UTF-8")).json()["data"]
48+
requests.post("http://httpbin.org/anything", data=open("/tmp/foobar", "r", encoding="UTF-8")).json()["data"]
4949

5050
# Mappings
5151
requests.post("http://httpbin.org/anything", data={b"foo": b"bar"}).json()["form"]

tests/check_consistent.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def check_test_cases() -> None:
7171
for file in testcase_dir.rglob("*.py"):
7272
assert file.stem.startswith("check_"), bad_test_case_filename.format(file)
7373
if package_name != "stdlib":
74-
with open(file) as f:
74+
with open(file, encoding="UTF-8") as f:
7575
lines = {line.strip() for line in f}
7676
pyright_setting_not_enabled_msg = (
7777
f'Third-party test-case file "{file}" must have '
@@ -93,7 +93,7 @@ def check_no_symlinks() -> None:
9393

9494
def check_versions() -> None:
9595
versions = set()
96-
with open("stdlib/VERSIONS") as f:
96+
with open("stdlib/VERSIONS", encoding="UTF-8") as f:
9797
data = f.read().splitlines()
9898
for line in data:
9999
line = strip_comments(line)
@@ -128,7 +128,7 @@ def _find_stdlib_modules() -> set[str]:
128128

129129
def check_metadata() -> None:
130130
for distribution in os.listdir("stubs"):
131-
with open(os.path.join("stubs", distribution, "METADATA.toml")) as f:
131+
with open(os.path.join("stubs", distribution, "METADATA.toml"), encoding="UTF-8") as f:
132132
data = tomli.loads(f.read())
133133
assert "version" in data, f"Missing version for {distribution}"
134134
version = data["version"]
@@ -153,14 +153,14 @@ def check_metadata() -> None:
153153

154154

155155
def get_txt_requirements() -> dict[str, SpecifierSet]:
156-
with open("requirements-tests.txt") as requirements_file:
156+
with open("requirements-tests.txt", encoding="UTF-8") as requirements_file:
157157
stripped_lines = map(strip_comments, requirements_file)
158158
requirements = map(Requirement, filter(None, stripped_lines))
159159
return {requirement.name: requirement.specifier for requirement in requirements}
160160

161161

162162
def get_precommit_requirements() -> dict[str, SpecifierSet]:
163-
with open(".pre-commit-config.yaml") as precommit_file:
163+
with open(".pre-commit-config.yaml", encoding="UTF-8") as precommit_file:
164164
precommit = precommit_file.read()
165165
yam = yaml.load(precommit, Loader=yaml.Loader)
166166
precommit_requirements = {}

tests/check_new_syntax.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def visit_If(self, node: ast.If) -> None:
9696
def main() -> None:
9797
errors = []
9898
for path in chain(Path("stdlib").rglob("*.pyi"), Path("stubs").rglob("*.pyi")):
99-
with open(path) as f:
99+
with open(path, encoding="UTF-8") as f:
100100
stub = f.read()
101101
tree = ast.parse(stub)
102102
errors.extend(check_new_syntax(tree, path, stub))

tests/mypy_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def match(path: Path, args: TestConfig) -> bool:
129129

130130
def parse_versions(fname: StrPath) -> dict[str, tuple[VersionTuple, VersionTuple]]:
131131
result = {}
132-
with open(fname) as f:
132+
with open(fname, encoding="UTF-8") as f:
133133
for line in f:
134134
line = strip_comments(line)
135135
if line == "":

tests/stubtest_third_party.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919

2020
@functools.lru_cache()
2121
def get_mypy_req() -> str:
22-
with open("requirements-tests.txt") as f:
22+
with open("requirements-tests.txt", encoding="UTF-8") as f:
2323
return next(line.strip() for line in f if "mypy" in line)
2424

2525

2626
def run_stubtest(dist: Path, *, verbose: bool = False) -> bool:
27-
with open(dist / "METADATA.toml") as f:
27+
with open(dist / "METADATA.toml", encoding="UTF-8") as f:
2828
metadata = dict(tomli.loads(f.read()))
2929

3030
print(f"{dist.name}... ", end="")

tests/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def get_all_testcase_directories() -> list[PackageInfo]:
101101

102102
@cache
103103
def get_gitignore_spec() -> pathspec.PathSpec:
104-
with open(".gitignore") as f:
104+
with open(".gitignore", encoding="UTF-8") as f:
105105
return pathspec.PathSpec.from_lines("gitwildmatch", f.readlines())
106106

107107

0 commit comments

Comments
 (0)