Skip to content

Commit 2aa0e0f

Browse files
authored
refactor: nicer input handling (#427)
1 parent 5afdbfd commit 2aa0e0f

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

src/boost_histogram/_internal/hist.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def __init__(self, *axes, **kwargs):
9191
9292
If you pass in a single argument, this will be treated as a
9393
histogram and this will convert the histogram to this type of
94-
histogram (DensityHistogram, Histogram, BoostHistogram).
94+
histogram.
9595
9696
Parameters
9797
----------
@@ -104,21 +104,23 @@ def __init__(self, *axes, **kwargs):
104104
"""
105105

106106
# Allow construction from a raw histogram object (internal)
107-
if not kwargs and len(axes) == 1 and isinstance(axes[0], _histograms):
107+
if len(axes) == 1 and isinstance(axes[0], _histograms):
108108
self._hist = axes[0]
109-
self.metadata = None
109+
self.metadata = kwargs.get("metadata")
110110
self.axes = self._generate_axes_()
111111
return
112112

113113
# If we construct with another Histogram, support that too
114-
if not kwargs and len(axes) == 1 and isinstance(axes[0], Histogram):
114+
if len(axes) == 1 and isinstance(axes[0], Histogram):
115115
self.__init__(axes[0]._hist)
116116
self.metadata = axes[0].metadata
117117
return
118118

119119
# Keyword only trick (change when Python2 is dropped)
120120
with KWArgs(kwargs) as k:
121-
storage = k.optional("storage", Double())
121+
storage = k.optional("storage")
122+
if storage is None:
123+
storage = Double()
122124
self.metadata = k.optional("metadata")
123125

124126
# Check for missed parenthesis or incorrect types
@@ -160,8 +162,7 @@ def _new_hist(self, _hist):
160162
Return a new histogram given a new _hist, copying metadata.
161163
"""
162164

163-
other = self.__class__(_hist)
164-
other.metadata = self.metadata
165+
other = self.__class__(_hist, metadata=self.metadata)
165166
return other
166167

167168
@property

tests/test_histogram_init.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ def test_make_any_hist_storage():
9090
assert isinstance(
9191
bh.Histogram(bh.axis.Regular(5, 1, 2), storage=bh.storage.Double())[0], float
9292
)
93+
assert isinstance(bh.Histogram(bh.axis.Regular(5, 1, 2), storage=None)[0], float)
9394

9495

9596
def test_issue_axis_bin_swan():

0 commit comments

Comments
 (0)