Skip to content

Commit bbb9c6e

Browse files
committed
Add annotation in error module
1 parent 7e66512 commit bbb9c6e

File tree

1 file changed

+38
-71
lines changed

1 file changed

+38
-71
lines changed

stagpy/error.py

Lines changed: 38 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
"""Exceptions raised by StagPy."""
22

3+
from __future__ import annotations
4+
import typing
5+
6+
if typing.TYPE_CHECKING:
7+
from typing import Sequence
8+
from os import PathLike
9+
from .stagyydata import StagyyData
10+
311

412
class StagpyError(Exception):
513
"""Base class for exceptions raised by StagPy.
@@ -16,32 +24,24 @@ class StagpyError(Exception):
1624
class NoSnapshotError(StagpyError):
1725
"""Raised when no snapshot can be found.
1826
19-
Args:
20-
sdat (:class:`~stagpy.stagyydata.StagyyData`): the StagyyData
21-
instance for which no snapshot were found.
22-
2327
Attributes:
24-
sdat (:class:`~stagpy.stagyydata.StagyyData`): the StagyyData
25-
instance for which no snapshot were found.
28+
sdat: the :class:`~stagpy.stagyydata.StagyyData` instance for which no
29+
snapshot was found.
2630
"""
2731

28-
def __init__(self, sdat):
32+
def __init__(self, sdat: StagyyData):
2933
self.sdat = sdat
3034
super().__init__(f'no snapshot found for {sdat}')
3135

3236

3337
class NoParFileError(StagpyError):
3438
"""Raised when no par file can be found.
3539
36-
Args:
37-
parfile (pathlike): the expected path of
38-
the par file.
39-
4040
Attributes:
41-
parfile (pathlike): the expected path of the par file.
41+
parfile: the expected path of the par file.
4242
"""
4343

44-
def __init__(self, parfile):
44+
def __init__(self, parfile: PathLike):
4545
self.parfile = parfile
4646
super().__init__(f'{parfile} file not found')
4747

@@ -55,40 +55,28 @@ class NotAvailableError(StagpyError):
5555
class ParsingError(StagpyError):
5656
"""Raised when a parsing error occurs.
5757
58-
Args:
59-
faulty_file (pathlike): path of the file where a parsing problem
60-
was encountered.
61-
msg (str): error message.
62-
6358
Attributes:
64-
file (pathlike): path of the file where a parsing problem was
65-
encountered.
66-
msg (str): error message.
59+
file: path of the file where a parsing problem was encountered.
60+
msg: error message.
6761
"""
6862

69-
def __init__(self, faulty_file, msg):
70-
self.file = faulty_file
63+
def __init__(self, file: PathLike, msg: str):
64+
self.file = file
7165
self.msg = msg
72-
super().__init__(faulty_file, msg)
66+
super().__init__(file, msg)
7367

7468

7569
class InvalidTimestepError(StagpyError, KeyError):
7670
"""Raised when invalid time step is requested.
7771
78-
Args:
79-
sdat (:class:`~stagpy.stagyydata.StagyyData`): the StagyyData
80-
instance for which the request was made.
81-
istep (int): the invalid time step index.
82-
msg (str): the error message.
83-
8472
Attributes:
85-
sdat (:class:`~stagpy.stagyydata.StagyyData`): the StagyyData
86-
instance for which the request was made.
87-
istep (int): the invalid time step index.
88-
msg (str): the error message.
73+
sdat: the :class:`~stagpy.stagyydata.StagyyData` instance to which the
74+
request was made.
75+
istep: the invalid time step index.
76+
msg: the error message.
8977
"""
9078

91-
def __init__(self, sdat, istep, msg):
79+
def __init__(self, sdat: StagyyData, istep: int, msg: str):
9280
self.sdat = sdat
9381
self.istep = istep
9482
self.msg = msg
@@ -98,20 +86,14 @@ def __init__(self, sdat, istep, msg):
9886
class InvalidSnapshotError(StagpyError, KeyError):
9987
"""Raised when invalid snapshot is requested.
10088
101-
Args:
102-
sdat (:class:`~stagpy.stagyydata.StagyyData`): the StagyyData
103-
instance for which the request was made.
104-
isnap (int): the invalid snapshot index.
105-
msg (str): the error message.
106-
10789
Attributes:
108-
sdat (:class:`~stagpy.stagyydata.StagyyData`): the StagyyData
109-
instance for which the request was made.
110-
isnap (int): the invalid snapshot index.
111-
msg (str): the error message.
90+
sdat: the :class:`~stagpy.stagyydata.StagyyData` instance to which the
91+
request was made.
92+
isnap: the invalid snapshot index.
93+
msg: the error message.
11294
"""
11395

114-
def __init__(self, sdat, isnap, msg):
96+
def __init__(self, sdat: StagyyData, isnap: int, msg: str):
11597
self.sdat = sdat
11698
self.isnap = isnap
11799
self.msg = msg
@@ -121,44 +103,35 @@ def __init__(self, sdat, isnap, msg):
121103
class InvalidTimeFractionError(StagpyError):
122104
"""Raised when invalid fraction of series is requested.
123105
124-
Args:
125-
fraction (float): the invalid fraction.
126-
127106
Attributes:
128-
fraction (float): the invalid fraction.
107+
fraction: the invalid fraction.
129108
"""
130109

131-
def __init__(self, fraction):
110+
def __init__(self, fraction: float):
132111
self.fraction = fraction
133112
super().__init__(f'Fraction should be in (0,1] (received {fraction})')
134113

135114

136115
class InvalidNfieldsError(StagpyError):
137116
"""Raised when invalid nfields_max is requested.
138117
139-
Args:
140-
nfields (int): the invalid number of fields.
141-
142118
Attributes:
143-
nfields (int): the invalid number of field.
119+
nfields: the invalid number of field.
144120
"""
145121

146-
def __init__(self, nfields):
122+
def __init__(self, nfields: int):
147123
self.nfields = nfields
148124
super().__init__(f'nfields_max should be >5 (received {nfields})')
149125

150126

151127
class InvalidZoomError(StagpyError):
152128
"""Raised when invalid zoom is requested.
153129
154-
Args:
155-
zoom (int): the invalid zoom level.
156-
157130
Attributes:
158-
zoom (int): the invalid zoom level.
131+
zoom: the invalid zoom level.
159132
"""
160133

161-
def __init__(self, zoom):
134+
def __init__(self, zoom: int):
162135
self.zoom = zoom
163136
super().__init__(f'Zoom angle should be in [0,360] (received {zoom})')
164137

@@ -172,29 +145,23 @@ class MissingDataError(StagpyError, KeyError):
172145
class UnknownVarError(StagpyError, KeyError):
173146
"""Raised when invalid var is requested.
174147
175-
Args:
176-
varname (str): the invalid var name.
177-
178148
Attributes:
179-
varname (str): the invalid var name.
149+
varname: the invalid var name.
180150
"""
181151

182-
def __init__(self, varname):
152+
def __init__(self, varname: str):
183153
self.varname = varname
184154
super().__init__(varname)
185155

186156

187157
class UnknownFiltersError(StagpyError):
188158
"""Raised when invalid step filter is requested.
189159
190-
Args:
191-
filters (list): the invalid filter names.
192-
193160
Attributes:
194-
filters (list): the invalid filter names.
161+
filters: the invalid filter names.
195162
"""
196163

197-
def __init__(self, filters):
164+
def __init__(self, filters: Sequence[str]):
198165
self.filters = filters
199166
super().__init__(', '.join(repr(f) for f in filters))
200167

0 commit comments

Comments
 (0)