Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix typing, mostly related to async_generator #86

Merged
merged 4 commits into from
Nov 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions asyncio_mqtt/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,21 @@
Type,
Union,
cast, Iterable,
AsyncContextManager,
TypeVar,
)



try:
from contextlib import asynccontextmanager
except ImportError:
from async_generator import asynccontextmanager # type: ignore
from async_generator import asynccontextmanager as _asynccontextmanager # type: ignore
from typing_extensions import ParamSpec
_P = ParamSpec('_P')
_T = TypeVar('_T')
def asynccontextmanager(func: Callable[_P, AsyncIterator[_T]]) -> Callable[_P, AsyncContextManager[_T]]: # type: ignore
return _asynccontextmanager(func)
Comment on lines +33 to +38
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#87 has a less-verbose fix for this for python3.7+

I suspect some combination of the two is desired for support for 3.6 with type information.

Copy link
Contributor Author

@laundmo laundmo Nov 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i did test this with 3.6, though not with a 3.6 pyright (I don't think it would make a difference)

this basically just copies the exact signature that the contextlib version has, so theres no need for pyright to understand sys.version, I don't think.

maybe it would still be better to use sys.version instead of the try-except though.

i might just merge your PR into my fork, when i have some time.


import paho.mqtt.client as mqtt # type: ignore
from paho.mqtt.properties import Properties
Expand Down Expand Up @@ -92,7 +100,7 @@ def __init__(
keepalive: int = 60,
bind_address: str = "",
bind_port: int = 0,
clean_start: bool = mqtt.MQTT_CLEAN_START_FIRST_ONLY,
clean_start: int = mqtt.MQTT_CLEAN_START_FIRST_ONLY,
properties: Optional[Properties] = None,
message_retry_set: int = 20,
socket_options: Optional[Iterable[SocketOption]] = (),
Expand Down Expand Up @@ -527,7 +535,7 @@ async def __aenter__(self) -> "Client":
return self

async def __aexit__(
self, exc_type: Type[Exception], exc: Exception, tb: TracebackType
self, exc_type: Optional[Type[BaseException]], exc: Optional[BaseException], tb: Optional[TracebackType]
) -> None:
"""Disconnect from the broker."""
# Early out if already disconnected...
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
with open("asyncio_mqtt/version.py", "r") as f:
exec(f.read())

with open("README.md", "r") as readme_file:
with open("README.md", "r", encoding="utf-8") as readme_file:
readme = readme_file.read()

setup(
Expand Down Expand Up @@ -36,5 +36,6 @@
install_requires=[
"paho-mqtt>=1.6.0",
"async_generator;python_version<'3.7'",
"typing_extensions;python_version<'3.7'"
],
)