Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Jan 29, 2024
1 parent 59ea7aa commit e293a7d
Show file tree
Hide file tree
Showing 18 changed files with 44 additions and 60 deletions.
9 changes: 4 additions & 5 deletions xarray/datatree_/datatree/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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.
"""
Expand Down
36 changes: 14 additions & 22 deletions xarray/datatree_/datatree/datatree.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand All @@ -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 (
Expand All @@ -63,6 +54,7 @@

if TYPE_CHECKING:
import pandas as pd

from xarray.core.merge import CoercibleValue
from xarray.core.types import ErrorOptions

Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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 = {}
Expand All @@ -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 = {}
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
1 change: 0 additions & 1 deletion xarray/datatree_/datatree/extensions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from xarray.core.extensions import _register_accessor

from xarray.datatree_.datatree.datatree import DataTree


Expand Down
1 change: 0 additions & 1 deletion xarray/datatree_/datatree/formatting.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
3 changes: 2 additions & 1 deletion xarray/datatree_/datatree/formatting_html.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
1 change: 0 additions & 1 deletion xarray/datatree_/datatree/io.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from xarray import Dataset, open_dataset

from xarray.datatree_.datatree.datatree import DataTree, NodePath


Expand Down
7 changes: 4 additions & 3 deletions xarray/datatree_/datatree/iterators.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -60,15 +61,15 @@ 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
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)]


Expand Down
5 changes: 2 additions & 3 deletions xarray/datatree_/datatree/mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
1 change: 0 additions & 1 deletion xarray/datatree_/datatree/ops.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import textwrap

from xarray import Dataset

from xarray.datatree_.datatree.mapping import map_over_subtree

"""
Expand Down
4 changes: 2 additions & 2 deletions xarray/datatree_/datatree/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -61,7 +61,7 @@ def __init__(self):
)


class RenderTree(object):
class RenderTree:
def __init__(
self, node: "DataTree", style=ContStyle(), childiter=list, maxlevel=None
):
Expand Down
3 changes: 1 addition & 2 deletions xarray/datatree_/datatree/testing.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion xarray/datatree_/datatree/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest
import xarray as xr

import xarray as xr
from datatree import DataTree


Expand Down
2 changes: 1 addition & 1 deletion xarray/datatree_/datatree/tests/test_dataset_api.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
6 changes: 3 additions & 3 deletions xarray/datatree_/datatree/tests/test_datatree.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 1 addition & 2 deletions xarray/datatree_/datatree/tests/test_formatting.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
2 changes: 1 addition & 1 deletion xarray/datatree_/datatree/tests/test_formatting_html.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest
import xarray as xr

import xarray as xr
from datatree import DataTree, formatting_html


Expand Down
2 changes: 1 addition & 1 deletion xarray/datatree_/datatree/tests/test_mapping.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
16 changes: 7 additions & 9 deletions xarray/datatree_/datatree/treenode.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit e293a7d

Please sign in to comment.