fbchat-dev / fbchat-dev/fbchat
mypy cannot typecheck event classes decorated with @attrs_event
- Dominant language
- Python
- Stars
- 1.2k
- Forks
- 403
- PR merge metrics
- No merged PRs in 30d
Description
## Description of the problem
Due to a known bug/missing feature in the mypy plugin for attrs, mypy cannot correctly infer keyword arguments for a class which is decorated with @attrs_event, or inherits from a class decorated with [@attrs_event](https://github.com/carpedm20/fbchat/blob/master/fbchat/_events/_common.py#L8).
The relevant issues on mypy: https://github.com/python/mypy/issues/5406
Two related issues: https://github.com/python-attrs/attrs/issues/594, https://github.com/python/mypy/issues/6239
There are workarounds provided in the issues. However, as a quick fix, it's possible to just replace all instances of @attrs_event with @attr.s(slots=True, kw_only=kw_only, frozen=True), after which mypy seems to typecheck fine. Since the @attrs_event decorator is not exported from fbchat anyway, this seems like an acceptable fix,
While this project doesn't seem to use mypy, it would be nice to fix this so that other projects that use fbchat can use mypy. It's a shame having type hints in fbchat that can't be leveraged by dependants.
For now, I can just workaround with `# type: ignore`, but this means I can't get the benefit of typechecking for any events I subclass from those provided by fbchat.
## Code to reproduce
```py
import fbchat
import attr
@attr.s
class MyEvent(fbchat.MessageEvent):
def makeMyEvent(self, event: fbchat.MessageEvent):
return MyEvent(thread=event.thread, author=event.author, message=event.message, at=event.at)
```
## Traceback
```
Unexpected keyword argument "thread" for "MyEvent"mypy(error)
Unexpected keyword argument "author" for "MyEvent"mypy(error)
Unexpected keyword argument "message" for "MyEvent"mypy(error)
Unexpected keyword argument "at" for "MyEvent"mypy(error)
```
## Environment information
- Python version 3.8.3
- `fbchat` version 2.0.0a2
Contributor guide
Assessment
This issue has not been assessed yet.