Skip to content
11 changes: 10 additions & 1 deletion src/hist/basehist.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ def __init__(
warnings.warn(msg)
storage = storage()
super().__init__(*args, storage=storage, metadata=metadata) # type: ignore

disallowed_names = {"weight", "sample", "threads"}
for ax in self.axes:
if ax.name in disallowed_names:
disallowed_warning = f"{ax.name} is a protected keyword and cannot be used as axis name"
warnings.warn(disallowed_warning)

valid_names = [ax.name for ax in self.axes if ax.name]
if len(valid_names) != len(set(valid_names)):
raise KeyError(
Expand Down Expand Up @@ -203,7 +210,9 @@ def fill(
}

if set(data_dict) != set(range(len(args), self.ndim)):
raise TypeError("All axes must be accounted for in fill")
raise TypeError(
"All axes must be accounted for in fill, you may have used a disallowed name in the axes"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eventually I'd only do this if you have an axis with a disallowed name, but this is fine for now.

I'd eventually make the disallowed_names a global DISALLOWED_NAMES: Final[set[str]] = {...}, then we could see if there is any overlap between these, and only suggest the "you may have used a disallowed name in the axes" part if there is a chance of this - most users seeing this error probably are making normal mistakes, not using one of these special names

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh okay, we can make it global after this.

)

data = (data_dict[i] for i in range(len(args), self.ndim))

Expand Down
14 changes: 13 additions & 1 deletion tests/test_axis.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from hist import axis
import pytest

from hist import axis, hist


def test_axis_names():
Expand Down Expand Up @@ -51,3 +53,13 @@ def test_axis_flow():
assert axis.Integer(0, 8, flow=False, overflow=True) == axis.Integer(
0, 8, underflow=False
)


def test_axis_disallowed_names():

with pytest.warns(UserWarning):
hist.Hist(axis.Regular(10, 0, 10, name="weight"))
with pytest.warns(UserWarning):
hist.Hist(axis.Regular(10, 0, 10, name="sample"))
with pytest.warns(UserWarning):
hist.Hist(axis.Regular(10, 0, 10, name="threads"))