Skip to content

fix: accept message string in error and warning constructors #272

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions decoy/warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"""

import os
from typing import Sequence
from typing import Sequence, Union

from .spy_events import SpyEvent, SpyRehearsal, VerifyRehearsal
from .stringify import stringify_call, stringify_error_message, count
Expand Down Expand Up @@ -74,14 +74,18 @@ class RedundantVerifyWarning(DecoyWarning):
[RedundantVerifyWarning guide]: usage/errors-and-warnings.md#redundantverifywarning
"""

def __init__(self, rehearsal: VerifyRehearsal) -> None:
message = os.linesep.join(
[
"The same rehearsal was used in both a `when` and a `verify`.",
"This is redundant and probably a misuse of the mock.",
f"\t{stringify_call(rehearsal)}",
"See https://michael.cousins.io/decoy/usage/errors-and-warnings/#redundantverifywarning",
]
def __init__(self, rehearsal: Union[VerifyRehearsal, str]) -> None:
Copy link
Owner

@mcous mcous Jul 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: rather than overload the constructor, let's make a larger change:

  • Add a create classmethod with the same signature as the old constructor, taking the rehearsal
  • Remove __init__, allowing it to use the default warning constructor

That way, our creation and (de)serialization both use the same string-based constructor, while message generation logic that relies on internal interfaces is split out to our own bespoke method

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mcous The constructors are currently being used to set properties on the warning classes that appear to be used for testing, should I omit that? If I don't, the constructor can't take the standard parameters

Copy link
Owner

@mcous mcous Jul 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right, good catch. The fact that they're relied upon in tests is an oversight and something I'm not particularly worried about maintaining. Use your best judgement to update tests if needed and I can fix anything up prior to merge.

The thing I am concerned about: removing those properties would technically be a breaking change. I am removing them in the next major version, so does something a little silly like this work to keep them for now?

class SomeWarning():
    @classmethod
    def create(cls, calls: ...):
        message = ...
        result = cls(message)
        result.calls = calls
        ...

        return result

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh also let me know if this is more effort than you want to / are available to take on! I really appreciate the work you've already done to observe and identify the issue

message = (
os.linesep.join(
[
"The same rehearsal was used in both a `when` and a `verify`.",
"This is redundant and probably a misuse of the mock.",
f"\t{stringify_call(rehearsal)}",
"See https://michael.cousins.io/decoy/usage/errors-and-warnings/#redundantverifywarning",
]
)
if isinstance(rehearsal, VerifyRehearsal)
else rehearsal
)
super().__init__(message)
self.rehearsal = rehearsal
Expand Down
Loading