Skip to content
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

Enable ruff's unspecified-encoding (PLW1514) rule and fix violations #3319

Merged
merged 3 commits into from
Jul 8, 2024
Merged
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
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:
Copy link
Member Author

@weiji14 weiji14 Jul 8, 2024

Choose a reason for hiding this comment

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

This assumes that *.gmt files are always encoded in UTF-8, is this the case, or should we choose encoding="locale" instead?

To be clear, we're only trying to parse the vector geometry type (Multipoint/Point) from the *.gmt file, not read the whole file, so maybe ok to assume that the header lines are utf-8 compatible?

Copy link
Member

@seisman seisman Jul 8, 2024

Choose a reason for hiding this comment

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

*.gmt can be encoded in any encoding, so there is no way to make it work in all cases. I guess encoding="utf-8" is the best choice.

To be clear, we're only trying to parse the vector geometry type (Multipoint/Point) from the *.gmt file, not read the whole file, so maybe ok to assume that the header lines are utf-8 compatible?

It usually works, but will fail if the file uses a different encoding and contains comments on the first line.

Actually, we're assuming that the @G record is always on the first line but that's not always true.

Copy link
Member Author

@weiji14 weiji14 Jul 8, 2024

Choose a reason for hiding this comment

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

Ok, will go with utf-8 then. Users can still manually override the style if needed, in case utf-8 encoding doesn't work and the style isn't automatically applied.

Actually, we're assuming that the @g record is always on the first line but that's not always true.

From https://docs.generic-mapping-tools.org/6.5/reference/ogrgmt-format.html#the-ogr-gmt-format, it says:

The first comment line must specify the version of the OGR/GMT data format, to allow for future changes or enhancements to be supported by future GMT programs. This document describes v1.0.

and the examples on the page below seem to suggest that # @VGMT1.0 is followed immediately by @GP... on the same line. Are there cases where @GP... goes onto the second line?

Copy link
Member

Choose a reason for hiding this comment

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

ogr2ogr also produces OGR_GMT files with # @VGMT1.0 followed by @GP, so it should be fine, although users can manually putting @GP at the second line and even add any comments at the begin of the file.

Anyway, that's just rare cases and it's safe to ignore them.

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
Loading