Skip to content

Commit 9c13eb0

Browse files
authored
Enable ruff's unspecified-encoding (PLW1514) rule and fix violations (#3319)
* Enable ruff's unspecified-encoding (PLW1514) rule Xref https://docs.astral.sh/ruff/rules/unspecified-encoding * Fix PLR1514 violations by setting encoding="locale" Default unsafe-fix is to use `encoding="locale"` * Switch from encoding="locale" to encoding="utf-8" PEP0597 hints at UTF-8 becoming the default encoding in the future, so pre-emptively applying it here. Xref https://peps.python.org/pep-0597/#prepare-to-change-the-default-encoding-to-utf-8
1 parent 0f31b7a commit 9c13eb0

File tree

10 files changed

+18
-15
lines changed

10 files changed

+18
-15
lines changed

pygmt/src/plot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ def plot( # noqa: PLR0912
247247
kwargs["S"] = "s0.2c"
248248
elif kind == "file" and str(data).endswith(".gmt"): # OGR_GMT file
249249
try:
250-
with Path(which(data)).open() as file:
250+
with Path(which(data)).open(encoding="utf-8") as file:
251251
line = file.readline()
252252
if "@GMULTIPOINT" in line or "@GPOINT" in line:
253253
kwargs["S"] = "s0.2c"

pygmt/src/plot3d.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ def plot3d( # noqa: PLR0912
223223
kwargs["S"] = "u0.2c"
224224
elif kind == "file" and str(data).endswith(".gmt"): # OGR_GMT file
225225
try:
226-
with Path(which(data)).open() as file:
226+
with Path(which(data)).open(encoding="utf-8") as file:
227227
line = file.readline()
228228
if "@GMULTIPOINT" in line or "@GPOINT" in line:
229229
kwargs["S"] = "u0.2c"

pygmt/tests/test_datatypes_dataset.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def test_dataset():
5757
Test the basic functionality of GMT_DATASET.
5858
"""
5959
with GMTTempFile(suffix=".txt") as tmpfile:
60-
with Path(tmpfile.name).open(mode="w") as fp:
60+
with Path(tmpfile.name).open(mode="w", encoding="utf-8") as fp:
6161
print(">", file=fp)
6262
print("1.0 2.0 3.0 TEXT1 TEXT23", file=fp)
6363
print("4.0 5.0 6.0 TEXT4 TEXT567", file=fp)
@@ -75,7 +75,7 @@ def test_dataset_empty():
7575
Make sure that an empty DataFrame is returned if a file contains no data.
7676
"""
7777
with GMTTempFile(suffix=".txt") as tmpfile:
78-
with Path(tmpfile.name).open(mode="w") as fp:
78+
with Path(tmpfile.name).open(mode="w", encoding="utf-8") as fp:
7979
print("# This is a comment line.", file=fp)
8080

8181
df = dataframe_from_gmt(tmpfile.name)
@@ -89,7 +89,7 @@ def test_dataset_header():
8989
Test parsing column names from dataset header.
9090
"""
9191
with GMTTempFile(suffix=".txt") as tmpfile:
92-
with Path(tmpfile.name).open(mode="w") as fp:
92+
with Path(tmpfile.name).open(mode="w", encoding="utf-8") as fp:
9393
print("# lon lat z text", file=fp)
9494
print("1.0 2.0 3.0 TEXT1 TEXT23", file=fp)
9595
print("4.0 5.0 6.0 TEXT4 TEXT567", file=fp)
@@ -109,7 +109,7 @@ def test_dataset_header_greater_than_nheaders():
109109
Test passing a header line number that is greater than the number of header lines.
110110
"""
111111
with GMTTempFile(suffix=".txt") as tmpfile:
112-
with Path(tmpfile.name).open(mode="w") as fp:
112+
with Path(tmpfile.name).open(mode="w", encoding="utf-8") as fp:
113113
print("# lon lat z text", file=fp)
114114
print("1.0 2.0 3.0 TEXT1 TEXT23", file=fp)
115115
print("4.0 5.0 6.0 TEXT4 TEXT567", file=fp)
@@ -127,7 +127,7 @@ def test_dataset_header_too_many_names():
127127
Test passing a header line with more column names than the number of columns.
128128
"""
129129
with GMTTempFile(suffix=".txt") as tmpfile:
130-
with Path(tmpfile.name).open(mode="w") as fp:
130+
with Path(tmpfile.name).open(mode="w", encoding="utf-8") as fp:
131131
print("# lon lat z text1 text2", file=fp)
132132
print("1.0 2.0 3.0 TEXT1 TEXT23", file=fp)
133133
print("4.0 5.0 6.0 TEXT4 TEXT567", file=fp)

pygmt/tests/test_helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def test_gmttempfile_read():
132132
Make sure GMTTempFile.read() works.
133133
"""
134134
with GMTTempFile() as tmpfile:
135-
Path(tmpfile.name).write_text("in.dat: N = 2\t<1/3>\t<2/4>\n")
135+
Path(tmpfile.name).write_text("in.dat: N = 2\t<1/3>\t<2/4>\n", encoding="utf-8")
136136
assert tmpfile.read() == "in.dat: N = 2 <1/3> <2/4>\n"
137137
assert tmpfile.read(keep_tabs=True) == "in.dat: N = 2\t<1/3>\t<2/4>\n"
138138

pygmt/tests/test_legend.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def test_legend_specfile():
9797
"""
9898

9999
with GMTTempFile() as specfile:
100-
Path(specfile.name).write_text(specfile_contents)
100+
Path(specfile.name).write_text(specfile_contents, encoding="utf-8")
101101
fig = Figure()
102102
fig.basemap(projection="x6i", region=[0, 1, 0, 1], frame=True)
103103
fig.legend(specfile.name, position="JTM+jCM+w5i")

pygmt/tests/test_meca.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def test_meca_spec_single_focalmecha_file():
7474
fig = Figure()
7575
fig.basemap(region=[-1, 1, 4, 6], projection="M8c", frame=2)
7676
with GMTTempFile() as temp:
77-
Path(temp.name).write_text("0 5 0 0 90 0 5")
77+
Path(temp.name).write_text("0 5 0 0 90 0 5", encoding="utf-8")
7878
fig.meca(spec=temp.name, convention="aki", scale="2.5c")
7979
return fig
8080

pygmt/tests/test_plot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ def test_plot_ogrgmt_file_multipoint_default_style(func):
487487
# FEATURE_DATA
488488
1 2
489489
"""
490-
Path(tmpfile.name).write_text(gmt_file)
490+
Path(tmpfile.name).write_text(gmt_file, encoding="utf-8")
491491
fig = Figure()
492492
fig.plot(
493493
data=func(tmpfile.name), region=[0, 2, 1, 3], projection="X2c", frame=True
@@ -506,7 +506,7 @@ def test_plot_ogrgmt_file_multipoint_non_default_style():
506506
# FEATURE_DATA
507507
1 2
508508
"""
509-
Path(tmpfile.name).write_text(gmt_file)
509+
Path(tmpfile.name).write_text(gmt_file, encoding="utf-8")
510510
fig = Figure()
511511
fig.plot(
512512
data=tmpfile.name,

pygmt/tests/test_plot3d.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ def test_plot3d_ogrgmt_file_multipoint_default_style(func):
444444
>
445445
1 1 2
446446
1.5 1.5 1"""
447-
Path(tmpfile.name).write_text(gmt_file)
447+
Path(tmpfile.name).write_text(gmt_file, encoding="utf-8")
448448
fig = Figure()
449449
fig.plot3d(
450450
data=func(tmpfile.name),
@@ -469,7 +469,7 @@ def test_plot3d_ogrgmt_file_multipoint_non_default_style():
469469
>
470470
1 1 2
471471
1.5 1.5 1"""
472-
Path(tmpfile.name).write_text(gmt_file)
472+
Path(tmpfile.name).write_text(gmt_file, encoding="utf-8")
473473
fig = Figure()
474474
fig.plot3d(
475475
data=tmpfile.name,

pygmt/tests/test_text.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,9 @@ def test_text_angle_font_justify_from_textfile():
299299
"""
300300
fig = Figure()
301301
with GMTTempFile(suffix=".txt") as tempfile:
302-
Path(tempfile.name).write_text("114 0.5 30 22p,Helvetica-Bold,black LM BORNEO")
302+
Path(tempfile.name).write_text(
303+
"114 0.5 30 22p,Helvetica-Bold,black LM BORNEO", encoding="utf-8"
304+
)
303305
fig.text(
304306
region=[113, 117.5, -0.5, 3],
305307
projection="M5c",

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ extend-select = [
124124
"D213", # Summary lines should be positioned on the second physical line of the docstring.
125125
"D410", # A blank line after section headings.
126126
"PLR6201", # Use a set literal when testing for membership
127+
"PLW1514", # {function_name} in text mode without explicit encoding argument
127128
]
128129
ignore = [
129130
"D200", # One-line docstring should fit on one line

0 commit comments

Comments
 (0)