@@ -112,13 +112,45 @@ def test_collection_pop(collection, popped_key, expected_popped, expected_collec
112112
113113@pytest .mark .parametrize (('collection' , 'key' , 'expected' ), [
114114 (cube_collection , "cube0" , NDCollection ([("cube1" , cube1 ), ("cube2" , cube2 )],
115- aligned_axes = aligned_axes [1 :]))])
115+ aligned_axes = aligned_axes [1 :])),
116+ # Regression test: __delitem__ used to call self.aligned_axes.__delitem__(key)
117+ # unconditionally, crashing with AttributeError on a collection with no
118+ # aligned axes since self.aligned_axes is None in that case.
119+ (unaligned_collection , "cube0" , NDCollection ([("cube1" , cube1 ), ("cube2" , cube2 )]))])
116120def test_del_collection (collection , key , expected ):
117121 del_collection = collection .copy ()
118122 del del_collection [key ]
119123 helpers .assert_collections_equal (del_collection , expected )
120124
121125
126+ def test_slice_by_keys_unaligned_collection ():
127+ # Regression test: slicing by a sequence of string keys used to crash with
128+ # TypeError ("'NoneType' object is not subscriptable") on a collection with
129+ # aligned_axes=None, because `self.aligned_axes[item_]` was called
130+ # unconditionally instead of being guarded by an is-None check.
131+ result = unaligned_collection [("cube0" , "cube1" )]
132+ assert list (result .keys ()) == ["cube0" , "cube1" ]
133+ assert result .aligned_axes is None
134+
135+
136+ def test_collection_getitem_unsupported_type ():
137+ # Regression test: the "unsupported slicing type" error message referenced
138+ # `axis_item`, a loop variable only bound inside the tuple-handling branch,
139+ # so hitting this branch raised NameError instead of the intended TypeError.
140+ with pytest .raises (TypeError , match = "Unsupported slicing type" ):
141+ cube_collection [1.5 ]
142+
143+
144+ def test_collection_slice_open_start_negative_stop_with_sliceable_meta ():
145+ # Regression test: sanitizing a slice item with sliceable meta compared
146+ # `item.start < 0` even when `item.start` is None (i.e. an open-ended
147+ # slice like `slice(None, -1)`), crashing with
148+ # "'<' not supported between instances of 'NoneType' and 'int'".
149+ result = cube_collection [slice (None , - 1 )]
150+ expected = cube_collection [slice (0 , cube_collection .aligned_dimensions [0 ] - 1 )]
151+ helpers .assert_collections_equal (result , expected )
152+
153+
122154@pytest .mark .parametrize (('collection' , 'key' , 'data' , 'aligned_axes' , 'expected' ), [
123155 (cube_collection , "cube1" , cube2 , aligned_axes [2 ], NDCollection (
124156 [("cube0" , cube0 ), ("cube1" , cube2 ), ("cube2" , cube2 )],
0 commit comments