Skip to content

Commit e6acc68

Browse files
committed
typing: use | None instead of Optional
1 parent 12b71ef commit e6acc68

File tree

13 files changed

+88
-97
lines changed

13 files changed

+88
-97
lines changed

src/stagpy/_helpers.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
import matplotlib.pyplot as plt
99

1010
if typing.TYPE_CHECKING:
11-
from typing import Optional
12-
1311
from matplotlib.figure import Figure
1412
from numpy.typing import NDArray
1513

@@ -24,7 +22,7 @@ def walk(sdat: StagyyData, conf: Config) -> StepsView:
2422
return sdat.snaps[conf.core.snapshots]
2523

2624

27-
def out_name(conf: Config, stem: str, timestep: Optional[int] = None) -> str:
25+
def out_name(conf: Config, stem: str, timestep: int | None = None) -> str:
2826
"""Return StagPy out file name.
2927
3028
Args:
@@ -62,7 +60,7 @@ def saveplot(
6260
conf: Config,
6361
fig: Figure,
6462
stem: str,
65-
timestep: Optional[int] = None,
63+
timestep: int | None = None,
6664
close: bool = True,
6765
) -> None:
6866
"""Save matplotlib figure.

src/stagpy/args.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from .config import Config
2727

2828
if typing.TYPE_CHECKING:
29-
from typing import Any, Callable, Optional
29+
from typing import Any, Callable
3030

3131

3232
def _sub(cmd: Any, *sections: str) -> Subcmd:
@@ -72,7 +72,7 @@ def _load_mplstyle(conf: Config) -> None:
7272

7373

7474
def parse_args(
75-
conf: Config, arglist: Optional[list[str]] = None
75+
conf: Config, arglist: list[str] | None = None
7676
) -> Callable[[Config], None]:
7777
"""Parse cmd line arguments.
7878

src/stagpy/commands.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from .stagyydata import _sdat_from_conf
1616

1717
if typing.TYPE_CHECKING:
18-
from typing import Callable, Iterable, Mapping, Optional, Sequence, Union
18+
from typing import Callable, Iterable, Mapping, Sequence, Union
1919

2020
from loam.base import Section
2121

@@ -56,7 +56,7 @@ def _pretty_print(
5656
key_val: Sequence[tuple[str, str]],
5757
sep: str = ": ",
5858
min_col_width: int = 39,
59-
text_width: Optional[int] = None,
59+
text_width: int | None = None,
6060
) -> None:
6161
"""Print a iterable of key/values.
6262

src/stagpy/config.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from dataclasses import dataclass
1010
from pathlib import Path
11-
from typing import Dict, Optional, Sequence, Union
11+
from typing import Dict, Sequence, Union
1212

1313
import loam.parsers as lprs
1414
from loam.base import ConfigBase, Section, entry
@@ -53,15 +53,15 @@ class Core(Section):
5353
class Plot(Section):
5454
"""Plotting."""
5555

