Skip to content

Commit eae70c9

Browse files
committed
fix: force subclasses to have __slots__
1 parent 22f63f1 commit eae70c9

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

src/boost_histogram/_internal/axis.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,9 @@ def __init__(self, bins, start, stop, **kwargs):
304304
The full metadata dictionary
305305
"""
306306

307+
# Inheriting an axis and forgetting to add __slots__ should be an error
308+
assert not hasattr(self, "__weakref__"), "Axis subclasses must have __slots__!"
309+
307310
with KWArgs(kwargs) as k:
308311
metadata = k.optional("metadata")
309312
transform = k.optional("transform")
@@ -411,6 +414,10 @@ def __init__(self, edges, **kwargs):
411414
__dict__: Optional[Dict[str, Any]] = None
412415
The full metadata dictionary
413416
"""
417+
418+
# Inheriting an axis and forgetting to add __slots__ should be an error
419+
assert not hasattr(self, "__weakref__"), "Axis subclasses must have __slots__!"
420+
414421
with KWArgs(kwargs) as k:
415422
metadata = k.optional("metadata")
416423
__dict__ = k.optional("__dict__")
@@ -492,6 +499,10 @@ def __init__(self, start, stop, **kwargs):
492499
__dict__: Optional[Dict[str, Any]] = None
493500
The full metadata dictionary
494501
"""
502+
503+
# Inheriting an axis and forgetting to add __slots__ should be an error
504+
assert not hasattr(self, "__weakref__"), "Axis subclasses must have __slots__!"
505+
495506
with KWArgs(kwargs) as k:
496507
metadata = k.optional("metadata")
497508
__dict__ = k.optional("__dict__")
@@ -575,6 +586,9 @@ def __init__(self, categories, **kwargs):
575586
The full metadata dictionary
576587
"""
577588

589+
# Inheriting an axis and forgetting to add __slots__ should be an error
590+
assert not hasattr(self, "__weakref__"), "Axis subclasses must have __slots__!"
591+
578592
with KWArgs(kwargs) as k:
579593
metadata = k.optional("metadata")
580594
__dict__ = k.optional("__dict__")
@@ -640,6 +654,10 @@ def __init__(self, categories, **kwargs):
640654
__dict__: Optional[Dict[str, Any]] = None
641655
The full metadata dictionary
642656
"""
657+
658+
# Inheriting an axis and forgetting to add __slots__ should be an error
659+
assert not hasattr(self, "__weakref__"), "Axis subclasses must have __slots__!"
660+
643661
with KWArgs(kwargs) as k:
644662
metadata = k.optional("metadata")
645663
__dict__ = k.optional("__dict__")
@@ -681,6 +699,10 @@ def __init__(self, **kwargs):
681699
__dict__: Optional[Dict[str, Any]] = None
682700
The full metadata dictionary
683701
"""
702+
703+
# Inheriting an axis and forgetting to add __slots__ should be an error
704+
assert not hasattr(self, "__weakref__"), "Axis subclasses must have __slots__!"
705+
684706
with KWArgs(kwargs) as k:
685707
metadata = k.optional("metadata")
686708
__dict__ = k.optional("__dict__")

tests/test_axis.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from pytest import approx
44

55
import boost_histogram as bh
6+
import boost_histogram.utils
67

78
import numpy as np
89
from numpy.testing import assert_array_equal, assert_allclose
@@ -82,6 +83,14 @@ def test_metadata(axis, args, opt, kwargs):
8283
with pytest.raises(KeyError):
8384
axis(*args, **kwargs, __dict__={"metadata": 2}, metadata=3)
8485

86+
# An assertion failure should occur if you forget to set slots!
87+
@boost_histogram.utils.set_family(object())
88+
class TmpAxis(axis):
89+
pass
90+
91+
with pytest.raises(AssertionError):
92+
TmpAxis(*args, **kwargs)
93+
8594

8695
# The point of this ABC is to force all the tests listed here to be
8796
# implemented for each axis type.

tests/test_subclassing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class MyHist(bh.Histogram):
1111

1212
@bh.utils.set_family(NEW_FAMILY)
1313
class MyRegular(bh.axis.Regular):
14-
pass
14+
__slots__ = ()
1515

1616
@bh.utils.set_family(NEW_FAMILY)
1717
class MyIntStorage(bh.storage.Int64):

0 commit comments

Comments
 (0)