|
16 | 16 | Tuple, |
17 | 17 | TypeVar, |
18 | 18 | Union, |
| 19 | + overload, |
19 | 20 | ) |
20 | 21 |
|
21 | 22 | import numpy as np |
|
35 | 36 |
|
36 | 37 | if TYPE_CHECKING: |
37 | 38 | from .dataarray import DataArray |
| 39 | + from .dataset import Dataset |
| 40 | + from .variable import Variable |
38 | 41 | from .weighted import Weighted |
39 | 42 |
|
40 | 43 | T_DataWithCoords = TypeVar("T_DataWithCoords", bound="DataWithCoords") |
@@ -1501,7 +1504,26 @@ def __getitem__(self, value): |
1501 | 1504 | raise NotImplementedError() |
1502 | 1505 |
|
1503 | 1506 |
|
1504 | | -def full_like(other, fill_value, dtype: DTypeLike = None): |
| 1507 | +@overload |
| 1508 | +def full_like( |
| 1509 | + other: "Dataset", |
| 1510 | + fill_value, |
| 1511 | + dtype: Union[DTypeLike, Mapping[Hashable, DTypeLike]] = None, |
| 1512 | +) -> "Dataset": |
| 1513 | + ... |
| 1514 | + |
| 1515 | + |
| 1516 | +@overload |
| 1517 | +def full_like(other: "DataArray", fill_value, dtype: DTypeLike = None) -> "DataArray": |
| 1518 | + ... |
| 1519 | + |
| 1520 | + |
| 1521 | +@overload |
| 1522 | +def full_like(other: "Variable", fill_value, dtype: DTypeLike = None) -> "Variable": |
| 1523 | + ... |
| 1524 | + |
| 1525 | + |
| 1526 | +def full_like(other, fill_value, dtype=None): |
1505 | 1527 | """Return a new object with the same shape and type as a given object. |
1506 | 1528 |
|
1507 | 1529 | Parameters |
@@ -1618,15 +1640,22 @@ def full_like(other, fill_value, dtype: DTypeLike = None): |
1618 | 1640 | f"fill_value must be scalar or, for datasets, a dict-like. Received {fill_value} instead." |
1619 | 1641 | ) |
1620 | 1642 |
|
| 1643 | + if not isinstance(other, Dataset) and isinstance(dtype, Mapping): |
| 1644 | + raise ValueError( |
| 1645 | + "'dtype' cannot be dict-like when passing a DataArray or Variable" |
| 1646 | + ) |
| 1647 | + |
1621 | 1648 | if isinstance(other, Dataset): |
1622 | 1649 | if not isinstance(fill_value, dict): |
1623 | 1650 | fill_value = {k: fill_value for k in other.data_vars.keys()} |
1624 | 1651 |
|
1625 | | - if not isinstance(dtype, dict): |
1626 | | - dtype = {k: dtype for k in other.data_vars.keys()} |
| 1652 | + if not isinstance(dtype, Mapping): |
| 1653 | + dtype_ = {k: dtype for k in other.data_vars.keys()} |
| 1654 | + else: |
| 1655 | + dtype_ = dtype |
1627 | 1656 |
|
1628 | 1657 | data_vars = { |
1629 | | - k: _full_like_variable(v, fill_value.get(k, dtypes.NA), dtype.get(k, None)) |
| 1658 | + k: _full_like_variable(v, fill_value.get(k, dtypes.NA), dtype_.get(k, None)) |
1630 | 1659 | for k, v in other.data_vars.items() |
1631 | 1660 | } |
1632 | 1661 | return Dataset(data_vars, coords=other.coords, attrs=other.attrs) |
|
0 commit comments