MCPcopy Create free account
hub / github.com/numpy/numpy / _promote_fields

Function _promote_fields

numpy/core/_internal.py:436–490  ·  view source on GitHub ↗

Perform type promotion for two structured dtypes. Parameters ---------- dt1 : structured dtype First dtype. dt2 : structured dtype Second dtype. Returns ------- out : dtype The promoted dtype Notes ----- If one of the inputs is alig

(dt1, dt2)

Source from the content-addressed store, hash-verified

434 return array(ary, dtype=copy_dtype, copy=True)
435
436def _promote_fields(dt1, dt2):
437 """ Perform type promotion for two structured dtypes.
438
439 Parameters
440 ----------
441 dt1 : structured dtype
442 First dtype.
443 dt2 : structured dtype
444 Second dtype.
445
446 Returns
447 -------
448 out : dtype
449 The promoted dtype
450
451 Notes
452 -----
453 If one of the inputs is aligned, the result will be. The titles of
454 both descriptors must match (point to the same field).
455 """
456 # Both must be structured and have the same names in the same order
457 if (dt1.names is None or dt2.names is None) or dt1.names != dt2.names:
458 raise DTypePromotionError(
459 f"field names `{dt1.names}` and `{dt2.names}` mismatch.")
460
461 # if both are identical, we can (maybe!) just return the same dtype.
462 identical = dt1 is dt2
463 new_fields = []
464 for name in dt1.names:
465 field1 = dt1.fields[name]
466 field2 = dt2.fields[name]
467 new_descr = promote_types(field1[0], field2[0])
468 identical = identical and new_descr is field1[0]
469
470 # Check that the titles match (if given):
471 if field1[2:] != field2[2:]:
472 raise DTypePromotionError(
473 f"field titles of field '{name}' mismatch")
474 if len(field1) == 2:
475 new_fields.append((name, new_descr))
476 else:
477 new_fields.append(((field1[2], name), new_descr))
478
479 res = dtype(new_fields, align=dt1.isalignedstruct or dt2.isalignedstruct)
480
481 # Might as well preserve identity (and metadata) if the dtype is identical
482 # and the itemsize, offsets are also unmodified. This could probably be
483 # sped up, but also probably just be removed entirely.
484 if identical and res.itemsize == dt1.itemsize:
485 for name in dt1.names:
486 if dt1.fields[name][1] != res.fields[name][1]:
487 return res # the dtype changed.
488 return dt1
489
490 return res
491
492
493def _getfield_is_safe(oldtype, newtype, offset):

Callers

nothing calls this directly

Calls 3

DTypePromotionErrorClass · 0.85
promote_typesFunction · 0.85
dtypeFunction · 0.85

Tested by

no test coverage detected