Skip to content

Commit 9965a8b

Browse files
authored
Merge pull request #144 from csiro-coasts/task/py3.10-type-hints
Upgrade to Python 3.10 using pyupgrade
2 parents 628f441 + ed2c4ff commit 9965a8b

File tree

27 files changed

+156
-174
lines changed

27 files changed

+156
-174
lines changed

docs/developing/grass.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import enum
33
from collections.abc import Hashable, Sequence
44
from functools import cached_property
5-
from typing import Optional
65

76
import numpy
87
import xarray
@@ -33,7 +32,7 @@ class Grass(DimensionConvention[GrassGridKind, GrassIndex]):
3332
default_grid_kind = GrassGridKind.field
3433

3534
@classmethod
36-
def check_dataset(cls, dataset: xarray.Dataset) -> Optional[int]:
35+
def check_dataset(cls, dataset: xarray.Dataset) -> int | None:
3736
# A Grass dataset is recognised by the 'Conventions' global attribute
3837
if dataset.attrs['Conventions'] == 'Grass 1.0':
3938
return Specificity.HIGH

docs/roles.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import re
2-
from collections.abc import Iterable
3-
from typing import Callable, cast
2+
from collections.abc import Callable, Iterable
3+
from typing import cast
44

55
import yaml
66
from docutils import nodes, utils
@@ -81,7 +81,7 @@ class Citation(Directive):
8181

8282
def load_citation_file(self) -> dict:
8383
citation_file = self.options['citation_file']
84-
with open(citation_file, 'r') as f:
84+
with open(citation_file) as f:
8585
return cast(dict, yaml.load(f, yaml.Loader))
8686

8787
def run(self) -> list[nodes.Node]:

