Skip to content

Commit

Permalink
Pyright reports false positive error with asynccontextmanager (#87)
Browse files Browse the repository at this point in the history
Pyright gets confused and will give you error messages like this:
```
error: Object of type "AsyncIterator[AsyncGenerator[...]]" cannot be used with "with" because it does not implement __aenter__ (reportGeneralTypeIssues)
error: Object of type "AsyncIterator[AsyncGenerator[...]]" cannot be used with "with" because it does not implement __aexit__ (reportGeneralTypeIssues)
```
For code like this:
```python
async with Client("test.mosquitto.org") as client:
    async with client.filtered_messages("floors/+/humidity") as messages:
        await client.subscribe("floors/#")
        async for message in messages:
            print(message.payload.decode())
```

This change statically checks against `sys.version_info` for the right version, which Pyright understands.
  • Loading branch information
sdwilsh committed Dec 5, 2021
1 parent aeee308 commit d0fd6b4
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions asyncio_mqtt/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
import socket
import ssl
import sys
from contextlib import contextmanager, suppress
from enum import IntEnum
from types import TracebackType
Expand All @@ -27,9 +28,9 @@



try:
if sys.version_info >= (3, 7)
from contextlib import asynccontextmanager
except ImportError:
else:
from async_generator import asynccontextmanager as _asynccontextmanager # type: ignore
from typing_extensions import ParamSpec
_P = ParamSpec('_P')
Expand Down

0 comments on commit d0fd6b4

Please sign in to comment.