Skip to content

fix: make Notifier accept configured service instances - #322

Open
pcbeingused333 wants to merge 1 commit into
pyfenn:mainfrom
pcbeingused333:fix/notifier-accepts-instances
Open

fix: make Notifier accept configured service instances#322
pcbeingused333 wants to merge 1 commit into
pyfenn:mainfrom
pcbeingused333:fix/notifier-accepts-instances

Conversation

@pcbeingused333

Copy link
Copy Markdown
Contributor

Problem

Notifier.add_service / add_services / remove_service are annotated as taking type[Service] and call service():

def add_service(self, service: type[Service]) -> None:
    self._services.append(service())

Every built-in service requires constructor arguments (Discord(webhook_url), Resend(api_key, from_email, to_emails_raw), ...), so:

  • notifier.add_services([Discord, Telegram]) — the form in the docstring — raises TypeError: __init__() missing 1 required positional argument.
  • notifier.add_service(Discord(url)) raises TypeError: 'Discord' object is not callable.

remove_service is worse: it builds a new instance and removes it by ==, so it never matches an added service (no __eq__ on the ABC) and always raises "not found".

Notifier has no tests and no internal callers, so nothing pinned the broken behavior.

Fix

add_service / add_services / remove_service take configured instances. add_service raises a clear TypeError if handed a class by mistake; remove_service removes the instance directly.

Tests

New tests/unit/notification/test_notifier.py — 8 cases: add/notify, add_services with instances, rejecting a class, empty-notify no-op, a failing service not blocking the others, remove, remove-missing, clear. 7 of the 8 fail on main. uv run pytest tests/unit/notification is green (24 passed); ruff check / ruff format clean.

`Notifier.add_service` / `add_services` / `remove_service` were annotated
to take a `type[Service]` and then called `service()`. Every built-in
service (`Discord`, `Slack`, `Telegram`, `Resend`) requires constructor
arguments, so `notifier.add_services([Discord, Telegram])` -- the form in
the docstring -- raised `TypeError`, and passing an instance raised
`'Discord' object is not callable`. `remove_service` was doubly broken:
it instantiated a fresh object and looked for it by `==`, which never
matches without a custom `__eq__`.

They now take instances. `add_service` raises a clear `TypeError` if given
a class by mistake, and `remove_service` removes the instance directly.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GtYgwYNfJ3wHrw9xrooVoB
@pcbeingused333

Copy link
Copy Markdown
Contributor Author

The pre-commit check is red on the ty hook, but that's pre-existing on main (commit 0d1862e fails the same job, on fenn/experimental/vision/normalize.py and fenn/logging.py — neither touched here). ruff check / ruff format pass; the four test jobs are the relevant ones.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant