@@ -159,34 +159,40 @@ class ComplainingNumpyNonObjectArray(metaclass=ComplainingNumpyNonObjectArrayMet
159
159
160
160
161
161
def with_container_arithmetic (
162
- * ,
163
- bcast_number : bool = True ,
164
- _bcast_actx_array_type : Optional [bool ] = None ,
165
- bcast_obj_array : Optional [bool ] = None ,
166
- bcast_numpy_array : bool = False ,
167
- bcast_container_types : Optional [Tuple [type , ...]] = None ,
168
- arithmetic : bool = True ,
169
- matmul : bool = False ,
170
- bitwise : bool = False ,
171
- shift : bool = False ,
172
- _cls_has_array_context_attr : Optional [bool ] = None ,
173
- eq_comparison : Optional [bool ] = None ,
174
- rel_comparison : Optional [bool ] = None ) -> Callable [[type ], type ]:
162
+ * ,
163
+ number_bcasts_across : Optional [bool ] = None ,
164
+ bcasts_across_obj_array : Optional [bool ] = None ,
165
+ container_types_bcast_across : Optional [Tuple [type , ...]] = None ,
166
+ arithmetic : bool = True ,
167
+ matmul : bool = False ,
168
+ bitwise : bool = False ,
169
+ shift : bool = False ,
170
+ _cls_has_array_context_attr : Optional [bool ] = None ,
171
+ eq_comparison : Optional [bool ] = None ,
172
+ rel_comparison : Optional [bool ] = None ,
173
+
174
+ # deprecated:
175
+ bcast_number : Optional [bool ] = None ,
176
+ bcast_obj_array : Optional [bool ] = None ,
177
+ bcast_numpy_array : bool = False ,
178
+ _bcast_actx_array_type : Optional [bool ] = None ,
179
+ bcast_container_types : Optional [Tuple [type , ...]] = None ,
180
+ ) -> Callable [[type ], type ]:
175
181
"""A class decorator that implements built-in operators for array containers
176
182
by propagating the operations to the elements of the container.
177
183
178
- :arg bcast_number : If *True*, numbers broadcast over the container
184
+ :arg number_bcasts_across : If *True*, numbers broadcast over the container
179
185
(with the container as the 'outer' structure).
180
- :arg bcast_obj_array : If *True*, this container will be broadcast
186
+ :arg bcasts_across_obj_array : If *True*, this container will be broadcast
181
187
across :mod:`numpy` object arrays
182
188
(with the object array as the 'outer' structure).
183
- Add :class:`numpy.ndarray` to *bcast_container_types * to achieve
189
+ Add :class:`numpy.ndarray` to *container_types_bcast_across * to achieve
184
190
the 'reverse' broadcasting.
185
- :arg bcast_container_types : A sequence of container types that will broadcast
191
+ :arg container_types_bcast_across : A sequence of container types that will broadcast
186
192
across this container, with this container as the 'outer' structure.
187
193
:class:`numpy.ndarray` is permitted to be part of this sequence to
188
- indicate that object arrays (and *only* object arrays) will be broadcasat .
189
- In this case, *bcast_obj_array * must be *False*.
194
+ indicate that object arrays (and *only* object arrays) will be broadcast .
195
+ In this case, *bcasts_across_obj_array * must be *False*.
190
196
:arg arithmetic: Implement the conventional arithmetic operators, including
191
197
``**``, :func:`divmod`, and ``//``. Also includes ``+`` and ``-`` as well as
192
198
:func:`abs`.
@@ -241,8 +247,71 @@ def _deserialize_init_arrays_code(cls, tmpl_instance_name, args):
241
247
242
248
# {{{ handle inputs
243
249
244
- if bcast_obj_array is None :
245
- raise TypeError ("bcast_obj_array must be specified" )
250
+ if rel_comparison and eq_comparison is None :
251
+ eq_comparison = True
252
+
253
+ if eq_comparison is None :
254
+ raise TypeError ("eq_comparison must be specified" )
255
+
256
+ # {{{ handle bcast_number
257
+
258
+ if bcast_number is not None :
259
+ if number_bcasts_across is not None :
260
+ raise TypeError (
261
+ "may specify at most one of 'bcast_number' and "
262
+ "'number_bcasts_across'" )
263
+
264
+ warn ("'bcast_number' is deprecated and will be unsupported from 2025. "
265
+ "Use 'number_bcasts_across', with equivalent meaning." ,
266
+ DeprecationWarning , stacklevel = 2 )
267
+ number_bcasts_across = bcast_number
268
+ else :
269
+ if number_bcasts_across is None :
270
+ number_bcasts_across = True
271
+
272
+ del bcast_number
273
+
274
+ # }}}
275
+
276
+ # {{{ handle bcast_obj_array
277
+
278
+ if bcast_obj_array is not None :
279
+ if bcasts_across_obj_array is not None :
280
+ raise TypeError (
281
+ "may specify at most one of 'bcast_obj_array' and "
282
+ "'bcasts_across_obj_array'" )
283
+
284
+ warn ("'bcast_obj_array' is deprecated and will be unsupported from 2025. "
285
+ "Use 'bcasts_across_obj_array', with equivalent meaning." ,
286
+ DeprecationWarning , stacklevel = 2 )
287
+ bcasts_across_obj_array = bcast_obj_array
288
+ else :
289
+ if bcasts_across_obj_array is None :
290
+ raise TypeError ("bcasts_across_obj_array must be specified" )
291
+
292
+ del bcast_obj_array
293
+
294
+ # }}}
295
+
296
+ # {{{ handle bcast_container_types
297
+
298
+ if bcast_container_types is not None :
299
+ if container_types_bcast_across is not None :
300
+ raise TypeError (
301
+ "may specify at most one of 'bcast_container_types' and "
302
+ "'container_types_bcast_across'" )
303
+
304
+ warn ("'bcast_container_types' is deprecated and will be unsupported from 2025. "
305
+ "Use 'container_types_bcast_across', with equivalent meaning." ,
306
+ DeprecationWarning , stacklevel = 2 )
307
+ container_types_bcast_across = bcast_container_types
308
+ else :
309
+ if container_types_bcast_across is None :
310
+ container_types_bcast_across = ()
311
+
312
+ del bcast_container_types
313
+
314
+ # }}}
246
315
247
316
if rel_comparison is None :
248
317
raise TypeError ("rel_comparison must be specified" )
@@ -255,36 +324,27 @@ def _deserialize_init_arrays_code(cls, tmpl_instance_name, args):
255
324
raise ValueError ("'bcast_numpy_array' and '_bcast_actx_array_type'"
256
325
" cannot be both set." )
257
326
258
- if rel_comparison and eq_comparison is None :
259
- eq_comparison = True
260
-
261
- if eq_comparison is None :
262
- raise TypeError ("eq_comparison must be specified" )
263
-
264
- if not bcast_obj_array and bcast_numpy_array :
327
+ if not bcasts_across_obj_array and bcast_numpy_array :
265
328
raise TypeError ("bcast_obj_array must be set if bcast_numpy_array is" )
266
329
267
330
if bcast_numpy_array :
268
331
def numpy_pred (name : str ) -> str :
269
332
return f"is_numpy_array({ name } )"
270
- elif bcast_obj_array :
333
+ elif bcasts_across_obj_array :
271
334
def numpy_pred (name : str ) -> str :
272
335
return f"isinstance({ name } , np.ndarray) and { name } .dtype.char == 'O'"
273
336
else :
274
337
def numpy_pred (name : str ) -> str :
275
338
return "False" # optimized away
276
339
277
- if bcast_container_types is None :
278
- bcast_container_types = ()
279
-
280
- if np .ndarray in bcast_container_types and bcast_obj_array :
340
+ if np .ndarray in container_types_bcast_across and bcasts_across_obj_array :
281
341
raise ValueError ("If numpy.ndarray is part of bcast_container_types, "
282
342
"bcast_obj_array must be False." )
283
343
284
344
numpy_check_types : list [type ] = [NumpyObjectArray , ComplainingNumpyNonObjectArray ]
285
- bcast_container_types = tuple (
345
+ container_types_bcast_across = tuple (
286
346
new_ct
287
- for old_ct in bcast_container_types
347
+ for old_ct in container_types_bcast_across
288
348
for new_ct in
289
349
(numpy_check_types
290
350
if old_ct is np .ndarray
@@ -334,7 +394,7 @@ def wrap(cls: Any) -> Any:
334
394
335
395
if bcast_actx_array_type is None :
336
396
if cls_has_array_context_attr :
337
- if bcast_number :
397
+ if number_bcasts_across :
338
398
bcast_actx_array_type = cls_has_array_context_attr
339
399
else :
340
400
bcast_actx_array_type = False
@@ -409,14 +469,14 @@ def is_numpy_array(arg):
409
469
""" )
410
470
gen ("" )
411
471
412
- if bcast_container_types :
413
- for i , bct in enumerate (bcast_container_types ):
472
+ if container_types_bcast_across :
473
+ for i , bct in enumerate (container_types_bcast_across ):
414
474
gen (f"from { bct .__module__ } import { bct .__qualname__ } as _bctype{ i } " )
415
475
gen ("" )
416
- outer_bcast_type_names = tuple (
417
- f"_bctype{ i } " for i in range (len (bcast_container_types )))
418
- if bcast_number :
419
- outer_bcast_type_names += ("Number" ,)
476
+ container_type_names_bcast_across = tuple (
477
+ f"_bctype{ i } " for i in range (len (container_types_bcast_across )))
478
+ if number_bcasts_across :
479
+ container_type_names_bcast_across += ("Number" ,)
420
480
421
481
def same_key (k1 : T , k2 : T ) -> T :
422
482
assert k1 == k2
@@ -428,9 +488,14 @@ def tup_str(t: Tuple[str, ...]) -> str:
428
488
else :
429
489
return "({},)" .format (", " .join (t ))
430
490
431
- gen (f"cls._outer_bcast_types = { tup_str (outer_bcast_type_names )} " )
491
+ gen (f"cls._outer_bcast_types = { tup_str (container_type_names_bcast_across )} " )
492
+ gen ("cls._container_types_bcast_across = "
493
+ f"{ tup_str (container_type_names_bcast_across )} " )
494
+
432
495
gen (f"cls._bcast_numpy_array = { bcast_numpy_array } " )
433
- gen (f"cls._bcast_obj_array = { bcast_obj_array } " )
496
+
497
+ gen (f"cls._bcast_obj_array = { bcasts_across_obj_array } " )
498
+ gen (f"cls._bcasts_across_obj_array = { bcasts_across_obj_array } " )
434
499
gen ("" )
435
500
436
501
# {{{ unary operators
@@ -535,9 +600,9 @@ def {fname}(arg1):
535
600
result[i] = { op_str .format ("arg1" , "arg2[i]" )}
536
601
return result
537
602
538
- if { bool (outer_bcast_type_names )} : # optimized away
603
+ if { bool (container_type_names_bcast_across )} : # optimized away
539
604
if isinstance(arg2,
540
- { tup_str (outer_bcast_type_names
605
+ { tup_str (container_type_names_bcast_across
541
606
+ bcast_actx_ary_types )} ):
542
607
if __debug__:
543
608
if isinstance(arg2, { tup_str (bcast_actx_ary_types )} ):
@@ -584,9 +649,9 @@ def {fname}(arg2, arg1):
584
649
for i in np.ndindex(arg1.shape):
585
650
result[i] = { op_str .format ("arg1[i]" , "arg2" )}
586
651
return result
587
- if { bool (outer_bcast_type_names )} : # optimized away
652
+ if { bool (container_type_names_bcast_across )} : # optimized away
588
653
if isinstance(arg1,
589
- { tup_str (outer_bcast_type_names
654
+ { tup_str (container_type_names_bcast_across
590
655
+ bcast_actx_ary_types )} ):
591
656
if __debug__:
592
657
if isinstance(arg1,
0 commit comments