Repository navigation
Further improve typing on external interface functions for Broker and Worker - #744
Conversation
| """ | ||
| raise NotImplementedError | ||
|
|
||
| def enqueue(self, message, *, delay: Optional[int] = None): # pragma: no cover |
There was a problem hiding this comment.
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]There was a problem hiding this comment.
And import the R from here
Line 34 in 2bbf61b
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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: Messagein 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.
8923eb6 to
37dd13b
Compare
Doh, this should’ve been part of PR #731 🤦🏻♂️