Skip to content

Commit

Permalink
remove broad exception raises
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewSpittlemeister committed Jul 29, 2024
1 parent 17a189a commit e04bad8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 28 deletions.
35 changes: 15 additions & 20 deletions bytechomp/data_descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
bytechomp.data_descriptor
"""

# pylint: disable=broad-exception-raised

from __future__ import annotations
from typing import Annotated, Union, Any, get_origin, get_args
from dataclasses import is_dataclass, fields, MISSING
Expand Down Expand Up @@ -50,21 +48,21 @@ def build_data_description(datatype: type) -> TypeTree:
)
elif inspect.isclass(field.type) and is_dataclass(field.type):
if field.default != MISSING:
raise Exception(f"cannot have default value on nested types (field: {field.name})")
raise TypeError(f"cannot have default value on nested types (field: {field.name})")
object_description[field.name] = build_data_description(field.type)
elif get_origin(field.type) == Annotated:
args = get_args(field.type)

if len(args) != 2:
raise Exception(
raise TypeError(
f"annotated value should only have two arguments (field: {field.name})"
)

arg_type = args[0]
length = args[1]

if not isinstance(length, int):
raise Exception("second annotated argument must be an integer to denote length")
raise TypeError("second annotated argument must be an integer to denote length")

# # deal with string type
# if arg_type == str:
Expand Down Expand Up @@ -92,7 +90,7 @@ def build_data_description(datatype: type) -> TypeTree:
list_type_args = get_args(arg_type)

if len(list_type_args) != 1:
raise Exception(
raise TypeError(
f"list must contain only one kind of data type (field: {field.name})"
)

Expand All @@ -110,16 +108,16 @@ def build_data_description(datatype: type) -> TypeTree:
elif inspect.isclass(list_type) and is_dataclass(list_type):
object_description[field.name] = [build_data_description(list_type)] * length
else:
raise Exception(f"unsupported list type: {list_type} (field: {field.name})")
raise TypeError(f"unsupported list type: {list_type} (field: {field.name})")

else:
raise Exception(f"unsupported annotated type: {arg_type} (field: {field.name})")
raise TypeError(f"unsupported annotated type: {arg_type} (field: {field.name})")
elif field.type in [list, bytes]:
raise Exception(
raise TypeError(
f"annotation needed for list/bytes (length required, field: {field.name})"
)
else:
raise Exception(f"unsupported data type ({field.type}) on field {field.name}")
raise TypeError(f"unsupported data type ({field.type}) on field {field.name}")

return object_description

Expand Down Expand Up @@ -149,11 +147,11 @@ def build_data_pattern(description: TypeTree) -> str:
elif isinstance(sub_element, OrderedDict):
pattern += build_data_pattern(sub_element)
else:
raise Exception(f"invalid list type found ({name})")
raise TypeError(f"invalid list type found ({name})")
elif isinstance(root_element, OrderedDict):
pattern += build_data_pattern(root_element)
else:
raise Exception(f"invalid element type found ({name}: {type(root_element)})")
raise TypeError(f"invalid element type found ({name}: {type(root_element)})")
return pattern


Expand All @@ -162,9 +160,6 @@ def resolve_basic_type(
) -> int | float | bytes:
"""Returns the value of the element while checking the intended type in the node.
Raises:
Exception: [description]
Returns:
int | float | bytes: Pythonic parsed value.
"""
Expand All @@ -173,7 +168,7 @@ def resolve_basic_type(
return arg
# if isinstance(arg, bytes) and element.python_type is str:
# return arg.decode("utf-8")
raise Exception(f"invalid match between types: {type(arg)} != {element.python_type}")
raise TypeError(f"invalid match between types: {type(arg)} != {element.python_type}")


def build_structure(
Expand All @@ -193,9 +188,9 @@ def build_structure(
# print(f"dat_args: {args}")
cls_type = description.get("__struct_type__")
if cls_type is not None and not isinstance(cls_type, type):
raise Exception("lost struct type information in description")
raise TypeError("lost struct type information in description")
if cls_type is None:
raise Exception("unable to find type information in description")
raise LookupError("unable to find type information in description")
cls_args: dict[str, Any] = {}
# print(f"constructing type {cls_type}")

Expand All @@ -213,12 +208,12 @@ def build_structure(
elif isinstance(sub_element, OrderedDict):
list_element.append(build_structure(args, sub_element))
else:
raise Exception(f"invalid list type found ({name})")
raise TypeError(f"invalid list type found ({name})")
cls_args[name] = list_element
elif isinstance(root_element, OrderedDict):
cls_args[name] = build_structure(args, root_element)
else:
raise Exception(f"invalid element type found ({name}: {type(root_element)})")
raise TypeError(f"invalid element type found ({name}: {type(root_element)})")

# print(f"cls_args: {cls_args}")
return cls_type(**cls_args)
14 changes: 6 additions & 8 deletions bytechomp/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
bytechomp.serialization
"""

# pylint: disable=broad-exception-raised

import struct
from typing import Annotated, get_origin, get_args, cast
from dataclasses import is_dataclass, fields
Expand Down Expand Up @@ -62,7 +60,7 @@ def flatten_dataclass(data_object: type) -> tuple[str, list[int | float | bytes]
args = get_args(field.type)

if len(args) != 2:
raise Exception(
raise TypeError(
f"annotated value should only have two arguments (field: {field.name})"
)

Expand Down Expand Up @@ -114,7 +112,7 @@ def flatten_dataclass(data_object: type) -> tuple[str, list[int | float | bytes]
list_type_args = get_args(arg_type)

if len(list_type_args) != 1:
raise Exception(
raise TypeError(
f"list must contain only one kind of data type (field: {field.name})"
)

Expand Down Expand Up @@ -142,16 +140,16 @@ def flatten_dataclass(data_object: type) -> tuple[str, list[int | float | bytes]
pattern += nested_pattern
values.extend(nested_values)
else:
raise Exception(f"unsupported list type: {list_type} (field: {field.name})")
raise TypeError(f"unsupported list type: {list_type} (field: {field.name})")

else:
raise Exception(f"unsupported annotated type: {arg_type} (field: {field.name})")
raise TypeError(f"unsupported annotated type: {arg_type} (field: {field.name})")
elif field.type in [list, bytes]:
raise Exception(
raise TypeError(
f"annotation needed for list/bytes (length required, field: {field.name})"
)
else:
raise Exception(f"unsupported data type ({field.type}) on field {field.name}")
raise TypeError(f"unsupported data type ({field.type}) on field {field.name}")

return pattern, values

Expand Down

0 comments on commit e04bad8

Please sign in to comment.