-
Notifications
You must be signed in to change notification settings - Fork 326
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
Closed
Closed
Changes from 1 commit
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,18 +3,21 @@ | |
| from collections.abc import MutableMapping | ||
| import logging | ||
| import threading | ||
| from typing import Callable, Dict, Iterator, List, Optional, Union | ||
| from typing import Callable, Dict, Iterator, List, Optional, Union, TYPE_CHECKING, TextIO | ||
|
|
||
| try: | ||
| import can | ||
| from can import Listener | ||
| from can import CanError | ||
| except ImportError: | ||
| # Do not fail if python-can is not installed | ||
| can = None | ||
| CanError = Exception | ||
| class Listener: | ||
| """ Dummy listener """ | ||
| # Type checkers don't like this conditional logic, so it is only run when | ||
| # not type checking | ||
| if not TYPE_CHECKING: | ||
| # Do not fail if python-can is not installed | ||
| can = None | ||
| CanError = Exception | ||
| class Listener: | ||
| """ Dummy listener """ | ||
sveinse marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| from canopen.node import RemoteNode, LocalNode | ||
| from canopen.sync import SyncProducer | ||
|
|
@@ -24,6 +27,9 @@ class Listener: | |
| from canopen.objectdictionary.eds import import_from_node | ||
| from canopen.objectdictionary import ObjectDictionary | ||
|
|
||
| if TYPE_CHECKING: | ||
| from can.typechecking import CanData | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| Callback = Callable[[int, bytearray, float], None] | ||
|
|
@@ -45,7 +51,7 @@ def __init__(self, bus: Optional[can.BusABC] = None): | |
| #: List of :class:`can.Listener` objects. | ||
| #: Includes at least MessageListener. | ||
| self.listeners = [MessageListener(self)] | ||
| self.notifier = None | ||
| self.notifier: Optional[can.Notifier] = None | ||
sveinse marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| self.nodes: Dict[int, Union[RemoteNode, LocalNode]] = {} | ||
| self.subscribers: Dict[int, List[Callback]] = {} | ||
| self.send_lock = threading.Lock() | ||
|
|
@@ -138,15 +144,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
Member
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 | ||
|
|
@@ -161,14 +167,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, | ||
| object_dictionary: Union[str, ObjectDictionary, TextIO, None] = None, | ||
| ) -> LocalNode: | ||
| """Create a local node in the network. | ||
|
|
||
|
|
@@ -183,11 +191,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 | ||
|
|
@@ -215,7 +225,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. | ||
|
|
||
|
|
@@ -295,7 +305,7 @@ class PeriodicMessageTask: | |
| def __init__( | ||
| self, | ||
| can_id: int, | ||
| data: bytes, | ||
| data: CanData, | ||
| period: float, | ||
| bus, | ||
| remote: bool = False, | ||
|
|
@@ -335,10 +345,12 @@ def update(self, data: bytes) -> None: | |
| old_data = self.msg.data | ||
| self.msg.data = new_data | ||
| if hasattr(self._task, "modify_data"): | ||
| assert self._task is not None # This will never be None, but mypy needs this | ||
sveinse marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| self._task.modify_data(self.msg) | ||
| elif new_data != old_data: | ||
| # Stop and start (will mess up period unfortunately) | ||
| self._task.stop() | ||
| if self._task is not None: | ||
| self._task.stop() | ||
sveinse marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| self._start() | ||
|
|
||
|
|
||
|
|
||
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.