Skip to content

Commit eead7fd

Browse files
committed
fix(backport): partial backport of metadata fix from #518
1 parent 1e7a65f commit eead7fd

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

src/boost_histogram/_internal/axis.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,12 @@ def _repr_kwargs(self):
170170
if not self.traits.overflow:
171171
ret += ", overflow=False"
172172

173+
if self.metadata is not None:
174+
if isinstance(self.metadata, string_types):
175+
ret += ", metadata={!r}".format(self.metadata)
176+
else:
177+
ret += ", metadata=..."
178+
173179
return ret
174180

175181
@deprecated("Use .traits instead", name="options")
@@ -536,6 +542,12 @@ def _repr_kwargs(self):
536542
elif self.traits.circular:
537543
ret += ", circular=True"
538544

545+
if self.metadata is not None:
546+
if isinstance(self.metadata, string_types):
547+
ret += ", metadata={!r}".format(self.metadata)
548+
else:
549+
ret += ", metadata=..."
550+
539551
return ret
540552

541553

@@ -698,4 +710,11 @@ def _repr_kwargs(self):
698710
just in case it spans multiple lines.
699711
"""
700712

701-
return ""
713+
ret = ""
714+
if self.metadata is not None:
715+
if isinstance(self.metadata, string_types):
716+
ret += ", metadata={!r}".format(self.metadata)
717+
else:
718+
ret += ", metadata=..."
719+
720+
return ret

tests/test_axis.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -223,16 +223,16 @@ def test_repr(self):
223223
assert repr(ax) == "Regular(4, 1.1, 2.2)"
224224

225225
ax = bh.axis.Regular(4, 1.1, 2.2, metadata="ra")
226-
assert repr(ax) == "Regular(4, 1.1, 2.2)"
226+
assert repr(ax) == "Regular(4, 1.1, 2.2, metadata='ra')"
227227

228228
ax = bh.axis.Regular(4, 1.1, 2.2, underflow=False)
229229
assert repr(ax) == "Regular(4, 1.1, 2.2, underflow=False)"
230230

231231
ax = bh.axis.Regular(4, 1.1, 2.2, metadata="ra", overflow=False)
232-
assert repr(ax) == "Regular(4, 1.1, 2.2, overflow=False)"
232+
assert repr(ax) == "Regular(4, 1.1, 2.2, overflow=False, metadata='ra')"
233233

234234
ax = bh.axis.Regular(4, 1.1, 2.2, metadata="ra", circular=True)
235-
assert repr(ax) == "Regular(4, 1.1, 2.2, circular=True)"
235+
assert repr(ax) == "Regular(4, 1.1, 2.2, circular=True, metadata='ra')"
236236

237237
ax = bh.axis.Regular(4, 1.1, 2.2, transform=bh.axis.transform.log)
238238
assert repr(ax) == "Regular(4, 1.1, 2.2, transform=log)"
@@ -407,7 +407,7 @@ def test_repr(self):
407407
assert repr(ax) == "Regular(4, 1.1, 2.2, circular=True)"
408408

409409
ax = bh.axis.Regular(4, 1.1, 2.2, metadata="hi", circular=True)
410-
assert repr(ax) == "Regular(4, 1.1, 2.2, circular=True)"
410+
assert repr(ax) == "Regular(4, 1.1, 2.2, circular=True, metadata='hi')"
411411

412412
def test_getitem(self):
413413
a = bh.axis.Regular(2, 1, 1 + np.pi * 2, circular=True)
@@ -520,7 +520,7 @@ def test_repr(self):
520520
assert repr(a) == "Variable([-0.1, 0.2])"
521521

522522
a = bh.axis.Variable([-0.1, 0.2], metadata="hi")
523-
assert repr(a) == "Variable([-0.1, 0.2])"
523+
assert repr(a) == "Variable([-0.1, 0.2], metadata='hi')"
524524

525525
def test_getitem(self):
526526
ref = [-0.1, 0.2, 0.3]
@@ -639,7 +639,10 @@ def test_repr(self):
639639
assert repr(a) == "Integer(-1, 1)"
640640

641641
a = bh.axis.Integer(-1, 1, metadata="hi")
642-
assert repr(a) == "Integer(-1, 1)"
642+
assert repr(a) == "Integer(-1, 1, metadata='hi')"
643+
644+
a = bh.axis.Integer(-1, 1, metadata=2)
645+
assert repr(a) == "Integer(-1, 1, metadata=...)"
643646

644647
a = bh.axis.Integer(-1, 1, underflow=False)
645648
assert repr(a) == "Integer(-1, 1, underflow=False)"
@@ -763,14 +766,14 @@ def test_repr(self):
763766
assert repr(ax) == "IntCategory([1, 2, 3])"
764767

765768
ax = bh.axis.IntCategory([1, 2, 3], metadata="foo")
766-
assert repr(ax) == "IntCategory([1, 2, 3])"
769+
assert repr(ax) == "IntCategory([1, 2, 3], metadata='foo')"
767770

768771
ax = bh.axis.StrCategory("ABC", metadata="foo")
769772
# If unicode is the default (Python 3, generally)
770773
if type("") == type(u""): # noqa: E721
771-
assert repr(ax) == "StrCategory(['A', 'B', 'C'])"
774+
assert repr(ax) == "StrCategory(['A', 'B', 'C'], metadata='foo')"
772775
else:
773-
assert repr(ax) == "StrCategory([u'A', u'B', u'C'])"
776+
assert repr(ax) == "StrCategory([u'A', u'B', u'C'], metadata='foo')"
774777

775778
@pytest.mark.parametrize("ref", ([1, 2, 3], "ABC"))
776779
def test_getitem(self, ref, growth):
@@ -868,7 +871,7 @@ def test_repr(self):
868871
assert repr(a) == "Boolean()"
869872

870873
a = bh.axis.Boolean(metadata="hi")
871-
assert repr(a) == "Boolean()"
874+
assert repr(a) == "Boolean(, metadata='hi')"
872875

873876
def test_label(self):
874877
a = bh.axis.Boolean(metadata="foo")

0 commit comments

Comments
 (0)