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

Add typing with mypy #791

Merged
merged 1 commit into from
Jan 10, 2024
Merged

Add typing with mypy #791

merged 1 commit into from
Jan 10, 2024

Conversation

PierreF
Copy link
Contributor

@PierreF PierreF commented Jan 3, 2024

Split of #786 : contains add of mypy on client.py

@PierreF PierreF mentioned this pull request Jan 3, 2024
@PierreF
Copy link
Contributor Author

PierreF commented Jan 3, 2024

Tests are a bit flappy (not related to this PR, it's also happen on master or other PR). @akx did you also had unstable tests result for PR you created recently (i.e. did you re-run failed tests until they succeed) ? Because if you didn't had to re-run failed test, tests seems to fail more often recently.

Edit: you already answered this question in #789 :)

Copy link
Contributor

@akx akx left a comment

Choose a reason for hiding this comment

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

Some softer suggestions and some actual bug-looking things here.

src/paho/mqtt/client.py Show resolved Hide resolved
src/paho/mqtt/client.py Outdated Show resolved Hide resolved
src/paho/mqtt/client.py Outdated Show resolved Hide resolved
src/paho/mqtt/client.py Outdated Show resolved Hide resolved
src/paho/mqtt/client.py Outdated Show resolved Hide resolved
src/paho/mqtt/publish.py Outdated Show resolved Hide resolved
src/paho/mqtt/publish.py Outdated Show resolved Hide resolved
@@ -10,7 +10,7 @@ def on_message(mqttc, obj, msg):
assert msg.topic == "pub/qos1/receive", f"Invalid topic: ({msg.topic})"
assert msg.payload == expected_payload, f"Invalid payload: ({msg.payload})"
assert msg.qos == 1, f"Invalid qos: ({msg.qos})"
assert msg.retain is not False, f"Invalid retain: ({msg.retain})"
assert not msg.retain, f"Invalid retain: ({msg.retain})"
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't this the inverse test c.f. before?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, but I think the test was wrong.

  • We don't produce a message with retain flag in this test, so it make sense that msg.retain is False
  • Previously, msg.retain was an interger so msg.retain is not False was always true.
  • I've checked and the value was 0. So it seems expected to assert the value False

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe this change should be done in a separate PR, then? I'm afraid it will confuse future code archeologists terribly if this happens in a commit that says "Add typing".

tests/test_client.py Outdated Show resolved Hide resolved
tox.ini Show resolved Hide resolved
@PierreF
Copy link
Contributor Author

PierreF commented Jan 3, 2024

Thank for the review. I've only fixed few of your feedback but it's bed time now, I'll continue tomorrow.

Copy link
Contributor

@akx akx left a comment

Choose a reason for hiding this comment

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

A couple more comments - sorry it took a while for the second round of reviews. Starting to look pretty nice though, excellent renovation here :) 🛠️

src/paho/mqtt/enums.py Outdated Show resolved Hide resolved
src/paho/mqtt/enums.py Show resolved Hide resolved
src/paho/mqtt/enums.py Outdated Show resolved Hide resolved
Comment on lines 67 to 70
mqtt_cs_new = 0
mqtt_cs_connected = 1
mqtt_cs_disconnecting = 2
mqtt_cs_connect_async = 3
Copy link
Contributor

Choose a reason for hiding this comment

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

These should maybe also be UPPER_SNAKE_CASE? The compatibility aliases in client.py can of course remain lower-case.

Comment on lines 24 to 26
import typing
from collections.abc import Iterable
from typing import Any, List, Tuple, Union
Copy link
Contributor

Choose a reason for hiding this comment

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

Might be good to import the typing one way only; I'd suggest the from typing way.

Comment on lines 3977 to 3978
return WebsocketWrapper(sock, self._host, self._port, self._ssl,
self._websocket_path, self._websocket_extra_headers)
Copy link
Contributor

Choose a reason for hiding this comment

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

Would maybe read easier as

Suggested change
return WebsocketWrapper(sock, self._host, self._port, self._ssl,
self._websocket_path, self._websocket_extra_headers)
return WebsocketWrapper(
sock,
self._host,
self._port,
self._ssl,
self._websocket_path,
self._websocket_extra_headers,
)

(and I'd also consider adding kwargs, so it's harder to accidentally pass things in the wrong order)

def _create_socket_connection(self):
def _create_socket(self) -> SocketLike:
tcp_sock = self._create_socket_connection()
sock = self._create_ssl_socket_if_enable(tcp_sock)
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure the _if_enable is a good idea - I'd just do

Suggested change
sock = self._create_ssl_socket_if_enable(tcp_sock)
if self._ssl:
sock = self._ssl_wrap_socket(tcp_sock)

or similar? That would also simplify the return type of that function.

Comment on lines 4020 to 4021
if (hasattr(self._ssl_context, 'check_hostname') and
self._ssl_context.check_hostname): # type: ignore
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (hasattr(self._ssl_context, 'check_hostname') and
self._ssl_context.check_hostname): # type: ignore
if getattr(self._ssl_context, 'check_hostname', False): # type: ignore

@@ -520,8 +636,9 @@ def __init__(self, client_id="", clean_session=None, userdata=None,
self._transport = transport.lower()
self._protocol = protocol
self._userdata = userdata
self._sock = None
self._sockpairR, self._sockpairW = (None, None,)
self._sock: socket.socket | WebsocketWrapper | ssl.SSLSocket | None = None
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
self._sock: socket.socket | WebsocketWrapper | ssl.SSLSocket | None = None
self._sock: SocketLike | None = None

Comment on lines -3126 to +3353
reason = 132 # Unsupported protocol version
reason = ReasonCodes(CONNACK >> 4, aName="Unsupported protocol version")
Copy link
Contributor

Choose a reason for hiding this comment

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

This change doesn't seem strictly type-related - would be nice to be sure it's covered by tests, at least?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've done this after adding type to the on_connect callback and saw that reason could be either a int or a ReasonCodes. I believe using int was a mistake.

Anyway I've added a test, and test show that it should no break compatibility (reason == 132 work)

@PierreF
Copy link
Contributor Author

PierreF commented Jan 9, 2024

All your comments should be resolved. I think I'll squash-merge this PR due to multiple commits adding the removing thing. If you believe that we should kept multiple commits, I can do a rebase to only squash together part that belong to the same commit (but I want to squash or rebase, because their is change + exact revert of that change in this PR)

@akx
Copy link
Contributor

akx commented Jan 10, 2024

@PierreF A squash-merge, or a squashing rebase in this PR before merging sounds fine :) In fact, if #791 gets merged (and Codecov gets a baseline of the coverage for master), this could be squash-rebased on master and we'd see the coverage diff here too (hopefully).

@PierreF PierreF merged commit 1097a18 into master Jan 10, 2024
18 of 21 checks passed
@PierreF PierreF deleted the add-typing branch January 10, 2024 20:34
@akx akx mentioned this pull request Jan 11, 2024
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