Skip to content

Commit e96f272

Browse files
Box, Position: Fix validation of shade_offset and refpoint (#4963)
1 parent 9f5b4c6 commit e96f272

4 files changed

Lines changed: 26 additions & 3 deletions

File tree

‎pygmt/params/box.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def _validate(self):
7070
)
7171

7272
# shade_offset must be a sequence of two values or None.
73-
if self.shade_offset and not (
73+
if self.shade_offset is not None and not (
7474
is_nonstr_iter(self.shade_offset) and len(self.shade_offset) == 2
7575
):
7676
raise GMTValueError(
@@ -91,7 +91,7 @@ def _shade(self) -> list[str | float] | None:
9191
"""
9292
Shading for the box, formatted as a list of 1-3 values, or None.
9393
"""
94-
_shade_offset = self.shade_offset or []
94+
_shade_offset = [] if self.shade_offset is None else self.shade_offset
9595
return [v for v in (*_shade_offset, self.shade_fill) if v is not None] or None
9696

9797
@property

‎pygmt/params/position.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,10 @@ def _validate(self):
180180
reason="Expect a sequence of two values.",
181181
)
182182
case "inside" | "outside":
183-
if self.refpoint not in _valid_anchors:
183+
if (
184+
not isinstance(self.refpoint, str)
185+
or self.refpoint not in _valid_anchors
186+
):
184187
raise GMTValueError(
185188
self.refpoint,
186189
description="reference point",

‎pygmt/tests/test_params_box.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Test the Box class.
33
"""
44

5+
import numpy as np
56
import pytest
67
from pygmt.exceptions import GMTParameterError, GMTValueError
78
from pygmt.params import Box
@@ -57,3 +58,13 @@ def test_params_box_invalid_innerborder():
5758
"""
5859
with pytest.raises(GMTParameterError):
5960
_ = str(Box(inner_gap="2p"))
61+
62+
63+
def test_params_box_shade_offset_array_like():
64+
"""
65+
Test that shade_offset accepts numpy arrays and rejects empty sequences.
66+
"""
67+
assert str(Box(shade_offset=np.array([1, 2]))) == "+s1/2"
68+
assert str(Box(shade_offset=np.array([1, 2]), shade_fill="red")) == "+s1/2/red"
69+
with pytest.raises(GMTValueError):
70+
Box(shade_offset=[])

‎pygmt/tests/test_params_position.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,12 @@ def test_params_position_invalid_anchor():
5959
"""
6060
with pytest.raises(GMTValueError):
6161
Position((10, 20), cstype="mapcoords", anchor="XX")
62+
63+
64+
def test_params_position_unhashable_justification_code():
65+
"""
66+
Test that an unhashable refpoint raises GMTValueError rather than TypeError.
67+
"""
68+
for cstype in ("inside", "outside"):
69+
with pytest.raises(GMTValueError):
70+
Position(["T", "L"], cstype=cstype)

0 commit comments

Comments
 (0)