disnake
disnake copied to clipboard
fix(typing): improve view type inference of ui decorators
trafficstars
Summary
This adjusts some internal typings to get rid of Unknowns in button/select decorators.
view_cls.methodnow has a properly typed(Self@V, Item[Self@V], MsgInter)signature, instead of(Any, Unknown, MsgInter)- more importantly,
self.methodnow returns anItem[Self@V]with the correctly bound view type, instead ofItem[V_co], which was unbound and didn't allow accessing custom attributes onself.method.view
class CustomView(disnake.ui.View):
x: int
@disnake.ui.string_select(options=["A", "B"])
async def sel(self, s, i):
# before: StringSelect[V_co@string_select] # <<< note, view type unbound
# after: StringSelect[Self@CustomView]
reveal_type(self.sel)
# before: (Any, Unknown, MessageInteraction[Unknown]) -> Coroutine[Any, Any, Any]
# after: (CustomView, StringSelect[Self@CustomView], MessageInteraction[Unknown]) -> Coroutine[Any, Any, Any]
reveal_type(type(self).sel)
# before: [error]
# after: int
reveal_type(self.sel.view.x)
Additionally widens the accepted input types for the cls parameter in decorators to any matching callable, instead of just matching types. The documentation for this hasn't been adjusted (yet?), but there isn't really a reason not to support it.
Checklist
- [x] If code changes were made, then they have been tested
- [ ] I have updated the documentation to reflect the changes
- [x] I have formatted the code properly by running
pdm lint - [x] I have type-checked the code by running
pdm pyright
- [ ] This PR fixes an issue
- [ ] This PR adds something new (e.g. new method or parameters)
- [ ] This PR is a breaking change (e.g. methods or parameters removed/renamed)
- [ ] This PR is not a code change (e.g. documentation, README, ...)