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

Method test_replace_namedarray

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

Source from the content-addressed store, hash-verified

450 assert var_int.dtype == dtype_int
451
452 def test_replace_namedarray(self) -> None:
453 dtype_float = np.dtype(np.float32)
454 np_val: np.ndarray[Any, np.dtype[np.float32]]
455 np_val = np.array([1.5, 3.2], dtype=dtype_float)
456 np_val2: np.ndarray[Any, np.dtype[np.float32]]
457 np_val2 = 2 * np_val
458
459 narr_float: NamedArray[Any, np.dtype[np.float32]]
460 narr_float = NamedArray(("x",), np_val)
461 assert narr_float.dtype == dtype_float
462
463 narr_float2: NamedArray[Any, np.dtype[np.float32]]
464 narr_float2 = NamedArray(("x",), np_val2)
465 assert narr_float2.dtype == dtype_float
466
467 class Variable(
468 NamedArray[_ShapeType_co, _DType_co], Generic[_ShapeType_co, _DType_co]
469 ):
470 @overload
471 def _new(
472 self,
473 dims: _DimsLike | Default = ...,
474 data: duckarray[Any, _DType] = ...,
475 attrs: _AttrsLike | Default = ...,
476 ) -> Variable[Any, _DType]: ...
477
478 @overload
479 def _new(
480 self,
481 dims: _DimsLike | Default = ...,
482 data: Default = ...,
483 attrs: _AttrsLike | Default = ...,
484 ) -> Variable[_ShapeType_co, _DType_co]: ...
485
486 def _new(
487 self,
488 dims: _DimsLike | Default = _default,
489 data: duckarray[Any, _DType] | Default = _default,
490 attrs: _AttrsLike | Default = _default,
491 ) -> Variable[Any, _DType] | Variable[_ShapeType_co, _DType_co]:
492 dims_ = copy.copy(self._dims) if dims is _default else dims
493
494 attrs_: Mapping[Any, Any] | None
495 if attrs is _default:
496 attrs_ = None if self._attrs is None else self._attrs.copy()
497 else:
498 attrs_ = attrs
499
500 if data is _default:
501 return type(self)(dims_, copy.copy(self._data), attrs_)
502 cls_ = cast("type[Variable[Any, _DType]]", type(self))
503 return cls_(dims_, data, attrs_)
504
505 var_float: Variable[Any, np.dtype[np.float32]]
506 var_float = Variable(("x",), np_val)
507 assert var_float.dtype == dtype_float
508
509 var_float2: Variable[Any, np.dtype[np.float32]]

Callers

nothing calls this directly

Calls 4

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

Tested by

no test coverage detected