99from typing import Sequence
1010
1111from .spy_events import SpyEvent , SpyRehearsal , VerifyRehearsal
12- from .stringify import stringify_call , stringify_error_message , count
12+ from .stringify import count , stringify_call , stringify_error_message
1313
1414
1515class DecoyWarning (UserWarning ):
@@ -31,18 +31,20 @@ class MiscalledStubWarning(DecoyWarning):
3131 [MiscalledStubWarning guide]: usage/errors-and-warnings.md#miscalledstubwarning
3232
3333 Attributes:
34- rehearsals: The mocks 's configured rehearsals.
34+ rehearsals: The mock 's configured rehearsals.
3535 calls: Actual calls to the mock.
3636 """
3737
3838 rehearsals : Sequence [SpyRehearsal ]
3939 calls : Sequence [SpyEvent ]
4040
41- def __init__ (
42- self ,
41+ @classmethod
42+ def create (
43+ cls ,
4344 rehearsals : Sequence [SpyRehearsal ],
4445 calls : Sequence [SpyEvent ],
45- ) -> None :
46+ ) -> "MiscalledStubWarning" :
47+ """Create a MiscalledStubWarning."""
4648 heading = os .linesep .join (
4749 [
4850 "Stub was called but no matching rehearsal found." ,
@@ -56,9 +58,11 @@ def __init__(
5658 calls = calls ,
5759 )
5860
59- super ().__init__ (message )
60- self .rehearsals = rehearsals
61- self .calls = calls
61+ result = cls (message )
62+ result .rehearsals = rehearsals
63+ result .calls = calls
64+
65+ return result
6266
6367
6468class RedundantVerifyWarning (DecoyWarning ):
@@ -74,7 +78,11 @@ class RedundantVerifyWarning(DecoyWarning):
7478 [RedundantVerifyWarning guide]: usage/errors-and-warnings.md#redundantverifywarning
7579 """
7680
77- def __init__ (self , rehearsal : VerifyRehearsal ) -> None :
81+ rehearsal : VerifyRehearsal
82+
83+ @classmethod
84+ def create (cls , rehearsal : VerifyRehearsal ) -> "RedundantVerifyWarning" :
85+ """Create a RedundantVerifyWarning."""
7886 message = os .linesep .join (
7987 [
8088 "The same rehearsal was used in both a `when` and a `verify`." ,
@@ -83,8 +91,11 @@ def __init__(self, rehearsal: VerifyRehearsal) -> None:
8391 "See https://michael.cousins.io/decoy/usage/errors-and-warnings/#redundantverifywarning" ,
8492 ]
8593 )
86- super ().__init__ (message )
87- self .rehearsal = rehearsal
94+
95+ result = cls (message )
96+ result .rehearsal = rehearsal
97+
98+ return result
8899
89100
90101class IncorrectCallWarning (DecoyWarning ):
0 commit comments