56-
ratio: Optional[float] = MaybeEntry(float).entry(
56+
ratio: float | None = MaybeEntry(float).entry(
5757
doc="force aspect ratio of field plot", in_file=False
5858
)
5959
raster: bool = switch_opt(True, None, "rasterize field plots")
6060
format: str = entry(val="pdf", doc="figure format (pdf, eps, svg, png)")
61-
vmin: Optional[float] = MaybeEntry(float).entry(
61+
vmin: float | None = MaybeEntry(float).entry(
6262
doc="minimal value on plot", in_file=False
6363
)
64-
vmax: Optional[float] = MaybeEntry(float).entry(
64+
vmax: float | None = MaybeEntry(float).entry(
6565
doc="maximal value on plot", in_file=False
6666
)
6767
cminmax: bool = switch_opt(False, "C", "constant min max across plots")
@@ -97,18 +97,18 @@ class FieldSec(Section):
9797
default="T,stream", cli_short="o", doc="variables to plot (see stagpy var)"
9898
)
9999
perturbation: bool = switch_opt(False, None, "plot departure from average profile")
100-
shift: Optional[int] = MaybeEntry(int).entry(
100+
shift: int | None = MaybeEntry(int).entry(
101101
doc="shift plot horizontally", in_file=False
102102
)
103103
timelabel: bool = switch_opt(False, None, "add label with time")
104104
colorbar: bool = switch_opt(True, None, "add color bar to plot")
105-
ix: Optional[int] = MaybeEntry(int).entry(
105+
ix: int | None = MaybeEntry(int).entry(
106106
doc="x-index of slice for 3D fields", in_file=False
107107
)
108-
iy: Optional[int] = MaybeEntry(int).entry(
108+
iy: int | None = MaybeEntry(int).entry(
109109
doc="y-index of slice for 3D fields", in_file=False
110110
)
111-
iz: Optional[int] = MaybeEntry(int).entry(
111+
iz: int | None = MaybeEntry(int).entry(
112112
doc="z-index of slice for 3D fields", in_file=False
113113
)
114114
isocolors: Sequence[str] = TupleEntry(str).entry(doc="list of colors for isolines")
@@ -151,11 +151,9 @@ class Time(Section):
151151
compstat: Sequence[str] = TupleEntry(str).entry(
152152
doc="compute mean and rms of listed variables", in_file=False
153153
)
154-
tstart: Optional[float] = MaybeEntry(float).entry(
155-
doc="beginning time", in_file=False
156-
)
157-
tend: Optional[float] = MaybeEntry(float).entry(doc="end time", in_file=False)
158-
fraction: Optional[float] = MaybeEntry(float).entry(
154+
tstart: float | None = MaybeEntry(float).entry(doc="beginning time", in_file=False)
155+
tend: float | None = MaybeEntry(float).entry(doc="end time", in_file=False)
156+
fraction: float | None = MaybeEntry(float).entry(
159157
doc="ending fraction of series to process", in_file=False
160158
)
161159
marktimes: Sequence[float] = TupleEntry(float).entry(
@@ -200,7 +198,7 @@ class Plates(Section):
200198
False, None, "plot number of plates as function of time"
201199
)
202200
distribution: bool = switch_opt(False, None, "plot plate size distribution")
203-
zoom: Optional[float] = MaybeEntry(float).entry(
201+
zoom: float | None = MaybeEntry(float).entry(
204202
doc="zoom around surface", in_file=False
205203
)
206204

src/stagpy/field.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from .stagyydata import _sdat_from_conf
1818

1919
if typing.TYPE_CHECKING:
20-
from typing import Any, Iterable, Optional, Union
20+
from typing import Any, Iterable
2121

2222
from matplotlib.axes import Axes
2323
from matplotlib.collections import QuadMesh
@@ -40,9 +40,9 @@ def _threed_extract(
4040
"""Return suitable slices and coords for 3D fields."""
4141
is_vector = not valid_field_var(var)
4242
hwalls = is_vector or walls
43-
i_x: Optional[Union[int, slice]] = conf.field.ix
44-
i_y: Optional[Union[int, slice]] = conf.field.iy
45-
i_z: Optional[Union[int, slice]] = conf.field.iz
43+
i_x: int | slice | None = conf.field.ix
44+
i_y: int | slice | None = conf.field.iy
45+
i_z: int | slice | None = conf.field.iz
4646
if i_x is not None or i_y is not None:
4747
i_z = None
4848
if i_x is not None or i_z is not None:
@@ -177,11 +177,11 @@ def get_meshes_vec(
177177
def plot_scalar(
178178
step: Step,
179179
var: str,
180-
field: Optional[NDArray] = None,
181-
axis: Optional[Axes] = None,
182-
conf: Optional[Config] = None,
180+
field: NDArray | None = None,
181+
axis: Axes | None = None,
182+
conf: Config | None = None,
183183
**extra: Any,
184-
) -> tuple[Figure, Axes, QuadMesh, Optional[Colorbar]]:
184+
) -> tuple[Figure, Axes, QuadMesh, Colorbar | None]:
185185
"""Plot scalar field.
186186
187187
Args:
@@ -271,8 +271,8 @@ def plot_iso(
271271
axis: Axes,
272272
step: Step,
273273
var: str,
274-
field: Optional[NDArray] = None,
275-
conf: Optional[Config] = None,
274+
field: NDArray | None = None,
275+
conf: Config | None = None,
276276
**extra: Any,
277277
) -> None:
278278
"""Plot isocontours of scalar field.
@@ -311,7 +311,7 @@ def plot_vec(
311311
axis: Axes,
312312
step: Step,
313313
var: str,
314-
conf: Optional[Config] = None,
314+
conf: Config | None = None,
315315
) -> None:
316316
"""Plot vector field.
317317

src/stagpy/plates.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from .stagyydata import _sdat_from_conf
1919

2020
if typing.TYPE_CHECKING:
21-
from typing import Optional, Sequence, TextIO, Union
21+
from typing import Sequence, TextIO, Union
2222

2323
from matplotlib.axes import Axes
2424
from numpy.typing import NDArray
@@ -211,7 +211,7 @@ def _continents_location(snap: Step, at_surface: bool = True) -> NDArray:
211211
def plot_at_surface(
212212
snap: Step,
213213
names: Sequence[Sequence[Sequence[str]]],
214-
conf: Optional[Config],
214+
conf: Config | None,
215215
) -> None:
216216
"""Plot surface diagnostics.
217217
@@ -333,7 +333,7 @@ def _write_trench_diagnostics(
333333
def plot_scalar_field(
334334
snap: Step,
335335
fieldname: str,
336-
conf: Optional[Config] = None,
336+
conf: Config | None = None,
337337
) -> None:
338338
"""Plot scalar field with plate information.
339339

src/stagpy/processing.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
from .error import NotAvailableError
1717

1818
if typing.TYPE_CHECKING:
19-
from typing import Optional
20-
2119
from numpy.typing import NDArray
2220

2321
from .stagyydata import StagyyData
@@ -121,7 +119,7 @@ def delta_r(step: Step) -> Rprof:
121119
return Rprof((edges[1:] - edges[:-1]), step.rprofs.centers, meta)
122120

123121

124-
def _scale_prof(step: Step, rprof: NDArray, rad: Optional[NDArray] = None) -> NDArray:
122+
def _scale_prof(step: Step, rprof: NDArray, rad: NDArray | None = None) -> NDArray:
125123
"""Scale profile to take sphericity into account."""
126124
rbot, rtop = step.rprofs.bounds
127125
if rbot == 0: # not spherical

src/stagpy/refstate.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
from __future__ import annotations
44

5-
from typing import Optional
6-
75
import matplotlib.pyplot as plt
86

97
from . import _helpers
@@ -12,7 +10,7 @@
1210
from .stagyydata import StagyyData, _sdat_from_conf
1311

1412

15-
def plot_ref(sdat: StagyyData, var: str, conf: Optional[Config] = None) -> None:
13+
def plot_ref(sdat: StagyyData, var: str, conf: Config | None = None) -> None:
1614
"""Plot one reference state.
1715
1816
Args:

src/stagpy/rprof.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111
from .stagyydata import _sdat_from_conf
1212

1313
if typing.TYPE_CHECKING:
14-
from typing import Optional, Sequence
14+
from typing import Sequence
1515

1616
from .step import Rprofs, Step
1717

1818

1919
def plot_rprofs(
2020
rprofs: Rprofs,
2121
names: Sequence[Sequence[Sequence[str]]],
22-
conf: Optional[Config] = None,
22+
conf: Config | None = None,
2323
) -> None:
2424
"""Plot requested radial profiles.
2525
@@ -70,7 +70,7 @@ def plot_rprofs(
7070
_helpers.saveplot(conf, fig, fname + stepstr)
7171

7272

73-
def plot_grid(step: Step, conf: Optional[Config] = None) -> None:
73+
def plot_grid(step: Step, conf: Config | None = None) -> None:
7474
"""Plot cell position and thickness.
7575
7676
The figure is call grid_N.pdf where N is replace by the step index.

0 commit comments

Comments
 (0)