Skip to content

Commit 87970b2

Browse files
authored
STYLE: specify encodings when opening files (#52999)
* Changes Confirmed * Encoding Completed * Spaces Are Completed * Pre-ccommit manually completed * b.pre-commit removed * Final Commit * Some Changed reflected * test_xml Updated * Pre-commit check passed * Mode changed in xml file * mode reverted * Try to fix errors * error-checks * Fix Some errors * Unspecified-encodingFixed * final commited * simplify --------- Co-authored-by: MarcoGorelli <>
1 parent 2cdca01 commit 87970b2

39 files changed

+113
-92
lines changed

.pre-commit-config.yaml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,6 @@ repos:
8383
hooks:
8484
- id: pylint
8585
stages: [manual]
86-
- repo: https://github.com/pycqa/pylint
87-
rev: v2.16.2
88-
hooks:
8986
- id: pylint
9087
alias: redefined-outer-name
9188
name: Redefining name from outer scope
@@ -99,6 +96,11 @@ repos:
9996
|^pandas/conftest\.py # keep excluded
10097
args: [--disable=all, --enable=redefined-outer-name]
10198
stages: [manual]
99+
- id: pylint
100+
alias: unspecified-encoding
101+
name: Using open without explicitly specifying an encoding
102+
args: [--disable=all, --enable=unspecified-encoding]
103+
stages: [manual]
102104
- repo: https://github.com/PyCQA/isort
103105
rev: 5.12.0
104106
hooks:

asv_bench/benchmarks/io/csv.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ class ReadCSVMemoryGrowth(BaseIO):
497497
param_names = ["engine"]
498498

499499
def setup(self, engine):
500-
with open(self.fname, "w") as f:
500+
with open(self.fname, "w", encoding="utf-8") as f:
501501
for i in range(self.num_rows):
502502
f.write(f"{i}\n")
503503

doc/make.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,12 @@ def _get_page_title(self, page):
163163
components=(docutils.parsers.rst.Parser,)
164164
)
165165
doc = docutils.utils.new_document("<doc>", option_parser.get_default_values())
166-
with open(fname) as f:
166+
with open(fname, encoding="utf-8") as f:
167167
data = f.read()
168168

169169
parser = docutils.parsers.rst.Parser()
170170
# do not generate any warning when parsing the rst
171-
with open(os.devnull, "a") as f:
171+
with open(os.devnull, "a", encoding="utf-8") as f:
172172
doc.reporter.stream = f
173173
parser.parse(data, doc)
174174

@@ -186,7 +186,7 @@ def _add_redirects(self):
186186
Create in the build directory an html file with a redirect,
187187
for every row in REDIRECTS_FILE.
188188
"""
189-
with open(REDIRECTS_FILE) as mapping_fd:
189+
with open(REDIRECTS_FILE, encoding="utf-8") as mapping_fd:
190190
reader = csv.reader(mapping_fd)
191191
for row in reader:
192192
if not row or row[0].strip().startswith("#"):
@@ -209,7 +209,7 @@ def _add_redirects(self):
209209
# sphinx specific stuff
210210
title = "this page"
211211

212-
with open(path, "w") as moved_page_fd:
212+
with open(path, "w", encoding="utf-8") as moved_page_fd:
213213
html = f"""\
214214
<html>
215215
<head>

doc/source/conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,9 @@
117117
elif single_doc and rel_fname != pattern:
118118
exclude_patterns.append(rel_fname)
119119

120-
with open(os.path.join(source_path, "index.rst.template")) as f:
120+
with open(os.path.join(source_path, "index.rst.template"), encoding="utf-8") as f:
121121
t = jinja2.Template(f.read())
122-
with open(os.path.join(source_path, "index.rst"), "w") as f:
122+
with open(os.path.join(source_path, "index.rst"), "w", encoding="utf-8") as f:
123123
f.write(
124124
t.render(
125125
include_api=include_api,

pandas/_testing/contexts.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,10 @@ def ensure_clean(
124124
path.touch()
125125

126126
handle_or_str: str | IO = str(path)
127+
encoding = kwargs.pop("encoding", None)
127128
if return_filelike:
128129
kwargs.setdefault("mode", "w+b")
129-
handle_or_str = open(path, **kwargs)
130+
handle_or_str = open(path, encoding=encoding, **kwargs)
130131

131132
try:
132133
yield handle_or_str

pandas/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ def git_get_keywords(versionfile_abs):
159159
# _version.py.
160160
keywords = {}
161161
try:
162-
with open(versionfile_abs) as fobj:
162+
with open(versionfile_abs, encoding="utf-8") as fobj:
163163
for line in fobj:
164164
if line.strip().startswith("git_refnames ="):
165165
mo = re.search(r'=\s*"(.*)"', line)

pandas/core/series.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1699,7 +1699,7 @@ def to_string(
16991699
if hasattr(buf, "write"):
17001700
buf.write(result)
17011701
else:
1702-
with open(buf, "w") as f:
1702+
with open(buf, "w", encoding="utf-8") as f:
17031703
f.write(result)
17041704
return None
17051705

pandas/io/clipboard/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,11 +282,11 @@ def copy_dev_clipboard(text):
282282
stacklevel=find_stack_level(),
283283
)
284284

285-
with open("/dev/clipboard", "w") as fd:
285+
with open("/dev/clipboard", "w", encoding="utf-8") as fd:
286286
fd.write(text)
287287

288288
def paste_dev_clipboard() -> str:
289-
with open("/dev/clipboard") as fd:
289+
with open("/dev/clipboard", encoding="utf-8") as fd:
290290
content = fd.read()
291291
return content
292292

pandas/tests/frame/methods/test_to_csv.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ def test_to_csv_float32_nanrep(self):
626626
with tm.ensure_clean("__tmp_to_csv_float32_nanrep__.csv") as path:
627627
df.to_csv(path, na_rep=999)
628628

629-
with open(path) as f:
629+
with open(path, encoding="utf-8") as f:
630630
lines = f.readlines()
631631
assert lines[1].split(",")[2] == "999"
632632

pandas/tests/io/excel/test_readers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1708,7 +1708,7 @@ def test_corrupt_files_closed(self, engine, read_ext):
17081708
errors = (BadZipFile, xlrd.biffh.XLRDError)
17091709

17101710
with tm.ensure_clean(f"corrupt{read_ext}") as file:
1711-
Path(file).write_text("corrupt")
1711+
Path(file).write_text("corrupt", encoding="utf-8")
17121712
with tm.assert_produces_warning(False):
17131713
try:
17141714
pd.ExcelFile(file, engine=engine)

0 commit comments

Comments
 (0)