File tree 2 files changed +21
-8
lines changed
2 files changed +21
-8
lines changed Original file line number Diff line number Diff line change 1
1
from dataclasses import dataclass
2
2
3
- from denokv ._pycompat .typing import cast
3
+ from denokv ._pycompat .typing import TYPE_CHECKING
4
4
5
5
6
6
@dataclass (init = False )
7
7
class DenoKvError (Exception ):
8
- message : str
8
+ # Define message for dataclass field metadata only, not type annotation.
9
+ if not TYPE_CHECKING :
10
+ message : str
9
11
10
- def __init__ (self , message : str , * args : object ) -> None :
11
- super ().__init__ (message , * args )
12
- if not isinstance (message , str ):
13
- raise TypeError (f"first argument must be a str message: { message !r} " )
12
+ def __init__ (self , * args : object ) -> None :
13
+ super (DenoKvError , self ).__init__ (* args )
14
14
15
- @property # type: ignore[no-redef]
15
+ @property
16
16
def message (self ) -> str :
17
- return cast (str , self .args [0 ])
17
+ if args := self .args :
18
+ return str (args [0 ])
19
+ return type (self ).__name__
18
20
19
21
20
22
class DenoKvValidationError (ValueError , DenoKvError ):
Original file line number Diff line number Diff line change @@ -7,3 +7,14 @@ def test_errors_are_regular_exceptions() -> None:
7
7
"""Errors must be caught by generic Exception handlers — not BaseException."""
8
8
with pytest .raises (Exception ): # noqa: B017
9
9
raise DenoKvError ("error" )
10
+
11
+
12
+ def test_DenoKvError_message () -> None :
13
+ assert DenoKvError ().message == "DenoKvError"
14
+ assert DenoKvError ("Foo bar" ).message == "Foo bar"
15
+
16
+ class CustomError (DenoKvError ):
17
+ pass
18
+
19
+ assert CustomError ().message == "CustomError"
20
+ assert CustomError ("Bar baz" ).message == "Bar baz"
You can’t perform that action at this time.
0 commit comments