Return `value` as a valid default of NumPy type `dtype`.
(value: Any, dtype: DTypeLike)
| 104 | |
| 105 | |
| 106 | def _normalize_default(value: Any, dtype: DTypeLike) -> np.ndarray: |
| 107 | """Return `value` as a valid default of NumPy type `dtype`.""" |
| 108 | # Create NumPy objects as defaults |
| 109 | # This is better in order to serialize them as attributes |
| 110 | if value is None: |
| 111 | value = 0 |
| 112 | basedtype = dtype.base |
| 113 | try: |
| 114 | default = np.array(value, dtype=basedtype) |
| 115 | except ValueError: |
| 116 | array = np.array(value) |
| 117 | if array.shape != basedtype.shape: |
| 118 | raise |
| 119 | # Maybe nested dtype with "scalar" value. |
| 120 | default = np.array(value, dtype=basedtype.base) |
| 121 | # 0-dim arrays will be representented as NumPy scalars |
| 122 | # (PyTables attribute convention) |
| 123 | if default.shape == (): |
| 124 | default = default[()] |
| 125 | return default |
| 126 | |
| 127 | |
| 128 | def _cmp_dispatcher(other_method_name: str) -> Callable[[Any, Any], bool]: |