scripts/min_deps_check.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def parse_requirements(
5555
5656
Yield (package name, major version, minor version, patch version)
5757
"""
58-
for line_number, line in enumerate(open(fname, 'r'), start=1):
58+
for line_number, line in enumerate(open(fname), start=1):
5959
if '#' in line:
6060
line = line[:line.index('#')]
6161
line = line.strip()

scripts/release.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import shlex
77
import subprocess
88
import sys
9-
from typing import Optional
109

1110
PROJECT = pathlib.Path(__file__).parent.parent
1211

@@ -29,7 +28,7 @@
2928

3029

3130
def main(
32-
args: Optional[list[str]] = None,
31+
args: list[str] | None = None,
3332
) -> None:
3433
parser = argparse.ArgumentParser()
3534
add_options(parser)
@@ -222,7 +221,7 @@ def output(*args: str) -> bytes:
222221

223222
def yn(
224223
prompt: str,
225-
default: Optional[bool] = None,
224+
default: bool | None = None,
226225
) -> bool:
227226
examples = {True: '[Yn]', False: '[yN]', None: '[yn]'}[default]
228227
prompt = f'{prompt.strip()} {examples} '

src/emsarray/cli/command.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import abc
22
import argparse
3-
from typing import Optional
43

54
from emsarray.cli import utils
65

@@ -24,11 +23,11 @@ def name(self) -> str:
2423

2524
#: A short description of what this subcommand does,
2625
#: shown as part of the usage message for the base command.
27-
help: Optional[str] = None
26+
help: str | None = None
2827

2928
#: A longer description of what this subcommand does,
3029
#: shown as part of the usage message for this subcommand.
31-
description: Optional[str] = None
30+
description: str | None = None
3231

3332
def add_parser(self, subparsers: argparse._SubParsersAction) -> None:
3433
parser = subparsers.add_parser(

src/emsarray/cli/commands/export_geometry.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import argparse
22
import logging
3+
from collections.abc import Callable
34
from pathlib import Path
4-
from typing import Callable
55

66
import xarray
77

src/emsarray/cli/commands/plot.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import argparse
22
import functools
33
import logging
4+
from collections.abc import Callable
45
from pathlib import Path
5-
from typing import Any, Callable, Optional, TypeVar
6+
from typing import Any, TypeVar
67

78
import emsarray
89
from emsarray.cli import BaseCommand, CommandException
@@ -28,7 +29,7 @@ def __init__(
2829
dest: str,
2930
*,
3031
value_type: Callable = str,
31-
default: Optional[dict[str, Any]] = None,
32+
default: dict[str, Any] | None = None,
3233
**kwargs: Any,
3334
):
3435
if default is None:
@@ -42,7 +43,7 @@ def __call__(
4243
parser: argparse.ArgumentParser,
4344
namespace: argparse.Namespace,
4445
values: Any,
45-
option_string: Optional[str] = None,
46+
option_string: str | None = None,
4647
) -> None:
4748
super().__call__
4849
holder = getattr(namespace, self.dest, {})

src/emsarray/cli/utils.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
import re
1010
import sys
1111
import textwrap
12-
from collections.abc import Iterator
12+
from collections.abc import Callable, Iterator
1313
from functools import wraps
1414
from pathlib import Path
15-
from typing import Callable, Optional, Protocol
15+
from typing import Protocol
1616

1717
from shapely.geometry import box, shape
1818
from shapely.geometry.base import BaseGeometry
@@ -30,7 +30,7 @@
3030
class MainCallable(Protocol):
3131
def __call__(
3232
self,
33-
argv: Optional[list[str]] = None,
33+
argv: list[str] | None = None,
3434
handle_errors: bool = True,
3535
) -> None:
3636
...
@@ -112,7 +112,7 @@ def decorator(
112112
) -> MainCallable:
113113
@wraps(fn)
114114
def wrapper(
115-
argv: Optional[list[str]] = None,
115+
argv: list[str] | None = None,
116116
handle_errors: bool = True,
117117
) -> None:
118118
parser = argparse.ArgumentParser(
@@ -172,7 +172,7 @@ def nice_console_errors() -> Iterator:
172172

173173
class DoubleNewlineDescriptionFormatter(argparse.HelpFormatter):
174174
def _fill_text(self, text: str, width: int, indent: str) -> str:
175-
fill_text = super(DoubleNewlineDescriptionFormatter, self)._fill_text
175+
fill_text = super()._fill_text
176176

177177
return '\n\n'.join(
178178
fill_text(paragraph, width, indent)

src/emsarray/compat/shapely.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import warnings
22
from collections.abc import Iterable
3-
from typing import Generic, TypeVar, Union, cast
3+
from typing import Generic, TypeVar, cast
44

55
import numpy
66
import shapely
@@ -31,7 +31,7 @@ class SpatialIndex(Generic[T]):
3131

3232
def __init__(
3333
self,
34-
items: Union[numpy.ndarray, Iterable[tuple[BaseGeometry, T]]],
34+
items: numpy.ndarray | Iterable[tuple[BaseGeometry, T]],
3535
):
3636
self.items = numpy.array(items, dtype=self.dtype)
3737

src/emsarray/conventions/_base.py

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
import enum
44
import logging
55
import warnings
6-
from collections.abc import Hashable, Iterable, Sequence
6+
from collections.abc import Callable, Hashable, Iterable, Sequence
77
from functools import cached_property
8-
from typing import (
9-
TYPE_CHECKING, Any, Callable, Generic, Optional, TypeVar, Union, cast
10-
)
8+
from typing import TYPE_CHECKING, Any, Generic, TypeVar, cast
119

1210
import numpy
1311
import xarray
@@ -166,7 +164,7 @@ def check_validity(cls, dataset: xarray.Dataset) -> None:
166164

167165
@classmethod
168166
@abc.abstractmethod
169-
def check_dataset(cls, dataset: xarray.Dataset) -> Optional[int]:
167+
def check_dataset(cls, dataset: xarray.Dataset) -> int | None:
170168
"""
171169
Check if a dataset uses this convention.
172170
@@ -582,7 +580,7 @@ def wind_index(
582580
self,
583581
linear_index: int,
584582
*,
585-
grid_kind: Optional[GridKind] = None,
583+
grid_kind: GridKind | None = None,
586584
) -> Index:
587585
"""Convert a linear index to a conventnion native index.
588586
@@ -635,7 +633,7 @@ def wind_index(
635633
def unravel_index(
636634
self,
637635
linear_index: int,
638-
grid_kind: Optional[GridKind] = None,
636+
grid_kind: GridKind | None = None,
639637
) -> Index:
640638
"""An alias for :meth:`Convention.wind_index()`.
641639
@@ -772,7 +770,7 @@ def ravel(
772770
self,
773771
data_array: xarray.DataArray,
774772
*,
775-
linear_dimension: Optional[Hashable] = None,
773+
linear_dimension: Hashable | None = None,
776774
) -> xarray.DataArray:
777775
"""
778776
Flatten the surface dimensions of a :class:`~xarray.DataArray`,
@@ -815,9 +813,9 @@ def wind(
815813
self,
816814
data_array: xarray.DataArray,
817815
*,
818-
grid_kind: Optional[GridKind] = None,
819-
axis: Optional[int] = None,
820-
linear_dimension: Optional[Hashable] = None,
816+
grid_kind: GridKind | None = None,
817+
axis: int | None = None,
818+
linear_dimension: Hashable | None = None,
821819
) -> xarray.DataArray:
822820
"""
823821
Wind a flattened :class:`~xarray.DataArray`
@@ -935,9 +933,9 @@ def data_crs(self) -> 'CRS':
935933
def plot_on_figure(
936934
self,
937935
figure: 'Figure',
938-
scalar: Optional[DataArrayOrName] = None,
939-
vector: Optional[tuple[DataArrayOrName, DataArrayOrName]] = None,
940-
title: Optional[str] = None,
936+
scalar: DataArrayOrName | None = None,
937+
vector: tuple[DataArrayOrName, DataArrayOrName] | None = None,
938+
title: str | None = None,
941939
**kwargs: Any,
942940
) -> None:
943941
"""Plot values for a :class:`~xarray.DataArray`
@@ -1015,10 +1013,10 @@ def plot(self, *args: Any, **kwargs: Any) -> None:
10151013
def animate_on_figure(
10161014
self,
10171015
figure: 'Figure',
1018-
scalar: Optional[DataArrayOrName] = None,
1019-
vector: Optional[tuple[DataArrayOrName, DataArrayOrName]] = None,
1020-
coordinate: Optional[DataArrayOrName] = None,
1021-
title: Optional[Union[str, Callable[[Any], str]]] = None,
1016+
scalar: DataArrayOrName | None = None,
1017+
vector: tuple[DataArrayOrName, DataArrayOrName] | None = None,
1018+
coordinate: DataArrayOrName | None = None,
1019+
title: str | Callable[[Any], str] | None = None,
10221020
**kwargs: Any,
10231021
) -> 'FuncAnimation':
10241022
"""
@@ -1115,7 +1113,7 @@ def animate_on_figure(
11151113
@utils.timed_func
11161114
def make_poly_collection(
11171115
self,
1118-
data_array: Optional[DataArrayOrName] = None,
1116+
data_array: DataArrayOrName | None = None,
11191117
**kwargs: Any,
11201118
) -> 'PolyCollection':
11211119
"""
@@ -1192,7 +1190,7 @@ def make_poly_collection(
11921190

11931191
def make_patch_collection(
11941192
self,
1195-
data_array: Optional[DataArrayOrName] = None,
1193+
data_array: DataArrayOrName | None = None,
11961194
**kwargs: Any,
11971195
) -> 'PolyCollection':
11981196
warnings.warn(
@@ -1206,8 +1204,8 @@ def make_patch_collection(
12061204
def make_quiver(
12071205
self,
12081206
axes: 'Axes',
1209-
u: Optional[DataArrayOrName] = None,
1210-
v: Optional[DataArrayOrName] = None,
1207+
u: DataArrayOrName | None = None,
1208+
v: DataArrayOrName | None = None,
12111209
**kwargs: Any,
12121210
) -> 'Quiver':
12131211
"""
@@ -1238,7 +1236,7 @@ def make_quiver(
12381236
# sometimes preferring to fill them in later,
12391237
# so `u` and `v` are optional.
12401238
# If they are not provided, we set default quiver values of `numpy.nan`.
1241-
values: Union[tuple[numpy.ndarray, numpy.ndarray], tuple[float, float]]
1239+
values: tuple[numpy.ndarray, numpy.ndarray] | tuple[float, float]
12421240
values = numpy.nan, numpy.nan
12431241

12441242
if u is not None and v is not None:
@@ -1331,7 +1329,7 @@ def mask(self) -> numpy.ndarray:
13311329
return cast(numpy.ndarray, mask)
13321330

13331331
@cached_property
1334-
def geometry(self) -> Union[Polygon, MultiPolygon]:
1332+
def geometry(self) -> Polygon | MultiPolygon:
13351333
"""
13361334
A :class:`shapely.Polygon` or :class:`shapely.MultiPolygon` that represents
13371335
the geometry of the entire dataset.
@@ -1438,7 +1436,7 @@ def spatial_index(self) -> SpatialIndex[SpatialIndexItem[Index]]:
14381436
def get_index_for_point(
14391437
self,
14401438
point: Point,
1441-
) -> Optional[SpatialIndexItem[Index]]:
1439+
) -> SpatialIndexItem[Index] | None:
14421440
"""
14431441
Find the index for a :class:`~shapely.Point` in the dataset.
14441442
@@ -1761,8 +1759,8 @@ def ocean_floor(self) -> xarray.Dataset:
17611759
def normalize_depth_variables(
17621760
self,
17631761
*,
1764-
positive_down: Optional[bool] = None,
1765-
deep_to_shallow: Optional[bool] = None,
1762+
positive_down: bool | None = None,
1763+
deep_to_shallow: bool | None = None,
17661764
) -> xarray.Dataset:
17671765
"""An alias for :func:`emsarray.operations.depth.normalize_depth_variables`"""
17681766
return depth.normalize_depth_variables(
@@ -1895,7 +1893,7 @@ def wind_index(
18951893
self,
18961894
linear_index: int,
18971895
*,
1898-
grid_kind: Optional[GridKind] = None,
1896+
grid_kind: GridKind | None = None,
18991897
) -> Index:
19001898
if grid_kind is None:
19011899
grid_kind = self.default_grid_kind
@@ -1907,7 +1905,7 @@ def ravel(
19071905
self,
19081906
data_array: xarray.DataArray,
19091907
*,
1910-
linear_dimension: Optional[Hashable] = None,
1908+
linear_dimension: Hashable | None = None,
19111909
) -> xarray.DataArray:
19121910
kind = self.get_grid_kind(data_array)
19131911
dimensions = self.grid_dimensions[kind]
@@ -1919,9 +1917,9 @@ def wind(
19191917
self,
19201918
data_array: xarray.DataArray,
19211919
*,
1922-
grid_kind: Optional[GridKind] = None,
1923-
axis: Optional[int] = None,
1924-
linear_dimension: Optional[Hashable] = None,
1920+
grid_kind: GridKind | None = None,
1921+
axis: int | None = None,
1922+
linear_dimension: Hashable | None = None,
19251923
) -> xarray.DataArray:
19261924
if axis is not None:
19271925
linear_dimension = data_array.dims[axis]

0 commit comments

Comments
 (0)