Skip to content

Commit

Permalink
DataTree should not be "Generic" (pydata#9445)
Browse files Browse the repository at this point in the history
* DataTree should not be "Generic"

DataTree isn't a Generic tree type. It's a specific tree type -- the
nodes are DataTree objects.

This was resulting in many cases where mypy insisting on explicit type
annotations, e.g., `tree: DataTree = DataTree(...)`, which is
unnecessary and annoying boilerplate.

* Fix type error

* type ignore
  • Loading branch information
shoyer authored and hollymandel committed Sep 23, 2024
1 parent 3193717 commit 2de4d27
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 65 deletions.
11 changes: 5 additions & 6 deletions xarray/core/datatree.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
Mapping,
)
from html import escape
from typing import TYPE_CHECKING, Any, Generic, Literal, NoReturn, Union, overload
from typing import TYPE_CHECKING, Any, Literal, NoReturn, Union, overload

from xarray.core import utils
from xarray.core.alignment import align
Expand All @@ -37,7 +37,7 @@
from xarray.core.indexes import Index, Indexes
from xarray.core.merge import dataset_update_method
from xarray.core.options import OPTIONS as XR_OPTS
from xarray.core.treenode import NamedNode, NodePath, Tree
from xarray.core.treenode import NamedNode, NodePath
from xarray.core.utils import (
Default,
Frozen,
Expand Down Expand Up @@ -365,8 +365,7 @@ class DataTree(
MappedDataWithCoords,
DataTreeArithmeticMixin,
TreeAttrAccessMixin,
Generic[Tree],
Mapping,
Mapping[str, "DataArray | DataTree"],
):
"""
A tree-like hierarchical collection of xarray objects.
Expand Down Expand Up @@ -701,8 +700,8 @@ def __contains__(self, key: object) -> bool:
def __bool__(self) -> bool:
return bool(self._data_variables) or bool(self._children)

def __iter__(self) -> Iterator[Hashable]:
return itertools.chain(self._data_variables, self._children)
def __iter__(self) -> Iterator[str]:
return itertools.chain(self._data_variables, self._children) # type: ignore

def __array__(self, dtype=None, copy=None):
raise TypeError(
Expand Down
Loading

0 comments on commit 2de4d27

Please sign in to comment.