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

Improve the type hints #133

Merged
merged 8 commits into from
Oct 15, 2022
Merged

Improve the type hints #133

merged 8 commits into from
Oct 15, 2022

Conversation

JonathanPlasse
Copy link
Collaborator

Hi,
I formatted the code with black and isort. I could roll it back if you want.
I use types-paho-mqtt to update the type hints.
It pass mypy strict mode.

Copy link
Collaborator

@frederikaalund frederikaalund left a comment

Choose a reason for hiding this comment

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

Thanks for this in-depth cleanup of the code! :) See my review for details. Let me know if you have any questions. 👍

~Frederik

ciphers: Optional[str] = None,
keyfile_password: Optional[str] = None
keyfile_password: Optional[ssl._PasswordType] = None,
Copy link
Collaborator

Choose a reason for hiding this comment

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

ssl._PasswordType is private (leading underscore) so it may change. Let's not depend on it. Should they every expose it (without a leading underscore) then let's use it. 👍

Copy link
Collaborator

Choose a reason for hiding this comment

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

Same goes for ssl._SSLMethod above.

@@ -182,20 +188,22 @@ def __init__(
self._clean_start = clean_start
self._properties = properties
self._loop = asyncio.get_event_loop()
self._connected: "asyncio.Future[int]" = asyncio.Future()
self._disconnected: "asyncio.Future[Optional[int]]" = asyncio.Future()
self._connected: "asyncio.Future[Union[int, mqtt.ReasonCodes]]" = (
Copy link
Collaborator

Choose a reason for hiding this comment

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

I know that these types were wrapped in "" before already but is there any reason to do so? We don't need forward reference semantics here.

asyncio_mqtt/client.py Show resolved Hide resolved
with self._pending_call(mid, cb_result, self._pending_subscribes):
# Wait for cb_result
return await self._wait_for(cb_result, timeout=timeout)
return await self._wait_for(cb_result.wait(), timeout=timeout)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is a breaking change! Before we returned int (the ID). Now, we return None (since asyncio.Event.wait returns None).

Let's not make a breaking change at this point. I get the intent to simplify but I vote that we revert for now (to not break things). Indirectly, this changes the type back to asyncio.Future[int]

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The type annotation was wrong, the Future was of type int instead of the actual type Union[Tuple[int], List[mqtt.ReasonCodes]].
So, the return should be of type Union[Tuple[int], List[mqtt.ReasonCodes]].
It would be a breaking change to now really return an int instead of Union[Tuple[int], List[mqtt.ReasonCodes]].

Copy link
Collaborator

Choose a reason for hiding this comment

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

I agree. :) Most important part is that we keep the raw Future for now instead of an Event (the former holds a value wheras the latter doesn't). 👍

options: Optional[mqtt.SubscribeOptions] = None,
properties: Optional[Properties] = None,
timeout: int = 10,
) -> int:
Copy link
Collaborator

Choose a reason for hiding this comment

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

I like the idea to explicitly type these parameters. Makes it easier to use the library.

I know that you exhaust the current argument list for paho.Client.subscribe. In any case, let's keep the *args and **kwargs forwarding just for the sake of future compatibility. 👍 This way, if paho adds a new argument, asyncio-mqtt is still compatible (even though we don't expose explicit typings for ssaid argument).

Of course, this goes for the other functions as well.

@@ -555,7 +593,7 @@ def _on_unsubscribe(
userdata: Any,
mid: int,
properties: Optional[mqtt.Properties] = None,
reasonCodes: Optional[List[mqtt.ReasonCodes]] = None,
reasonCodes: Optional[List[mqtt.ReasonCodes] | mqtt.ReasonCodes] = None,
Copy link
Collaborator

Choose a reason for hiding this comment

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

See above (about Union over | for now).

asyncio_mqtt/client.py Show resolved Hide resolved
asyncio_mqtt/client.py Show resolved Hide resolved
asyncio_mqtt/client.py Show resolved Hide resolved
fut = self._pending_subscribes.pop(mid)
if not fut.done():
fut.set_result(granted_qos)
self._pending_subscribes.pop(mid).set()
Copy link
Collaborator

Choose a reason for hiding this comment

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

See above. Revert for now. It's not time for a breaking change. Also, the if may seem redundant but it actually avoids a race condition. Trust me. :)

- Add *args, **kwargs to subscribe, unsubscribe and publish
- Remove forward reference
- Remove private ssl types
- Revert _pending_subscribes to asyncio.Future
- Use Union instead of |
- Use List and Tuple instead of list and tuple
-  Other minor changes
@JonathanPlasse
Copy link
Collaborator Author

Should I squash and rebase the commits?

@JonathanPlasse
Copy link
Collaborator Author

Change Awaitable with Coroutine for _outgoing_call as returning the former caused a type error with anyio.abc.TaskGroup.start_soon.

Copy link
Collaborator

@frederikaalund frederikaalund left a comment

Choose a reason for hiding this comment

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

Well done. 👍 I have no further comments. LGTM.

@frederikaalund
Copy link
Collaborator

Should I squash and rebase the commits?

I can (and probably will) do that through the GitHub UI so you don't have to. :)

Change Awaitable with Coroutine for _outgoing_call as returning the former caused a type error with anyio.abc.TaskGroup.start_soon.

Another good catch! Well done. 👍


I'm very happy with this PR. Thank you for your contribution to asyncio-mqtt! Keep it coming. 😄

@frederikaalund frederikaalund merged commit a150973 into empicano:master Oct 15, 2022
@JonathanPlasse
Copy link
Collaborator Author

Thanks

@JonathanPlasse JonathanPlasse deleted the improve-typing branch October 15, 2022 17:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants