Skip to content

exceptions

Classes:

CatchHandlerStmt

CatchHandlerStmt(
    body: Block[AST],
    name: Optional[Identifier] = None,
    types: Optional[
        Iterable[Identifier] | ASTNodes[Identifier]
    ] = None,
    parent: Optional[ASTNodes] = None,
    loc: SourceLocation = NO_SOURCE_LOCATION,
)

Bases: StatementType

Methods:

Source code in packages/astx/src/astx/exceptions.py
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
def __init__(
    self,
    body: Block[AST],
    name: Optional[Identifier] = None,
    types: Optional[Iterable[Identifier] | ASTNodes[Identifier]] = None,
    parent: Optional[ASTNodes] = None,
    loc: SourceLocation = NO_SOURCE_LOCATION,
) -> None:
    """
    title: Initialize the instance.
    parameters:
      body:
        type: Block[AST]
      name:
        type: Optional[Identifier]
      types:
        type: Optional[Iterable[Identifier] | ASTNodes[Identifier]]
      parent:
        type: Optional[ASTNodes]
      loc:
        type: SourceLocation
    """
    super().__init__(loc=loc, parent=parent)
    self.body = body
    self.name = name

    if types:
        if isinstance(types, ASTNodes):
            self.types = types
        else:
            self.types = ASTNodes[Identifier]()
            for t in types:
                self.types.append(t)
    else:
        self.types = None

    self.kind = ASTKind.CatchHandlerStmtKind

get_struct

get_struct(simplified: bool = False) -> ReprStruct
Source code in packages/astx/src/astx/exceptions.py
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """
    title: Return the AST structure of the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: ReprStruct
    """
    key = (
        f"CATCH-HANDLER-STMT[{id(self)}]"
        if simplified
        else "CATCH-HANDLER-STMT"
    )
    body_dict = {"body": self.body.get_struct(simplified)}
    name_dict = (
        {"name": self.name.get_struct(simplified)} if self.name else {}
    )
    types_dict = (
        {"types": self.types.get_struct(simplified)} if self.types else {}
    )

    value: DictDataTypesStruct = {
        **cast(DictDataTypesStruct, body_dict),
        **cast(DictDataTypesStruct, name_dict),
        **cast(DictDataTypesStruct, types_dict),
    }
    return self._prepare_struct(key, value, simplified)

to_json

to_json(simplified: bool = False) -> str
Source code in packages/astx/src/astx/base.py
400
401
402
403
404
405
406
407
408
409
def to_json(self, simplified: bool = False) -> str:
    """
    title: Return an json string that represents the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: str
    """
    return json.dumps(self.get_struct(simplified=simplified), indent=2)

to_yaml

to_yaml(simplified: bool = False) -> str
Source code in packages/astx/src/astx/base.py
387
388
389
390
391
392
393
394
395
396
397
398
def to_yaml(self, simplified: bool = False) -> str:
    """
    title: Return an yaml string that represents the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: str
    """
    return str(
        yaml.dump(self.get_struct(simplified=simplified), sort_keys=False)
    )

ExceptionHandlerStmt

ExceptionHandlerStmt(
    body: Block[AST],
    handlers: Iterable[CatchHandlerStmt]
    | ASTNodes[CatchHandlerStmt],
    finally_handler: Optional[FinallyHandlerStmt] = None,
    parent: Optional[ASTNodes] = None,
    loc: SourceLocation = NO_SOURCE_LOCATION,
)

Bases: StatementType

Methods:

Source code in packages/astx/src/astx/exceptions.py
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
def __init__(
    self,
    body: Block[AST],
    handlers: Iterable[CatchHandlerStmt] | ASTNodes[CatchHandlerStmt],
    finally_handler: Optional[FinallyHandlerStmt] = None,
    parent: Optional[ASTNodes] = None,
    loc: SourceLocation = NO_SOURCE_LOCATION,
) -> None:
    """
    title: Initialize the instance.
    parameters:
      body:
        type: Block[AST]
      handlers:
        type: Iterable[CatchHandlerStmt] | ASTNodes[CatchHandlerStmt]
      finally_handler:
        type: Optional[FinallyHandlerStmt]
      parent:
        type: Optional[ASTNodes]
      loc:
        type: SourceLocation
    """
    super().__init__(loc=loc, parent=parent)
    self.body = body

    if isinstance(handlers, ASTNodes):
        self.handlers = handlers
    else:
        self.handlers = ASTNodes[CatchHandlerStmt]()
        for h in handlers:
            self.handlers.append(h)

    self.finally_handler = finally_handler

    self.kind = ASTKind.ExceptionHandlerStmtKind

get_struct

get_struct(simplified: bool = False) -> ReprStruct
Source code in packages/astx/src/astx/exceptions.py
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """
    title: Return the AST structure of the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: ReprStruct
    """
    key = (
        f"EXCEPTION-HANDLER-STMT[{id(self)}]"
        if simplified
        else "EXCEPTION-HANDLER-STMT"
    )

    body_dict = {"body": self.body.get_struct(simplified)}
    handlers_dict = {"handlers": self.handlers.get_struct(simplified)}
    finally_dict = (
        {"finally_handler": self.finally_handler.get_struct(simplified)}
        if self.finally_handler
        else {}
    )

    value: DictDataTypesStruct = {
        **cast(DictDataTypesStruct, body_dict),
        **cast(DictDataTypesStruct, handlers_dict),
        **cast(DictDataTypesStruct, finally_dict),
    }
    return self._prepare_struct(key, value, simplified)

to_json

to_json(simplified: bool = False) -> str
Source code in packages/astx/src/astx/base.py
400
401
402
403
404
405
406
407
408
409
def to_json(self, simplified: bool = False) -> str:
    """
    title: Return an json string that represents the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: str
    """
    return json.dumps(self.get_struct(simplified=simplified), indent=2)

to_yaml

to_yaml(simplified: bool = False) -> str
Source code in packages/astx/src/astx/base.py
387
388
389
390
391
392
393
394
395
396
397
398
def to_yaml(self, simplified: bool = False) -> str:
    """
    title: Return an yaml string that represents the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: str
    """
    return str(
        yaml.dump(self.get_struct(simplified=simplified), sort_keys=False)
    )

FinallyHandlerStmt

FinallyHandlerStmt(
    body: Block[AST],
    parent: Optional[ASTNodes] = None,
    loc: SourceLocation = NO_SOURCE_LOCATION,
)

Bases: StatementType

Methods:

Source code in packages/astx/src/astx/exceptions.py
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
def __init__(
    self,
    body: Block[AST],
    parent: Optional[ASTNodes] = None,
    loc: SourceLocation = NO_SOURCE_LOCATION,
) -> None:
    """
    title: Initialize the instance.
    parameters:
      body:
        type: Block[AST]
      parent:
        type: Optional[ASTNodes]
      loc:
        type: SourceLocation
    """
    super().__init__(loc=loc, parent=parent)
    self.body = body
    self.kind = ASTKind.FinallyHandlerStmtKind

get_struct

get_struct(simplified: bool = False) -> ReprStruct
Source code in packages/astx/src/astx/exceptions.py
327
328
329
330
331
332
333
334
335
336
337
338
339
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """
    title: Return the AST structure of the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: ReprStruct
    """
    key = f"FINALLY-STMT[{id(self)}]" if simplified else "FINALLY-STMT"
    value: DictDataTypesStruct = {"body": self.body.get_struct(simplified)}

    return self._prepare_struct(key, value, simplified)

to_json

to_json(simplified: bool = False) -> str
Source code in packages/astx/src/astx/base.py
400
401
402
403
404
405
406
407
408
409
def to_json(self, simplified: bool = False) -> str:
    """
    title: Return an json string that represents the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: str
    """
    return json.dumps(self.get_struct(simplified=simplified), indent=2)

to_yaml

to_yaml(simplified: bool = False) -> str
Source code in packages/astx/src/astx/base.py
387
388
389
390
391
392
393
394
395
396
397
398
def to_yaml(self, simplified: bool = False) -> str:
    """
    title: Return an yaml string that represents the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: str
    """
    return str(
        yaml.dump(self.get_struct(simplified=simplified), sort_keys=False)
    )

ThrowStmt

ThrowStmt(
    exception: Optional[Expr] = None,
    parent: Optional[ASTNodes] = None,
    loc: SourceLocation = NO_SOURCE_LOCATION,
)

Bases: StatementType

Methods:

Source code in packages/astx/src/astx/exceptions.py
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
def __init__(
    self,
    exception: Optional[Expr] = None,
    parent: Optional[ASTNodes] = None,
    loc: SourceLocation = NO_SOURCE_LOCATION,
) -> None:
    """
    title: Initialize the instance.
    parameters:
      exception:
        type: Optional[Expr]
      parent:
        type: Optional[ASTNodes]
      loc:
        type: SourceLocation
    """
    super().__init__(loc=loc, parent=parent)
    self.exception = exception
    self.kind = ASTKind.ThrowStmtKind

get_struct

get_struct(simplified: bool = False) -> ReprStruct
Source code in packages/astx/src/astx/exceptions.py
74
75
76
77
78
79
80
81
82
83
84
85
def get_struct(self, simplified: bool = False) -> ReprStruct:
    """
    title: Return the AST structure of the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: ReprStruct
    """
    key = f"THROW-STMT[{id(self)}]" if simplified else "THROW-STMT"
    value = self.exception.get_struct(simplified) if self.exception else ""
    return self._prepare_struct(key, value, simplified)

to_json

to_json(simplified: bool = False) -> str
Source code in packages/astx/src/astx/base.py
400
401
402
403
404
405
406
407
408
409
def to_json(self, simplified: bool = False) -> str:
    """
    title: Return an json string that represents the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: str
    """
    return json.dumps(self.get_struct(simplified=simplified), indent=2)

to_yaml

to_yaml(simplified: bool = False) -> str
Source code in packages/astx/src/astx/base.py
387
388
389
390
391
392
393
394
395
396
397
398
def to_yaml(self, simplified: bool = False) -> str:
    """
    title: Return an yaml string that represents the object.
    parameters:
      simplified:
        type: bool
    returns:
      type: str
    """
    return str(
        yaml.dump(self.get_struct(simplified=simplified), sort_keys=False)
    )