From d1a155300688042ef3e83a33b7b638b1995e1517 Mon Sep 17 00:00:00 2001 From: Wei Ji <23487320+weiji14@users.noreply.github.com> Date: Wed, 1 Oct 2025 15:31:29 +1300 Subject: [PATCH 1/6] Check for invalid non-int values passed to Pattern class Set default pattern value as None to fix autodoc warning about `AttributeError: type object 'Pattern' has no attribute 'pattern'`. Add proper validation to ensure non-int and non-PathLike values will raise a GMTValueError. --- pygmt/params/pattern.py | 6 ++++-- pygmt/tests/test_params_pattern.py | 6 +++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/pygmt/params/pattern.py b/pygmt/params/pattern.py index 33c83a569cd..d3b4fceb4d6 100644 --- a/pygmt/params/pattern.py +++ b/pygmt/params/pattern.py @@ -69,7 +69,7 @@ class Pattern(BaseParam): >>> fig.show() """ - pattern: int | PathLike + pattern: int | PathLike = None dpi: int | None = None bgcolor: str | None = None fgcolor: str | None = None @@ -80,7 +80,9 @@ def _validate(self): Validate the parameters. """ # Integer pattern number must be in the range 1-90. - if isinstance(self.pattern, int) and not (1 <= self.pattern <= 90): + if self.pattern is not PathLike or ( + isinstance(self.pattern, int) and not (1 <= self.pattern <= 90) + ): raise GMTValueError( self.pattern, description="pattern number", diff --git a/pygmt/tests/test_params_pattern.py b/pygmt/tests/test_params_pattern.py index 713503bb008..6307a6db6e5 100644 --- a/pygmt/tests/test_params_pattern.py +++ b/pygmt/tests/test_params_pattern.py @@ -35,8 +35,12 @@ def test_pattern(): def test_pattern_invalid_pattern(): """ - Test that an invalid pattern number raises a GMTValueError. + Test that an invalid pattern value or number raises a GMTValueError. """ + with pytest.raises(GMTValueError): + _ = Pattern() # Default value None + with pytest.raises(GMTValueError): + _ = Pattern([]) # non-PathLike value with pytest.raises(GMTValueError): _ = str(Pattern(0)) with pytest.raises(GMTValueError): From c76d951c62a12dce262406c414ee848a2317d5b1 Mon Sep 17 00:00:00 2001 From: Wei Ji <23487320+weiji14@users.noreply.github.com> Date: Wed, 1 Oct 2025 15:37:22 +1300 Subject: [PATCH 2/6] Fix type checking --- pygmt/params/pattern.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/params/pattern.py b/pygmt/params/pattern.py index d3b4fceb4d6..316c99b05e0 100644 --- a/pygmt/params/pattern.py +++ b/pygmt/params/pattern.py @@ -69,7 +69,7 @@ class Pattern(BaseParam): >>> fig.show() """ - pattern: int | PathLike = None + pattern: int | PathLike | None = None dpi: int | None = None bgcolor: str | None = None fgcolor: str | None = None From 4df456672c1389f54b506901d2d5ca2160d44729 Mon Sep 17 00:00:00 2001 From: Wei Ji <23487320+weiji14@users.noreply.github.com> Date: Wed, 1 Oct 2025 15:45:54 +1300 Subject: [PATCH 3/6] Update isinstance check to account for int types --- pygmt/params/pattern.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pygmt/params/pattern.py b/pygmt/params/pattern.py index 316c99b05e0..14cd8864a0b 100644 --- a/pygmt/params/pattern.py +++ b/pygmt/params/pattern.py @@ -80,7 +80,7 @@ def _validate(self): Validate the parameters. """ # Integer pattern number must be in the range 1-90. - if self.pattern is not PathLike or ( + if not isinstance(self.pattern, (PathLike, int)) or ( isinstance(self.pattern, int) and not (1 <= self.pattern <= 90) ): raise GMTValueError( From 68f6c8050ca2d87ccab8829ba12e6a378ab8b200 Mon Sep 17 00:00:00 2001 From: Wei Ji <23487320+weiji14@users.noreply.github.com> Date: Thu, 2 Oct 2025 08:51:56 +1300 Subject: [PATCH 4/6] Set default pattern to 1 --- pygmt/params/pattern.py | 4 ++-- pygmt/tests/test_params_pattern.py | 4 +--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/pygmt/params/pattern.py b/pygmt/params/pattern.py index 14cd8864a0b..837a59f5a2f 100644 --- a/pygmt/params/pattern.py +++ b/pygmt/params/pattern.py @@ -37,7 +37,7 @@ class Pattern(BaseParam): The pattern to use. It can be specified in two forms: - An integer in the range of 1-90, corresponding to one of 90 predefined 64x64 - bit-patterns + bit-patterns. [Default is 1]. - Name of a 1-, 8-, or 24-bit image raster file, to create customized, repeating images using image raster files. dpi @@ -69,7 +69,7 @@ class Pattern(BaseParam): >>> fig.show() """ - pattern: int | PathLike | None = None + pattern: int | PathLike = 1 dpi: int | None = None bgcolor: str | None = None fgcolor: str | None = None diff --git a/pygmt/tests/test_params_pattern.py b/pygmt/tests/test_params_pattern.py index 6307a6db6e5..51d768693ea 100644 --- a/pygmt/tests/test_params_pattern.py +++ b/pygmt/tests/test_params_pattern.py @@ -38,9 +38,7 @@ def test_pattern_invalid_pattern(): Test that an invalid pattern value or number raises a GMTValueError. """ with pytest.raises(GMTValueError): - _ = Pattern() # Default value None - with pytest.raises(GMTValueError): - _ = Pattern([]) # non-PathLike value + _ = Pattern(None) # Value that is neither int nor PathLike with pytest.raises(GMTValueError): _ = str(Pattern(0)) with pytest.raises(GMTValueError): From 709f7f8bcf607fbb71d351dce2fd988b3fa169a1 Mon Sep 17 00:00:00 2001 From: Wei Ji <23487320+weiji14@users.noreply.github.com> Date: Thu, 2 Oct 2025 15:08:54 +1300 Subject: [PATCH 5/6] Update pygmt/params/pattern.py Co-authored-by: Dongdong Tian --- pygmt/params/pattern.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pygmt/params/pattern.py b/pygmt/params/pattern.py index 837a59f5a2f..ab1f966fd66 100644 --- a/pygmt/params/pattern.py +++ b/pygmt/params/pattern.py @@ -80,8 +80,9 @@ def _validate(self): Validate the parameters. """ # Integer pattern number must be in the range 1-90. - if not isinstance(self.pattern, (PathLike, int)) or ( - isinstance(self.pattern, int) and not (1 <= self.pattern <= 90) + if not ( + isinstance(self.pattern, PathLike) or + (isinstance(self.pattern, int) and 1 <= self.pattern <= 90) ): raise GMTValueError( self.pattern, From 319b01aa3d410f46d1e54618014b5c7dc83e9625 Mon Sep 17 00:00:00 2001 From: actions-bot <58130806+actions-bot@users.noreply.github.com> Date: Thu, 2 Oct 2025 03:44:29 +0000 Subject: [PATCH 6/6] [format-command] fixes --- pygmt/params/pattern.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pygmt/params/pattern.py b/pygmt/params/pattern.py index ab1f966fd66..7740352c35d 100644 --- a/pygmt/params/pattern.py +++ b/pygmt/params/pattern.py @@ -81,8 +81,8 @@ def _validate(self): """ # Integer pattern number must be in the range 1-90. if not ( - isinstance(self.pattern, PathLike) or - (isinstance(self.pattern, int) and 1 <= self.pattern <= 90) + isinstance(self.pattern, PathLike) + or (isinstance(self.pattern, int) and 1 <= self.pattern <= 90) ): raise GMTValueError( self.pattern,