From e04bad82023ce2310eceaaf0f5dba0a1f711eb14 Mon Sep 17 00:00:00 2001 From: Andrew Spittlemeister Date: Mon, 29 Jul 2024 15:36:11 -0500 Subject: [PATCH] remove broad exception raises --- bytechomp/data_descriptor.py | 35 +++++++++++++++-------------------- bytechomp/serialization.py | 14 ++++++-------- 2 files changed, 21 insertions(+), 28 deletions(-) diff --git a/bytechomp/data_descriptor.py b/bytechomp/data_descriptor.py index a8467a3..b92b3b1 100644 --- a/bytechomp/data_descriptor.py +++ b/bytechomp/data_descriptor.py @@ -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 @@ -50,13 +48,13 @@ 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})" ) @@ -64,7 +62,7 @@ def build_data_description(datatype: type) -> TypeTree: 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: @@ -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})" ) @@ -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 @@ -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 @@ -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. """ @@ -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( @@ -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}") @@ -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) diff --git a/bytechomp/serialization.py b/bytechomp/serialization.py index 0edb4e2..8e5f783 100644 --- a/bytechomp/serialization.py +++ b/bytechomp/serialization.py @@ -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 @@ -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})" ) @@ -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})" ) @@ -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