Repository navigation
Conversation
This is not ideal, since it bypasses the overloads if there's a typing issue in your wait_for call when using a str event. When using `Event.*`, the `check` and return types are still enforced as they should be.
8d27cad to
ee43582
Compare
|
@shiftinv would you please solve conflicts? |
b3b0cef to
cf276c7
Compare
done, rebased away the commits from the base PR and updated to master 👍 |
a171fab to
61f85fb
Compare
|
I hate the concept of putting a ton of overloads in the main |
| # n.b. this one isn't ideal, but there's no way to prevent type-checkers from using the fallback in this case | ||
| _ = assert_type(client.wait_for("guild_join", check=lambda: True), Coroutine[Any, Any, Any]) |
There was a problem hiding this comment.
Technically, an overload like
@overload
async def wait_for(
self,
event: Literal["guild_join", "ready", ...] | Event,
*,
check: Callable[..., Any],
timeout: Optional[float] = None,
) -> Never: ...Could work, but the Literal would be huge, and it's non-obvious what that signature means/why it got matched
There was a problem hiding this comment.
oh, interesting idea
And yeah, communicating this clearly to the user is tough. I could see InvalidCheckType = Never (or w/e) maybe being an option, which would look something like this, but it's not pretty:

fwiw, I think ultimately the approach in this PR is somewhat flawed, but it's the best we can do for now, sadly. In an ideal world (3.0?), every event would be a dataclass/namedtuple, and event names + overloads would be a thing of the past.
|
@shiftinv Hi shift, could you please resolve conflicts with master? |
…ait-for-overloads # Conflicts: # disnake/client.py # disnake/interactions/base.py # scripts/codemods/combined.py # test_bot/cogs/modals.py
this reduced the number of overloads from 123 down to 73
…e main package for now
…ait-for-overloads
Summary
Part 1/? of event QoL work, adds just a few overloads (73, to be exact) to
Client.wait_for, one for each event.This allows for correct typing of the
checkparameter and the return type, particularly when used with theEventenum (see below).These are being auto-generated through yet another custom libcst module. While certainly not ideal, there isn't a better way to do this with Python's current typing system due to several reasons.
That one big lookup table is a separate module as part of the library, since it'll be reused and expanded in future PRs with runtime usage.
Some other things:
Fallback for str
One of the overloads takes an unspecific
strevent. This is meant to be the fallback overload for anyone using the existing event infrastructure in the lib for custom events, since doing that would otherwise run into typing issues:disnake/disnake/client.py
Lines 2742 to 2751 in a52790d
This, however, means that incorrectly typing a library event handler also falls back to this, since it's the most generic one - i.e. passing a wrong
checktype for.wait_for("message")will not show any errors. This is not the case withEvent.message, which will correctly show a typing error if necessary.But what about the other event methods?
No :) Well, maybe.
I partially implemented this, but decided to remove it again; it may or may not affect the performance of pyright(/mypy), and the library typing is already complex enough.
Reason for choosing
wait_forin particular is that it's usually used together withcheck=lambda x, y, z: ..., and you can't add type annotations to inline lambdas.Somewhat depends on #1016 due to tests.
Checklist
pdm lintpdm pyright