From 5af45cffa8cb0008386cd37bba3523e000dd9121 Mon Sep 17 00:00:00 2001 From: Wei Ji <23487320+weiji14@users.noreply.github.com> Date: Mon, 8 Jul 2024 19:51:23 +1200 Subject: [PATCH 1/3] Enable ruff's unspecified-encoding (PLW1514) rule Xref https://docs.astral.sh/ruff/rules/unspecified-encoding --- pyproject.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/pyproject.toml b/pyproject.toml index 98ef1f40125..98637f1f8b6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 From ac7ea582d3c2a366a852bc86eabf3088b6a8bc09 Mon Sep 17 00:00:00 2001 From: Wei Ji <23487320+weiji14@users.noreply.github.com> Date: Mon, 8 Jul 2024 19:55:31 +1200 Subject: [PATCH 2/3] Fix PLR1514 violations by setting encoding="encoding" Default unsafe-fix is to use `encoding="locale"` --- pygmt/src/plot.py | 2 +- pygmt/src/plot3d.py | 2 +- pygmt/tests/test_datatypes_dataset.py | 10 +++++----- pygmt/tests/test_helpers.py | 4 +++- pygmt/tests/test_legend.py | 2 +- pygmt/tests/test_meca.py | 2 +- pygmt/tests/test_plot.py | 4 ++-- pygmt/tests/test_plot3d.py | 4 ++-- pygmt/tests/test_text.py | 4 +++- 9 files changed, 19 insertions(+), 15 deletions(-) diff --git a/pygmt/src/plot.py b/pygmt/src/plot.py index e660f370848..8642ff6ff5d 100644 --- a/pygmt/src/plot.py +++ b/pygmt/src/plot.py @@ -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="locale") as file: line = file.readline() if "@GMULTIPOINT" in line or "@GPOINT" in line: kwargs["S"] = "s0.2c" diff --git a/pygmt/src/plot3d.py b/pygmt/src/plot3d.py index 929847337c5..9f79854ca91 100644 --- a/pygmt/src/plot3d.py +++ b/pygmt/src/plot3d.py @@ -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="locale") as file: line = file.readline() if "@GMULTIPOINT" in line or "@GPOINT" in line: kwargs["S"] = "u0.2c" diff --git a/pygmt/tests/test_datatypes_dataset.py b/pygmt/tests/test_datatypes_dataset.py index aa261c74a62..cc1ea601beb 100644 --- a/pygmt/tests/test_datatypes_dataset.py +++ b/pygmt/tests/test_datatypes_dataset.py @@ -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="locale") 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) @@ -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="locale") as fp: print("# This is a comment line.", file=fp) df = dataframe_from_gmt(tmpfile.name) @@ -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="locale") 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) @@ -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="locale") 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) @@ -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="locale") 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) diff --git a/pygmt/tests/test_helpers.py b/pygmt/tests/test_helpers.py index b805523f75d..e4299d852ed 100644 --- a/pygmt/tests/test_helpers.py +++ b/pygmt/tests/test_helpers.py @@ -132,7 +132,9 @@ 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="locale" + ) 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" diff --git a/pygmt/tests/test_legend.py b/pygmt/tests/test_legend.py index 8721bb66384..8dac0fcea6b 100644 --- a/pygmt/tests/test_legend.py +++ b/pygmt/tests/test_legend.py @@ -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="locale") fig = Figure() fig.basemap(projection="x6i", region=[0, 1, 0, 1], frame=True) fig.legend(specfile.name, position="JTM+jCM+w5i") diff --git a/pygmt/tests/test_meca.py b/pygmt/tests/test_meca.py index e54799711ad..b5a90317ce0 100644 --- a/pygmt/tests/test_meca.py +++ b/pygmt/tests/test_meca.py @@ -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="locale") fig.meca(spec=temp.name, convention="aki", scale="2.5c") return fig diff --git a/pygmt/tests/test_plot.py b/pygmt/tests/test_plot.py index ecae491c6fa..27a9555c073 100644 --- a/pygmt/tests/test_plot.py +++ b/pygmt/tests/test_plot.py @@ -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="locale") fig = Figure() fig.plot( 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(): # FEATURE_DATA 1 2 """ - Path(tmpfile.name).write_text(gmt_file) + Path(tmpfile.name).write_text(gmt_file, encoding="locale") fig = Figure() fig.plot( data=tmpfile.name, diff --git a/pygmt/tests/test_plot3d.py b/pygmt/tests/test_plot3d.py index 33f3c94812f..ce2c414edc3 100644 --- a/pygmt/tests/test_plot3d.py +++ b/pygmt/tests/test_plot3d.py @@ -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="locale") fig = Figure() fig.plot3d( data=func(tmpfile.name), @@ -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="locale") fig = Figure() fig.plot3d( data=tmpfile.name, diff --git a/pygmt/tests/test_text.py b/pygmt/tests/test_text.py index 6bd2c61383e..161092a7a20 100644 --- a/pygmt/tests/test_text.py +++ b/pygmt/tests/test_text.py @@ -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="locale" + ) fig.text( region=[113, 117.5, -0.5, 3], projection="M5c", From 85bf0fec7eb46f66ebbb0208253b23d915bc5f6b Mon Sep 17 00:00:00 2001 From: Wei Ji <23487320+weiji14@users.noreply.github.com> Date: Mon, 8 Jul 2024 20:04:22 +1200 Subject: [PATCH 3/3] 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 --- pygmt/src/plot.py | 2 +- pygmt/src/plot3d.py | 2 +- pygmt/tests/test_datatypes_dataset.py | 10 +++++----- pygmt/tests/test_helpers.py | 4 +--- pygmt/tests/test_legend.py | 2 +- pygmt/tests/test_meca.py | 2 +- pygmt/tests/test_plot.py | 4 ++-- pygmt/tests/test_plot3d.py | 4 ++-- pygmt/tests/test_text.py | 2 +- 9 files changed, 15 insertions(+), 17 deletions(-) diff --git a/pygmt/src/plot.py b/pygmt/src/plot.py index 8642ff6ff5d..43b26232871 100644 --- a/pygmt/src/plot.py +++ b/pygmt/src/plot.py @@ -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(encoding="locale") 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" diff --git a/pygmt/src/plot3d.py b/pygmt/src/plot3d.py index 9f79854ca91..65d87761d5c 100644 --- a/pygmt/src/plot3d.py +++ b/pygmt/src/plot3d.py @@ -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(encoding="locale") 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" diff --git a/pygmt/tests/test_datatypes_dataset.py b/pygmt/tests/test_datatypes_dataset.py index cc1ea601beb..78c51e827e5 100644 --- a/pygmt/tests/test_datatypes_dataset.py +++ b/pygmt/tests/test_datatypes_dataset.py @@ -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", encoding="locale") 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) @@ -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", encoding="locale") 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) @@ -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", encoding="locale") 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) @@ -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", encoding="locale") 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) @@ -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", encoding="locale") 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) diff --git a/pygmt/tests/test_helpers.py b/pygmt/tests/test_helpers.py index e4299d852ed..ea9e4c87225 100644 --- a/pygmt/tests/test_helpers.py +++ b/pygmt/tests/test_helpers.py @@ -132,9 +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", encoding="locale" - ) + 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" diff --git a/pygmt/tests/test_legend.py b/pygmt/tests/test_legend.py index 8dac0fcea6b..5280c131cda 100644 --- a/pygmt/tests/test_legend.py +++ b/pygmt/tests/test_legend.py @@ -97,7 +97,7 @@ def test_legend_specfile(): """ with GMTTempFile() as specfile: - Path(specfile.name).write_text(specfile_contents, encoding="locale") + 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") diff --git a/pygmt/tests/test_meca.py b/pygmt/tests/test_meca.py index b5a90317ce0..2e71cc9665d 100644 --- a/pygmt/tests/test_meca.py +++ b/pygmt/tests/test_meca.py @@ -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", encoding="locale") + 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 diff --git a/pygmt/tests/test_plot.py b/pygmt/tests/test_plot.py index 27a9555c073..1de025c3c0e 100644 --- a/pygmt/tests/test_plot.py +++ b/pygmt/tests/test_plot.py @@ -487,7 +487,7 @@ def test_plot_ogrgmt_file_multipoint_default_style(func): # FEATURE_DATA 1 2 """ - Path(tmpfile.name).write_text(gmt_file, encoding="locale") + 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 @@ -506,7 +506,7 @@ def test_plot_ogrgmt_file_multipoint_non_default_style(): # FEATURE_DATA 1 2 """ - Path(tmpfile.name).write_text(gmt_file, encoding="locale") + Path(tmpfile.name).write_text(gmt_file, encoding="utf-8") fig = Figure() fig.plot( data=tmpfile.name, diff --git a/pygmt/tests/test_plot3d.py b/pygmt/tests/test_plot3d.py index ce2c414edc3..60453b3fe63 100644 --- a/pygmt/tests/test_plot3d.py +++ b/pygmt/tests/test_plot3d.py @@ -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, encoding="locale") + Path(tmpfile.name).write_text(gmt_file, encoding="utf-8") fig = Figure() fig.plot3d( data=func(tmpfile.name), @@ -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, encoding="locale") + Path(tmpfile.name).write_text(gmt_file, encoding="utf-8") fig = Figure() fig.plot3d( data=tmpfile.name, diff --git a/pygmt/tests/test_text.py b/pygmt/tests/test_text.py index 161092a7a20..8543734bb30 100644 --- a/pygmt/tests/test_text.py +++ b/pygmt/tests/test_text.py @@ -300,7 +300,7 @@ 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", encoding="locale" + "114 0.5 30 22p,Helvetica-Bold,black LM BORNEO", encoding="utf-8" ) fig.text( region=[113, 117.5, -0.5, 3],