Skip to content

Commit cf18bcf

Browse files
Apply ruff/flake8-type-checking rule TCH003
TCH003 Move standard library import into a type-checking block
1 parent aeb992f commit cf18bcf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+145
-78
lines changed

ci/min_deps_check.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@
88

99
import itertools
1010
import sys
11-
from collections.abc import Iterator
1211
from datetime import datetime
12+
from typing import TYPE_CHECKING
1313

1414
import libmambapy # type: ignore[import]
1515
import yaml
1616
from dateutil.relativedelta import relativedelta
1717

18+
if TYPE_CHECKING:
19+
from collections.abc import Iterator
20+
1821
CHANNELS = ["conda-forge", "defaults"]
1922
IGNORE_DEPS = {
2023
"coveralls",

xarray/backends/common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import os
55
import time
66
import traceback
7-
from collections.abc import Iterable, Mapping, Sequence
87
from glob import glob
98
from typing import TYPE_CHECKING, Any, ClassVar, TypeVar, cast, overload
109

@@ -18,6 +17,7 @@
1817
from xarray.namedarray.pycompat import is_chunked_array
1918

2019
if TYPE_CHECKING:
20+
from collections.abc import Iterable, Mapping, Sequence
2121
from io import BufferedIOBase
2222

2323
from xarray.core.dataset import Dataset

xarray/backends/file_manager.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@
22

33
import atexit
44
import contextlib
5-
import io
65
import threading
76
import uuid
87
import warnings
9-
from collections.abc import Hashable
10-
from typing import Any
8+
from typing import TYPE_CHECKING, Any
119

1210
from xarray.backends.locks import acquire
1311
from xarray.backends.lru_cache import LRUCache
1412
from xarray.core import utils
1513
from xarray.core.options import OPTIONS
1614

15+
if TYPE_CHECKING:
16+
import io
17+
from collections.abc import Hashable
18+
1719
# Global cache for storing open files.
1820
FILE_CACHE: LRUCache[Any, io.IOBase] = LRUCache(
1921
maxsize=OPTIONS["file_cache_maxsize"], on_evict=lambda k, v: v.close()

xarray/backends/h5netcdf_.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import functools
44
import io
55
import os
6-
from collections.abc import Iterable
76
from typing import TYPE_CHECKING, Any
87

98
import numpy as np
@@ -39,6 +38,7 @@
3938
from xarray.core.variable import Variable
4039

4140
if TYPE_CHECKING:
41+
from collections.abc import Iterable
4242
from io import BufferedIOBase
4343

4444
from xarray.backends.common import AbstractDataStore

xarray/backends/locks.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
import threading
55
import uuid
66
import weakref
7-
from collections.abc import Hashable, MutableMapping
8-
from typing import Any, ClassVar
7+
from typing import TYPE_CHECKING, Any, ClassVar
98
from weakref import WeakValueDictionary
109

10+
if TYPE_CHECKING:
11+
from collections.abc import Hashable, MutableMapping
12+
1113

1214
# SerializableLock is adapted from Dask:
1315
# https://github.com/dask/dask/blob/74e898f0ec712e8317ba86cc3b9d18b6b9922be0/dask/utils.py#L1160-L1224

xarray/backends/netCDF4_.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import functools
44
import operator
55
import os
6-
from collections.abc import Iterable
76
from contextlib import suppress
87
from typing import TYPE_CHECKING, Any
98

@@ -41,6 +40,7 @@
4140
from xarray.core.variable import Variable
4241

4342
if TYPE_CHECKING:
43+
from collections.abc import Iterable
4444
from io import BufferedIOBase
4545

4646
from h5netcdf.core import EnumType as h5EnumType

xarray/backends/plugins.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import inspect
55
import itertools
66
import warnings
7-
from collections.abc import Callable
87
from importlib.metadata import entry_points
98
from typing import TYPE_CHECKING, Any
109

@@ -13,6 +12,7 @@
1312

1413
if TYPE_CHECKING:
1514
import os
15+
from collections.abc import Callable
1616
from importlib.metadata import EntryPoint, EntryPoints
1717
from io import BufferedIOBase
1818

xarray/backends/pydap_.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import annotations
22

3-
from collections.abc import Iterable
43
from typing import TYPE_CHECKING, Any
54

65
import numpy as np
@@ -26,6 +25,7 @@
2625

2726
if TYPE_CHECKING:
2827
import os
28+
from collections.abc import Iterable
2929
from io import BufferedIOBase
3030

3131
from xarray.core.dataset import Dataset

xarray/backends/scipy_.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import gzip
44
import io
55
import os
6-
from collections.abc import Iterable
76
from typing import TYPE_CHECKING, Any
87

98
import numpy as np
@@ -34,6 +33,7 @@
3433
from xarray.core.variable import Variable
3534

3635
if TYPE_CHECKING:
36+
from collections.abc import Iterable
3737
from io import BufferedIOBase
3838

3939
from xarray.backends.common import AbstractDataStore

xarray/backends/store.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import annotations
22

3-
from collections.abc import Iterable
43
from typing import TYPE_CHECKING, Any
54

65
from xarray import conventions
@@ -13,6 +12,7 @@
1312

1413
if TYPE_CHECKING:
1514
import os
15+
from collections.abc import Iterable
1616
from io import BufferedIOBase
1717

1818

xarray/backends/zarr.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import json
55
import os
66
import struct
7-
from collections.abc import Hashable, Iterable, Mapping
87
from typing import TYPE_CHECKING, Any, Literal
98

109
import numpy as np
@@ -35,6 +34,7 @@
3534
from xarray.namedarray.utils import module_available
3635

3736
if TYPE_CHECKING:
37+
from collections.abc import Hashable, Iterable, Mapping
3838
from io import BufferedIOBase
3939

4040
from zarr import Group as ZarrGroup

xarray/coding/cftime_offsets.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444

4545
import re
4646
import warnings
47-
from collections.abc import Mapping
4847
from datetime import datetime, timedelta
4948
from functools import partial
5049
from typing import TYPE_CHECKING, ClassVar, Literal, TypeVar
@@ -76,6 +75,8 @@
7675

7776

7877
if TYPE_CHECKING:
78+
from collections.abc import Mapping
79+
7980
from xarray.core.types import InclusiveOptions, Self, SideOptions, TypeAlias
8081

8182

xarray/coding/variables.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from __future__ import annotations
44

55
import warnings
6-
from collections.abc import Callable, Hashable, MutableMapping
76
from functools import partial
87
from typing import TYPE_CHECKING, Any, Union
98

@@ -16,6 +15,8 @@
1615
from xarray.namedarray.pycompat import is_chunked_array
1716

1817
if TYPE_CHECKING:
18+
from collections.abc import Callable, Hashable, MutableMapping
19+
1920
T_VarTuple = tuple[tuple[Hashable, ...], Any, dict, dict]
2021
T_Name = Union[Hashable, None]
2122

xarray/core/_aggregations.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44

55
from __future__ import annotations
66

7-
from collections.abc import Callable, Sequence
87
from typing import TYPE_CHECKING, Any
98

109
from xarray.core import duck_array_ops
1110
from xarray.core.options import OPTIONS
1211
from xarray.core.utils import contains_only_chunked_or_numpy, module_available
1312

1413
if TYPE_CHECKING:
14+
from collections.abc import Callable, Sequence
15+
1516
from xarray.core.dataarray import DataArray
1617
from xarray.core.dataset import Dataset
1718
from xarray.core.types import Dims, Self

xarray/core/_typed_ops.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
from __future__ import annotations
66

77
import operator
8-
from collections.abc import Callable
98
from typing import TYPE_CHECKING, Any, overload
109

1110
from xarray.core import nputils, ops
1211

1312
if TYPE_CHECKING:
13+
from collections.abc import Callable
14+
1415
from xarray.core.dataarray import DataArray
1516
from xarray.core.dataset import Dataset
1617
from xarray.core.datatree import DataTree

xarray/core/accessor_str.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
import codecs
4343
import re
4444
import textwrap
45-
from collections.abc import Callable, Hashable, Mapping
4645
from functools import reduce
4746
from operator import or_ as set_union
4847
from re import Pattern
@@ -56,6 +55,8 @@
5655
from xarray.core.types import T_DataArray
5756

5857
if TYPE_CHECKING:
58+
from collections.abc import Callable, Hashable, Mapping
59+
5960
from numpy.typing import DTypeLike
6061

6162
from xarray.core.dataarray import DataArray

xarray/core/common.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import annotations
22

3-
import datetime
43
import warnings
54
from collections.abc import Callable, Hashable, Iterable, Iterator, Mapping
65
from contextlib import suppress
@@ -34,6 +33,8 @@
3433

3534

3635
if TYPE_CHECKING:
36+
import datetime
37+
3738
from numpy.typing import DTypeLike
3839

3940
from xarray.core.dataarray import DataArray

xarray/core/computation.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,6 @@
99
import operator
1010
import warnings
1111
from collections import Counter
12-
from collections.abc import (
13-
Callable,
14-
Hashable,
15-
Iterable,
16-
Iterator,
17-
Mapping,
18-
Sequence,
19-
Set,
20-
)
2112
from typing import TYPE_CHECKING, Any, Literal, TypeVar, Union, cast, overload
2213

2314
import numpy as np
@@ -38,6 +29,16 @@
3829
from xarray.util.deprecation_helpers import deprecate_dims
3930

4031
if TYPE_CHECKING:
32+
from collections.abc import (
33+
Callable,
34+
Hashable,
35+
Iterable,
36+
Iterator,
37+
Mapping,
38+
Sequence,
39+
Set,
40+
)
41+
4142
from xarray.core.coordinates import Coordinates
4243
from xarray.core.dataarray import DataArray
4344
from xarray.core.dataset import Dataset

xarray/core/concat.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import annotations
22

3-
from collections.abc import Hashable, Iterable
43
from typing import TYPE_CHECKING, Any, Union, overload
54

65
import numpy as np
@@ -21,6 +20,8 @@
2120
from xarray.core.variable import concat as concat_vars
2221

2322
if TYPE_CHECKING:
23+
from collections.abc import Hashable, Iterable
24+
2425
from xarray.core.types import (
2526
CombineAttrsOptions,
2627
CompatOptions,

xarray/core/dataarray.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import annotations
22

3-
import datetime
43
import warnings
54
from collections.abc import (
65
Callable,
@@ -11,8 +10,6 @@
1110
Sequence,
1211
)
1312
from functools import partial
14-
from os import PathLike
15-
from types import EllipsisType
1613
from typing import (
1714
TYPE_CHECKING,
1815
Any,
@@ -87,6 +84,10 @@
8784
from xarray.util.deprecation_helpers import _deprecate_positional_args, deprecate_dims
8885

8986
if TYPE_CHECKING:
87+
import datetime
88+
from os import PathLike
89+
from types import EllipsisType
90+
9091
from dask.dataframe import DataFrame as DaskDataFrame
9192
from dask.delayed import Delayed
9293
from iris.cube import Cube as iris_Cube

xarray/core/dataset.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import annotations
22

33
import copy
4-
import datetime
54
import inspect
65
import itertools
76
import math
@@ -22,8 +21,6 @@
2221
from html import escape
2322
from numbers import Number
2423
from operator import methodcaller
25-
from os import PathLike
26-
from types import EllipsisType
2724
from typing import IO, TYPE_CHECKING, Any, Generic, Literal, cast, overload
2825

2926
import numpy as np
@@ -132,6 +129,10 @@
132129
from xarray.util.deprecation_helpers import _deprecate_positional_args, deprecate_dims
133130

134131
if TYPE_CHECKING:
132+
import datetime
133+
from os import PathLike
134+
from types import EllipsisType
135+
135136
from dask.dataframe import DataFrame as DaskDataFrame
136137
from dask.delayed import Delayed
137138
from numpy.typing import ArrayLike

xarray/core/datatree_io.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
from __future__ import annotations
22

3-
from collections.abc import Mapping, MutableMapping
4-
from os import PathLike
53
from typing import TYPE_CHECKING, Any, Literal, get_args
64

75
if TYPE_CHECKING:
6+
from collections.abc import Mapping, MutableMapping
7+
from os import PathLike
8+
89
from xarray.core.datatree import DataTree
910
from xarray.core.types import NetcdfWriteModes, ZarrWriteModes
1011

0 commit comments

Comments
 (0)