Skip to content

Commit a301a6c

Browse files
weiji14seismanactions-bot
authored
pygmt.params.Pattern: Set default pattern value to 1 plus extra validation checks (#4134)
Set default pattern value as 1 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. Co-authored-by: Dongdong Tian <[email protected]> Co-authored-by: actions-bot <[email protected]>
1 parent 663a3b8 commit a301a6c

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

pygmt/params/pattern.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class Pattern(BaseParam):
3737
The pattern to use. It can be specified in two forms:
3838
3939
- An integer in the range of 1-90, corresponding to one of 90 predefined 64x64
40-
bit-patterns
40+
bit-patterns. [Default is 1].
4141
- Name of a 1-, 8-, or 24-bit image raster file, to create customized, repeating
4242
images using image raster files.
4343
dpi
@@ -69,7 +69,7 @@ class Pattern(BaseParam):
6969
>>> fig.show()
7070
"""
7171

72-
pattern: int | PathLike
72+
pattern: int | PathLike = 1
7373
dpi: int | None = None
7474
bgcolor: str | None = None
7575
fgcolor: str | None = None
@@ -80,7 +80,10 @@ def _validate(self):
8080
Validate the parameters.
8181
"""
8282
# Integer pattern number must be in the range 1-90.
83-
if isinstance(self.pattern, int) and not (1 <= self.pattern <= 90):
83+
if not (
84+
isinstance(self.pattern, PathLike)
85+
or (isinstance(self.pattern, int) and 1 <= self.pattern <= 90)
86+
):
8487
raise GMTValueError(
8588
self.pattern,
8689
description="pattern number",

pygmt/tests/test_params_pattern.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ def test_pattern():
3535

3636
def test_pattern_invalid_pattern():
3737
"""
38-
Test that an invalid pattern number raises a GMTValueError.
38+
Test that an invalid pattern value or number raises a GMTValueError.
3939
"""
40+
with pytest.raises(GMTValueError):
41+
_ = Pattern(None) # Value that is neither int nor PathLike
4042
with pytest.raises(GMTValueError):
4143
_ = str(Pattern(0))
4244
with pytest.raises(GMTValueError):

0 commit comments

Comments
 (0)