Skip to content

Commit e4fb375

Browse files
committed
Avoid raising plain exception
1 parent d44c753 commit e4fb375

File tree

4 files changed

+12
-7
lines changed

4 files changed

+12
-7
lines changed

src/cattrs/converters.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
)
8080
from .enums import enum_structure_factory, enum_unstructure_factory
8181
from .errors import (
82+
CattrsError,
8283
IterableValidationError,
8384
IterableValidationNote,
8485
StructureHandlerNotFoundError,
@@ -708,7 +709,7 @@ def _structure_call(obj: Any, cl: type[T]) -> Any:
708709
@staticmethod
709710
def _structure_simple_literal(val, type):
710711
if val not in type.__args__:
711-
raise Exception(f"{val} not in literal {type}")
712+
raise CattrsError(f"{val} not in literal {type}")
712713
return val
713714

714715
@staticmethod
@@ -717,7 +718,7 @@ def _structure_enum_literal(val, type):
717718
try:
718719
return vals[val]
719720
except KeyError:
720-
raise Exception(f"{val} not in literal {type}") from None
721+
raise CattrsError(f"{val} not in literal {type}") from None
721722

722723
def _structure_newtype(self, val: UnstructuredValue, type) -> StructuredValue:
723724
base = get_newtype_base(type)

src/cattrs/preconf/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55

66
from .._compat import is_subclass
77
from ..converters import Converter, UnstructureHook
8+
from ..errors import CattrsError
89
from ..fns import identity
910

1011

1112
def validate_datetime(v, _):
1213
if not isinstance(v, datetime):
13-
raise Exception(f"Expected datetime, got {v}")
14+
raise CattrsError(f"Expected datetime, got {v}")
1415
return v
1516

1617

tests/preconf/test_pyyaml.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from yaml import safe_dump, safe_load
99

1010
from cattrs._compat import FrozenSetSubscriptable
11-
from cattrs.errors import ClassValidationError
11+
from cattrs.errors import CattrsError, ClassValidationError
1212
from cattrs.preconf.pyyaml import make_converter
1313

1414
from ..test_preconf import Everything, everythings, native_unions
@@ -70,13 +70,15 @@ class A:
7070
date: 1
7171
"""
7272

73-
with raises(ClassValidationError if detailed_validation else Exception) as exc_info:
73+
with raises(
74+
ClassValidationError if detailed_validation else CattrsError
75+
) as exc_info:
7476
converter.loads(bad_data, A)
7577

7678
if detailed_validation:
7779
assert (
7880
repr(exc_info.value.exceptions[0])
79-
== "Exception('Expected datetime, got 1')"
81+
== "CattrsError('Expected datetime, got 1')"
8082
)
8183
assert (
8284
repr(exc_info.value.exceptions[1]) == "ValueError('Expected date, got 1')"

tests/test_enums.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from cattrs import BaseConverter
1010
from cattrs._compat import Literal
11+
from cattrs.errors import CattrsError
1112

1213
from .untyped import enums_of_primitives
1314

@@ -27,7 +28,7 @@ def test_enum_failure(enum):
2728
converter = BaseConverter()
2829
type = Literal[next(iter(enum))]
2930

30-
with raises(Exception) as exc_info:
31+
with raises(CattrsError) as exc_info:
3132
converter.structure("", type)
3233

3334
assert exc_info.value.args[0] == f" not in literal {type!r}"

0 commit comments

Comments
 (0)