MCPcopy Index your code
hub / github.com/pydata/xarray / test_new_namedarray

Method test_new_namedarray

xarray/tests/test_namedarray.py:395–450  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

393 check_duck_array_typevar(arrayapi_a)
394
395 def test_new_namedarray(self) -> None:
396 dtype_float = np.dtype(np.float32)
397 narr_float: NamedArray[Any, np.dtype[np.float32]]
398 narr_float = NamedArray(("x",), np.array([1.5, 3.2], dtype=dtype_float))
399 assert narr_float.dtype == dtype_float
400
401 dtype_int = np.dtype(np.int8)
402 narr_int: NamedArray[Any, np.dtype[np.int8]]
403 narr_int = narr_float._new(("x",), np.array([1, 3], dtype=dtype_int))
404 assert narr_int.dtype == dtype_int
405
406 class Variable(
407 NamedArray[_ShapeType_co, _DType_co], Generic[_ShapeType_co, _DType_co]
408 ):
409 @overload
410 def _new(
411 self,
412 dims: _DimsLike | Default = ...,
413 data: duckarray[Any, _DType] = ...,
414 attrs: _AttrsLike | Default = ...,
415 ) -> Variable[Any, _DType]: ...
416
417 @overload
418 def _new(
419 self,
420 dims: _DimsLike | Default = ...,
421 data: Default = ...,
422 attrs: _AttrsLike | Default = ...,
423 ) -> Variable[_ShapeType_co, _DType_co]: ...
424
425 def _new(
426 self,
427 dims: _DimsLike | Default = _default,
428 data: duckarray[Any, _DType] | Default = _default,
429 attrs: _AttrsLike | Default = _default,
430 ) -> Variable[Any, _DType] | Variable[_ShapeType_co, _DType_co]:
431 dims_ = copy.copy(self._dims) if dims is _default else dims
432
433 attrs_: Mapping[Any, Any] | None
434 if attrs is _default:
435 attrs_ = None if self._attrs is None else self._attrs.copy()
436 else:
437 attrs_ = attrs
438
439 if data is _default:
440 return type(self)(dims_, copy.copy(self._data), attrs_)
441 cls_ = cast("type[Variable[Any, _DType]]", type(self))
442 return cls_(dims_, data, attrs_)
443
444 var_float: Variable[Any, np.dtype[np.float32]]
445 var_float = Variable(("x",), np.array([1.5, 3.2], dtype=dtype_float))
446 assert var_float.dtype == dtype_float
447
448 var_int: Variable[Any, np.dtype[np.int8]]
449 var_int = var_float._new(("x",), np.array([1, 3], dtype=dtype_int))
450 assert var_int.dtype == dtype_int
451
452 def test_replace_namedarray(self) -> None:

Callers

nothing calls this directly

Calls 5

_newMethod · 0.95
_newMethod · 0.95
NamedArrayClass · 0.90
VariableClass · 0.70
dtypeMethod · 0.45

Tested by

no test coverage detected