-
Notifications
You must be signed in to change notification settings - Fork 202
Add mypy support and fixup project to give no errors #512
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
Draft
sveinse
wants to merge
3
commits into
canopen-python:master
Choose a base branch
from
sveinse:feature-mypy-step1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
import logging | ||
import threading | ||
from collections.abc import MutableMapping | ||
from typing import Callable, Dict, Final, Iterator, List, Optional, Union | ||
from typing import Callable, Dict, Final, Iterator, List, Optional, Union, TYPE_CHECKING, TextIO | ||
|
||
import can | ||
from can import Listener | ||
|
@@ -16,6 +16,8 @@ | |
from canopen.sync import SyncProducer | ||
from canopen.timestamp import TimeProducer | ||
|
||
if TYPE_CHECKING: | ||
from can.typechecking import CanData | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
@@ -134,15 +136,15 @@ def __exit__(self, type, value, traceback): | |
|
||
def add_node( | ||
self, | ||
node: Union[int, RemoteNode, LocalNode], | ||
object_dictionary: Union[str, ObjectDictionary, None] = None, | ||
node: Union[int, RemoteNode], | ||
object_dictionary: Union[str, ObjectDictionary, TextIO, None] = None, | ||
upload_eds: bool = False, | ||
) -> RemoteNode: | ||
"""Add a remote node to the network. | ||
|
||
:param node: | ||
Can be either an integer representing the node ID, a | ||
:class:`canopen.RemoteNode` or :class:`canopen.LocalNode` object. | ||
:class:`canopen.RemoteNode` object. | ||
Comment on lines
-145
to
+147
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think what is wrong here is the return type annotation. It's perfectly alright to add an existing |
||
:param object_dictionary: | ||
Can be either a string for specifying the path to an | ||
Object Dictionary file or a | ||
|
@@ -157,14 +159,16 @@ def add_node( | |
if upload_eds: | ||
logger.info("Trying to read EDS from node %d", node) | ||
object_dictionary = import_from_node(node, self) | ||
node = RemoteNode(node, object_dictionary) | ||
self[node.id] = node | ||
return node | ||
nodeobj = RemoteNode(node, object_dictionary) | ||
else: | ||
nodeobj = node | ||
self[nodeobj.id] = nodeobj | ||
return nodeobj | ||
|
||
def create_node( | ||
self, | ||
node: int, | ||
object_dictionary: Union[str, ObjectDictionary, None] = None, | ||
node: Union[int, LocalNode], | ||
object_dictionary: Union[str, ObjectDictionary, TextIO, None] = None, | ||
) -> LocalNode: | ||
"""Create a local node in the network. | ||
|
||
|
@@ -179,11 +183,13 @@ def create_node( | |
The Node object that was added. | ||
""" | ||
if isinstance(node, int): | ||
node = LocalNode(node, object_dictionary) | ||
self[node.id] = node | ||
return node | ||
nodeobj = LocalNode(node, object_dictionary) | ||
else: | ||
nodeobj = node | ||
self[nodeobj.id] = nodeobj | ||
return nodeobj | ||
|
||
def send_message(self, can_id: int, data: bytes, remote: bool = False) -> None: | ||
def send_message(self, can_id: int, data: CanData, remote: bool = False) -> None: | ||
"""Send a raw CAN message to the network. | ||
|
||
This method may be overridden in a subclass if you need to integrate | ||
|
@@ -211,7 +217,7 @@ def send_message(self, can_id: int, data: bytes, remote: bool = False) -> None: | |
self.check() | ||
|
||
def send_periodic( | ||
self, can_id: int, data: bytes, period: float, remote: bool = False | ||
self, can_id: int, data: CanData, period: float, remote: bool = False | ||
) -> PeriodicMessageTask: | ||
"""Start sending a message periodically. | ||
|
||
|
@@ -227,6 +233,8 @@ def send_periodic( | |
:return: | ||
An task object with a ``.stop()`` method to stop the transmission | ||
""" | ||
if not self.bus: | ||
raise RuntimeError("Not connected to CAN bus") | ||
return PeriodicMessageTask(can_id, data, period, self.bus, remote) | ||
|
||
def notify(self, can_id: int, data: bytearray, timestamp: float) -> None: | ||
|
@@ -306,9 +314,9 @@ class PeriodicMessageTask: | |
def __init__( | ||
self, | ||
can_id: int, | ||
data: bytes, | ||
data: CanData, | ||
period: float, | ||
bus, | ||
bus: can.BusABC, | ||
remote: bool = False, | ||
): | ||
""" | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cosmetics, let's keep these separate from the mypy error fixing. There are lots of things like this to do, but better to keep a PR focused and revisit such cleanups later. IMHO.