Skip to content

Further improve typing on external interface functions for Broker and Worker - #744

Merged
LincolnPuzey merged 1 commit into
Bogdanp:masterfrom
jenstroeger:add-some-typing-broker
Aug 4, 2025
Merged

LincolnPuzey merged 1 commit into
Bogdanp:masterfrom
jenstroeger:add-some-typing-broker

Conversation

@jenstroeger

Copy link
Copy Markdown
Contributor

Doh, this should’ve been part of PR #731 🤦🏻‍♂️

@LincolnPuzey LincolnPuzey left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks,

Comment thread dramatiq/broker.py
"""
raise NotImplementedError

def enqueue(self, message, *, delay: Optional[int] = None): # pragma: no cover

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can the message parameter also be typed?

The Message class is generic for the Result type.
And the Message result type won't change when the message gets enqueued,
so strictly speaking I think this method also needs to be generic

def enqueue(self, message: Message[R], *, delay: Optional[int] = None) -> Message[R]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

And import the R from here

R = TypeVar("R", covariant=True)
or define it again?

However, grep’ing through the code there is no other use of Message[R] as a type annotation so, for consistency’s sake, I’m tempted to omit that for now, and revisit generic types in a separate PR?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

DO NOT import it.
Sorry for yelling, but importing it is an anti-pattern I have seen before.
It gives the illusion that the TypeVar is somehow 'shared' between the different places it is used, when in fact it is not.

side note, if you are supporting 3.12+ you can use the new syntax for generics which makes the question moot. https://docs.python.org/3/whatsnew/3.12.html#pep-695-type-parameter-syntax

Sure, that can be a different PR. I suggest turning on disallow_any_generics and seeing what errors there are.
Maybe still add message: Message in this PR.

@jenstroeger jenstroeger Aug 4, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

DO NOT import it. Sorry for yelling, […]

Sheeesus you woke me up from drowsy code doodling!

[…] but importing it is an anti-pattern I have seen before. It gives the illusion that the TypeVar is somehow 'shared' between the different places it is used, when in fact it is not.

Agreed, though I thought I’d ask anyway. With that in mind I always use _R or some such sunder-name to denote exactly that “private” purpose.

side note, if you are supporting 3.12+ you can use the new syntax for generics which makes the question moot. https://docs.python.org/3/whatsnew/3.12.html#pep-695-type-parameter-syntax

Well Dramatiq still supports 3.9+ so our hands are a bit tied.

Sure, that can be a different PR. I suggest turning on disallow_any_generics and seeing what errors there are. Maybe still add message: Message in this PR.

That introduced a lot of noise, let me open an issue in a bit to track that change so I opened PR #752.

I updated the PR with the Message type only.

@LincolnPuzey
LincolnPuzey merged commit 9037687 into Bogdanp:master Aug 4, 2025
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.

2 participants