From e293a7d1fdd0adb9089b294886768448c0965a92 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 29 Jan 2024 18:56:43 +0000 Subject: [PATCH] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- xarray/datatree_/datatree/common.py | 9 +++-- xarray/datatree_/datatree/datatree.py | 36 ++++++++----------- xarray/datatree_/datatree/extensions.py | 1 - xarray/datatree_/datatree/formatting.py | 1 - xarray/datatree_/datatree/formatting_html.py | 3 +- xarray/datatree_/datatree/io.py | 1 - xarray/datatree_/datatree/iterators.py | 7 ++-- xarray/datatree_/datatree/mapping.py | 5 ++- xarray/datatree_/datatree/ops.py | 1 - xarray/datatree_/datatree/render.py | 4 +-- xarray/datatree_/datatree/testing.py | 3 +- xarray/datatree_/datatree/tests/conftest.py | 2 +- .../datatree/tests/test_dataset_api.py | 2 +- .../datatree_/datatree/tests/test_datatree.py | 6 ++-- .../datatree/tests/test_formatting.py | 3 +- .../datatree/tests/test_formatting_html.py | 2 +- .../datatree_/datatree/tests/test_mapping.py | 2 +- xarray/datatree_/datatree/treenode.py | 16 ++++----- 18 files changed, 44 insertions(+), 60 deletions(-) diff --git a/xarray/datatree_/datatree/common.py b/xarray/datatree_/datatree/common.py index e4d52925ede..f4f74337c50 100644 --- a/xarray/datatree_/datatree/common.py +++ b/xarray/datatree_/datatree/common.py @@ -6,8 +6,9 @@ """ import warnings +from collections.abc import Hashable, Iterable, Mapping from contextlib import suppress -from typing import Any, Hashable, Iterable, List, Mapping +from typing import Any class TreeAttrAccessMixin: @@ -83,16 +84,14 @@ def __setattr__(self, name: str, value: Any) -> None: except AttributeError as e: # Don't accidentally shadow custom AttributeErrors, e.g. # DataArray.dims.setter - if str(e) != "{!r} object has no attribute {!r}".format( - type(self).__name__, name - ): + if str(e) != f"{type(self).__name__!r} object has no attribute {name!r}": raise raise AttributeError( f"cannot set attribute {name!r} on a {type(self).__name__!r} object. Use __setitem__ style" "assignment (e.g., `ds['name'] = ...`) instead of assigning variables." ) from e - def __dir__(self) -> List[str]: + def __dir__(self) -> list[str]: """Provide method name lookup and completion. Only provide 'public' methods. """ diff --git a/xarray/datatree_/datatree/datatree.py b/xarray/datatree_/datatree/datatree.py index 61636012ba2..902215a3362 100644 --- a/xarray/datatree_/datatree/datatree.py +++ b/xarray/datatree_/datatree/datatree.py @@ -3,22 +3,14 @@ import copy import itertools from collections import OrderedDict +from collections.abc import Hashable, Iterable, Iterator, Mapping, MutableMapping from html import escape from typing import ( TYPE_CHECKING, Any, Callable, - Dict, Generic, - Hashable, - Iterable, - Iterator, - List, - Mapping, - MutableMapping, Optional, - Set, - Tuple, Union, overload, ) @@ -39,7 +31,6 @@ maybe_wrap_array, ) from xarray.core.variable import Variable - from xarray.datatree_.datatree import formatting, formatting_html from xarray.datatree_.datatree.common import TreeAttrAccessMixin from xarray.datatree_.datatree.mapping import ( @@ -63,6 +54,7 @@ if TYPE_CHECKING: import pandas as pd + from xarray.core.merge import CoercibleValue from xarray.core.types import ErrorOptions @@ -343,14 +335,14 @@ class DataTree( _name: Optional[str] _parent: Optional[DataTree] _children: OrderedDict[str, DataTree] - _attrs: Optional[Dict[Hashable, Any]] - _cache: Dict[str, Any] - _coord_names: Set[Hashable] - _dims: Dict[Hashable, int] - _encoding: Optional[Dict[Hashable, Any]] + _attrs: Optional[dict[Hashable, Any]] + _cache: dict[str, Any] + _coord_names: set[Hashable] + _dims: dict[Hashable, int] + _encoding: Optional[dict[Hashable, Any]] _close: Optional[Callable[[], None]] - _indexes: Dict[Hashable, Index] - _variables: Dict[Hashable, Variable] + _indexes: dict[Hashable, Index] + _variables: dict[Hashable, Variable] __slots__ = ( "_name", @@ -526,7 +518,7 @@ def variables(self) -> Mapping[Hashable, Variable]: return Frozen(self._variables) @property - def attrs(self) -> Dict[Hashable, Any]: + def attrs(self) -> dict[Hashable, Any]: """Dictionary of global attributes on this node object.""" if self._attrs is None: self._attrs = {} @@ -537,7 +529,7 @@ def attrs(self, value: Mapping[Any, Any]) -> None: self._attrs = dict(value) @property - def encoding(self) -> Dict: + def encoding(self) -> dict: """Dictionary of global encoding attributes on this node object.""" if self._encoding is None: self._encoding = {} @@ -592,7 +584,7 @@ def _item_sources(self) -> Iterable[Mapping[Any, Any]]: # immediate child nodes yield self.children - def _ipython_key_completions_(self) -> List[str]: + def _ipython_key_completions_(self) -> list[str]: """Provide method for the key-autocompletions in IPython. See http://ipython.readthedocs.io/en/stable/config/integrating.html#tab-completion For the details. @@ -1088,7 +1080,7 @@ def from_dict( return obj - def to_dict(self) -> Dict[str, Dataset]: + def to_dict(self) -> dict[str, Dataset]: """ Create a dictionary mapping of absolute node paths to the data contained in those nodes. @@ -1316,7 +1308,7 @@ def map_over_subtree( func: Callable, *args: Iterable[Any], **kwargs: Any, - ) -> DataTree | Tuple[DataTree]: + ) -> DataTree | tuple[DataTree]: """ Apply a function to every dataset in this subtree, returning a new tree which stores the results. diff --git a/xarray/datatree_/datatree/extensions.py b/xarray/datatree_/datatree/extensions.py index 51a8ee577ab..57ee9170b73 100644 --- a/xarray/datatree_/datatree/extensions.py +++ b/xarray/datatree_/datatree/extensions.py @@ -1,5 +1,4 @@ from xarray.core.extensions import _register_accessor - from xarray.datatree_.datatree.datatree import DataTree diff --git a/xarray/datatree_/datatree/formatting.py b/xarray/datatree_/datatree/formatting.py index 070dd09ca5b..e47abbdbc92 100644 --- a/xarray/datatree_/datatree/formatting.py +++ b/xarray/datatree_/datatree/formatting.py @@ -1,7 +1,6 @@ from typing import TYPE_CHECKING from xarray.core.formatting import _compat_to_str, diff_dataset_repr - from xarray.datatree_.datatree.mapping import diff_treestructure from xarray.datatree_.datatree.render import RenderTree diff --git a/xarray/datatree_/datatree/formatting_html.py b/xarray/datatree_/datatree/formatting_html.py index 4531f5aec18..0917c4d0da3 100644 --- a/xarray/datatree_/datatree/formatting_html.py +++ b/xarray/datatree_/datatree/formatting_html.py @@ -1,6 +1,7 @@ +from collections.abc import Mapping from functools import partial from html import escape -from typing import Any, Mapping +from typing import Any from xarray.core.formatting_html import ( _mapping_section, diff --git a/xarray/datatree_/datatree/io.py b/xarray/datatree_/datatree/io.py index cd6d5d0ec17..5b5b19f4da4 100644 --- a/xarray/datatree_/datatree/io.py +++ b/xarray/datatree_/datatree/io.py @@ -1,5 +1,4 @@ from xarray import Dataset, open_dataset - from xarray.datatree_.datatree.datatree import DataTree, NodePath diff --git a/xarray/datatree_/datatree/iterators.py b/xarray/datatree_/datatree/iterators.py index a3c092b004c..81207c4b13c 100644 --- a/xarray/datatree_/datatree/iterators.py +++ b/xarray/datatree_/datatree/iterators.py @@ -1,6 +1,7 @@ from abc import abstractmethod from collections import abc -from typing import Callable, Iterator, List, Optional +from collections.abc import Iterator +from typing import Callable, Optional from xarray.datatree_.datatree.treenode import Tree @@ -60,7 +61,7 @@ def __next__(self) -> Iterator[Tree]: @staticmethod @abstractmethod - def _iter(children: List[Tree], filter_, stop, maxlevel) -> Iterator[Tree]: + def _iter(children: list[Tree], filter_, stop, maxlevel) -> Iterator[Tree]: ... @staticmethod @@ -68,7 +69,7 @@ def _abort_at_level(level, maxlevel): return maxlevel is not None and level > maxlevel @staticmethod - def _get_children(children: List[Tree], stop) -> List[Tree]: + def _get_children(children: list[Tree], stop) -> list[Tree]: return [child for child in children if not stop(child)] diff --git a/xarray/datatree_/datatree/mapping.py b/xarray/datatree_/datatree/mapping.py index 9b008c2e840..c49bd807b3f 100644 --- a/xarray/datatree_/datatree/mapping.py +++ b/xarray/datatree_/datatree/mapping.py @@ -4,10 +4,9 @@ import sys from itertools import repeat from textwrap import dedent -from typing import TYPE_CHECKING, Callable, Tuple +from typing import TYPE_CHECKING, Callable from xarray import DataArray, Dataset - from xarray.datatree_.datatree.iterators import LevelOrderIter from xarray.datatree_.datatree.treenode import NodePath, TreeNode @@ -154,7 +153,7 @@ def map_over_subtree(func: Callable) -> Callable: # TODO inspect function to work out immediately if the wrong number of arguments were passed for it? @functools.wraps(func) - def _map_over_subtree(*args, **kwargs) -> DataTree | Tuple[DataTree, ...]: + def _map_over_subtree(*args, **kwargs) -> DataTree | tuple[DataTree, ...]: """Internal function which maps func over every node in tree, returning a tree of the results.""" from xarray.datatree_.datatree.datatree import DataTree diff --git a/xarray/datatree_/datatree/ops.py b/xarray/datatree_/datatree/ops.py index b4687447be4..7275ba84a34 100644 --- a/xarray/datatree_/datatree/ops.py +++ b/xarray/datatree_/datatree/ops.py @@ -1,7 +1,6 @@ import textwrap from xarray import Dataset - from xarray.datatree_.datatree.mapping import map_over_subtree """ diff --git a/xarray/datatree_/datatree/render.py b/xarray/datatree_/datatree/render.py index 7f407606722..cc539d2f333 100644 --- a/xarray/datatree_/datatree/render.py +++ b/xarray/datatree_/datatree/render.py @@ -11,7 +11,7 @@ Row = collections.namedtuple("Row", ("pre", "fill", "node")) -class AbstractStyle(object): +class AbstractStyle: def __init__(self, vertical, cont, end): """ Tree Render Style. @@ -61,7 +61,7 @@ def __init__(self): ) -class RenderTree(object): +class RenderTree: def __init__( self, node: "DataTree", style=ContStyle(), childiter=list, maxlevel=None ): diff --git a/xarray/datatree_/datatree/testing.py b/xarray/datatree_/datatree/testing.py index 658056cf795..576201b7b09 100644 --- a/xarray/datatree_/datatree/testing.py +++ b/xarray/datatree_/datatree/testing.py @@ -1,7 +1,6 @@ -from xarray.testing.assertions import ensure_warnings - from xarray.datatree_.datatree.datatree import DataTree from xarray.datatree_.datatree.formatting import diff_tree_repr +from xarray.testing.assertions import ensure_warnings @ensure_warnings diff --git a/xarray/datatree_/datatree/tests/conftest.py b/xarray/datatree_/datatree/tests/conftest.py index 3ed1325ccd5..8965b097cd1 100644 --- a/xarray/datatree_/datatree/tests/conftest.py +++ b/xarray/datatree_/datatree/tests/conftest.py @@ -1,6 +1,6 @@ import pytest -import xarray as xr +import xarray as xr from datatree import DataTree diff --git a/xarray/datatree_/datatree/tests/test_dataset_api.py b/xarray/datatree_/datatree/tests/test_dataset_api.py index 6879b869299..c59633ae4cb 100644 --- a/xarray/datatree_/datatree/tests/test_dataset_api.py +++ b/xarray/datatree_/datatree/tests/test_dataset_api.py @@ -1,6 +1,6 @@ import numpy as np -import xarray as xr +import xarray as xr from datatree import DataTree from datatree.testing import assert_equal diff --git a/xarray/datatree_/datatree/tests/test_datatree.py b/xarray/datatree_/datatree/tests/test_datatree.py index 3815f99bcb2..84aa8f2d6a1 100644 --- a/xarray/datatree_/datatree/tests/test_datatree.py +++ b/xarray/datatree_/datatree/tests/test_datatree.py @@ -2,12 +2,12 @@ import numpy as np import pytest -import xarray as xr -import xarray.testing as xrt -from xarray.tests import create_test_data, source_ndarray import datatree.testing as dtt +import xarray as xr +import xarray.testing as xrt from datatree import DataTree, NotFoundInTreeError +from xarray.tests import create_test_data, source_ndarray class TestTreeCreation: diff --git a/xarray/datatree_/datatree/tests/test_formatting.py b/xarray/datatree_/datatree/tests/test_formatting.py index 0f64644c05a..7f77db07aa0 100644 --- a/xarray/datatree_/datatree/tests/test_formatting.py +++ b/xarray/datatree_/datatree/tests/test_formatting.py @@ -1,9 +1,8 @@ from textwrap import dedent -from xarray import Dataset - from datatree import DataTree from datatree.formatting import diff_tree_repr +from xarray import Dataset class TestRepr: diff --git a/xarray/datatree_/datatree/tests/test_formatting_html.py b/xarray/datatree_/datatree/tests/test_formatting_html.py index 7c6a47ea86e..857962394c8 100644 --- a/xarray/datatree_/datatree/tests/test_formatting_html.py +++ b/xarray/datatree_/datatree/tests/test_formatting_html.py @@ -1,6 +1,6 @@ import pytest -import xarray as xr +import xarray as xr from datatree import DataTree, formatting_html diff --git a/xarray/datatree_/datatree/tests/test_mapping.py b/xarray/datatree_/datatree/tests/test_mapping.py index 929ce7644dd..90d7a9d5735 100644 --- a/xarray/datatree_/datatree/tests/test_mapping.py +++ b/xarray/datatree_/datatree/tests/test_mapping.py @@ -1,7 +1,7 @@ import numpy as np import pytest -import xarray as xr +import xarray as xr from datatree.datatree import DataTree from datatree.mapping import TreeIsomorphismError, check_isomorphic, map_over_subtree from datatree.testing import assert_equal diff --git a/xarray/datatree_/datatree/treenode.py b/xarray/datatree_/datatree/treenode.py index 01529e82bdf..91d813aaf1b 100644 --- a/xarray/datatree_/datatree/treenode.py +++ b/xarray/datatree_/datatree/treenode.py @@ -2,14 +2,12 @@ import sys from collections import OrderedDict +from collections.abc import Iterator, Mapping from pathlib import PurePosixPath from typing import ( TYPE_CHECKING, Generic, - Iterator, - Mapping, Optional, - Tuple, TypeVar, Union, ) @@ -242,7 +240,7 @@ def _iter_parents(self: Tree) -> Iterator[Tree]: yield node node = node.parent - def iter_lineage(self: Tree) -> Tuple[Tree, ...]: + def iter_lineage(self: Tree) -> tuple[Tree, ...]: """Iterate up the tree, starting from the current node.""" from warnings import warn @@ -254,7 +252,7 @@ def iter_lineage(self: Tree) -> Tuple[Tree, ...]: return tuple((self, *self.parents)) @property - def lineage(self: Tree) -> Tuple[Tree, ...]: + def lineage(self: Tree) -> tuple[Tree, ...]: """All parent nodes and their parent nodes, starting with the closest.""" from warnings import warn @@ -266,12 +264,12 @@ def lineage(self: Tree) -> Tuple[Tree, ...]: return self.iter_lineage() @property - def parents(self: Tree) -> Tuple[Tree, ...]: + def parents(self: Tree) -> tuple[Tree, ...]: """All parent nodes and their parent nodes, starting with the closest.""" return tuple(self._iter_parents()) @property - def ancestors(self: Tree) -> Tuple[Tree, ...]: + def ancestors(self: Tree) -> tuple[Tree, ...]: """All parent nodes and their parent nodes, starting with the most distant.""" from warnings import warn @@ -306,7 +304,7 @@ def is_leaf(self) -> bool: return self.children == {} @property - def leaves(self: Tree) -> Tuple[Tree, ...]: + def leaves(self: Tree) -> tuple[Tree, ...]: """ All leaf nodes. @@ -346,7 +344,7 @@ def subtree(self: Tree) -> Iterator[Tree]: return iterators.PreOrderIter(self) @property - def descendants(self: Tree) -> Tuple[Tree, ...]: + def descendants(self: Tree) -> tuple[Tree, ...]: """ Child nodes and all their child nodes.