dbusutil: Type hinting improvements #437
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR improves the type hinting in
dbusutil
.bus
. basedPyright was warning "Function calls and mutable objects not allowed within parameter default value expression", and when looking it up, it seems this is for good reason. This has been replaced with defaulting the parameter toNone
and if it isNone
, assign it toQDBusConnection.sessionBus
inside of the function body.None
and you just want to state in the re-definition that it will never beNone
(I guess the expectation is that the code speaks for itself, and basedPyright is able to infer that the type will never beNone
)create_and_send_dbus_message
I accidentally set it asQDBusConnection.sessionBus
without the parentheses, so if we ever actually called this withoutbus
it would probably have crashed!constants.py
'sDBUS_INTERFACES_AND_SIGNALS
constant.dict[str, Any]
increate_and_send_dbus_message
because DBus should always get a string key (e.g.progress
) but the actual value given could beAny
thing depending on what signal we give.dbus_progress_message
because we know what the valid values are for this dict, we know that it should only ever have an int, float, or boolean as values (int/float for progress, int for count, and bool for the visibility indicators).bus.send
to_
to make it explicit thatbus.send
does return a value, but we don't capture it (this was flagged up by basedPyright). Thebus.send
cal returns abool
(I assume a success boolean). Ignoring this is something we might want to discuss for a couple of reasons.bool
from these functions? Right now we don't consume any return at all when we call this so we'd have to move the_ =
pattern up a level into the caller of this function, but it's up to you.bool
from these functions but just ignoring it at the caller level, so that we have the return if we ever decide we want it (e.g. for unit tests)_ =
ignore pattern we want to have in place across the codebase? I'm cautious of doing things just to appease a checker, but it can make it clear that a function is expected to return a value, but that we aren't using it.Just some general typing improvements with this one, motivated by a desire to write some form of test for
dbusutil
. It helps a lot to have good hinting for tests alone it feels like from my (albeit brief) experience, and usage and readability of course is also helped 😄