Skip to content

Commit

Permalink
Enable ruff's unspecified-encoding (PLW1514) rule and fix violations (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
weiji14 authored Jul 8, 2024
1 parent 0f31b7a commit 9c13eb0
Show file tree
Hide file tree
Showing 10 changed files with 18 additions and 15 deletions.
2 changes: 1 addition & 1 deletion pygmt/src/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ def plot( # noqa: PLR0912
kwargs["S"] = "s0.2c"
elif kind == "file" and str(data).endswith(".gmt"): # OGR_GMT file
try:
with Path(which(data)).open() as file:
with Path(which(data)).open(encoding="utf-8") as file:
line = file.readline()
if "@GMULTIPOINT" in line or "@GPOINT" in line:
kwargs["S"] = "s0.2c"
Expand Down
2 changes: 1 addition & 1 deletion pygmt/src/plot3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def plot3d( # noqa: PLR0912
kwargs["S"] = "u0.2c"
elif kind == "file" and str(data).endswith(".gmt"): # OGR_GMT file
try:
with Path(which(data)).open() as file:
with Path(which(data)).open(encoding="utf-8") as file:
line = file.readline()
if "@GMULTIPOINT" in line or "@GPOINT" in line:
kwargs["S"] = "u0.2c"
Expand Down
10 changes: 5 additions & 5 deletions pygmt/tests/test_datatypes_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def test_dataset():
Test the basic functionality of GMT_DATASET.
"""
with GMTTempFile(suffix=".txt") as tmpfile:
with Path(tmpfile.name).open(mode="w") as fp:
with Path(tmpfile.name).open(mode="w", encoding="utf-8") as fp:
print(">", file=fp)
print("1.0 2.0 3.0 TEXT1 TEXT23", file=fp)
print("4.0 5.0 6.0 TEXT4 TEXT567", file=fp)
Expand All @@ -75,7 +75,7 @@ def test_dataset_empty():
Make sure that an empty DataFrame is returned if a file contains no data.
"""
with GMTTempFile(suffix=".txt") as tmpfile:
with Path(tmpfile.name).open(mode="w") as fp:
with Path(tmpfile.name).open(mode="w", encoding="utf-8") as fp:
print("# This is a comment line.", file=fp)

df = dataframe_from_gmt(tmpfile.name)
Expand All @@ -89,7 +89,7 @@ def test_dataset_header():
Test parsing column names from dataset header.
"""
with GMTTempFile(suffix=".txt") as tmpfile:
with Path(tmpfile.name).open(mode="w") as fp:
with Path(tmpfile.name).open(mode="w", encoding="utf-8") as fp:
print("# lon lat z text", file=fp)
print("1.0 2.0 3.0 TEXT1 TEXT23", file=fp)
print("4.0 5.0 6.0 TEXT4 TEXT567", file=fp)
Expand All @@ -109,7 +109,7 @@ def test_dataset_header_greater_than_nheaders():
Test passing a header line number that is greater than the number of header lines.
"""
with GMTTempFile(suffix=".txt") as tmpfile:
with Path(tmpfile.name).open(mode="w") as fp:
with Path(tmpfile.name).open(mode="w", encoding="utf-8") as fp:
print("# lon lat z text", file=fp)
print("1.0 2.0 3.0 TEXT1 TEXT23", file=fp)
print("4.0 5.0 6.0 TEXT4 TEXT567", file=fp)
Expand All @@ -127,7 +127,7 @@ def test_dataset_header_too_many_names():
Test passing a header line with more column names than the number of columns.
"""
with GMTTempFile(suffix=".txt") as tmpfile:
with Path(tmpfile.name).open(mode="w") as fp:
with Path(tmpfile.name).open(mode="w", encoding="utf-8") as fp:
print("# lon lat z text1 text2", file=fp)
print("1.0 2.0 3.0 TEXT1 TEXT23", file=fp)
print("4.0 5.0 6.0 TEXT4 TEXT567", file=fp)
Expand Down
2 changes: 1 addition & 1 deletion pygmt/tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def test_gmttempfile_read():
Make sure GMTTempFile.read() works.
"""
with GMTTempFile() as tmpfile:
Path(tmpfile.name).write_text("in.dat: N = 2\t<1/3>\t<2/4>\n")
Path(tmpfile.name).write_text("in.dat: N = 2\t<1/3>\t<2/4>\n", encoding="utf-8")
assert tmpfile.read() == "in.dat: N = 2 <1/3> <2/4>\n"
assert tmpfile.read(keep_tabs=True) == "in.dat: N = 2\t<1/3>\t<2/4>\n"

Expand Down
2 changes: 1 addition & 1 deletion pygmt/tests/test_legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def test_legend_specfile():
"""

with GMTTempFile() as specfile:
Path(specfile.name).write_text(specfile_contents)
Path(specfile.name).write_text(specfile_contents, encoding="utf-8")
fig = Figure()
fig.basemap(projection="x6i", region=[0, 1, 0, 1], frame=True)
fig.legend(specfile.name, position="JTM+jCM+w5i")
Expand Down
2 changes: 1 addition & 1 deletion pygmt/tests/test_meca.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def test_meca_spec_single_focalmecha_file():
fig = Figure()
fig.basemap(region=[-1, 1, 4, 6], projection="M8c", frame=2)
with GMTTempFile() as temp:
Path(temp.name).write_text("0 5 0 0 90 0 5")
Path(temp.name).write_text("0 5 0 0 90 0 5", encoding="utf-8")
fig.meca(spec=temp.name, convention="aki", scale="2.5c")
return fig

Expand Down
4 changes: 2 additions & 2 deletions pygmt/tests/test_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ def test_plot_ogrgmt_file_multipoint_default_style(func):
# FEATURE_DATA
1 2
"""
Path(tmpfile.name).write_text(gmt_file)
Path(tmpfile.name).write_text(gmt_file, encoding="utf-8")
fig = Figure()
fig.plot(
data=func(tmpfile.name), region=[0, 2, 1, 3], projection="X2c", frame=True
Expand All @@ -506,7 +506,7 @@ def test_plot_ogrgmt_file_multipoint_non_default_style():
# FEATURE_DATA
1 2
"""
Path(tmpfile.name).write_text(gmt_file)
Path(tmpfile.name).write_text(gmt_file, encoding="utf-8")
fig = Figure()
fig.plot(
data=tmpfile.name,
Expand Down
4 changes: 2 additions & 2 deletions pygmt/tests/test_plot3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ def test_plot3d_ogrgmt_file_multipoint_default_style(func):
>
1 1 2
1.5 1.5 1"""
Path(tmpfile.name).write_text(gmt_file)
Path(tmpfile.name).write_text(gmt_file, encoding="utf-8")
fig = Figure()
fig.plot3d(
data=func(tmpfile.name),
Expand All @@ -469,7 +469,7 @@ def test_plot3d_ogrgmt_file_multipoint_non_default_style():
>
1 1 2
1.5 1.5 1"""
Path(tmpfile.name).write_text(gmt_file)
Path(tmpfile.name).write_text(gmt_file, encoding="utf-8")
fig = Figure()
fig.plot3d(
data=tmpfile.name,
Expand Down
4 changes: 3 additions & 1 deletion pygmt/tests/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,9 @@ def test_text_angle_font_justify_from_textfile():
"""
fig = Figure()
with GMTTempFile(suffix=".txt") as tempfile:
Path(tempfile.name).write_text("114 0.5 30 22p,Helvetica-Bold,black LM BORNEO")
Path(tempfile.name).write_text(
"114 0.5 30 22p,Helvetica-Bold,black LM BORNEO", encoding="utf-8"
)
fig.text(
region=[113, 117.5, -0.5, 3],
projection="M5c",
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ extend-select = [
"D213", # Summary lines should be positioned on the second physical line of the docstring.
"D410", # A blank line after section headings.
"PLR6201", # Use a set literal when testing for membership
"PLW1514", # {function_name} in text mode without explicit encoding argument
]
ignore = [
"D200", # One-line docstring should fit on one line
Expand Down

0 comments on commit 9c13eb0

Please sign in to comment.