Skip to content

Type annotate everywhere Network is used #575

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

Merged
merged 3 commits into from
Jun 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions canopen/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,9 @@ class NodeScanner:
SERVICES = (0x700, 0x580, 0x180, 0x280, 0x380, 0x480, 0x80)

def __init__(self, network: Optional[Network] = None):
self.network = network
if network is None:
network = _UNINITIALIZED_NETWORK
self.network: Network = network
#: A :class:`list` of nodes discovered
self.nodes: List[int] = []

Expand All @@ -408,8 +410,6 @@ def reset(self):

def search(self, limit: int = 127) -> None:
"""Search for nodes by sending SDO requests to all node IDs."""
if self.network is None:
raise RuntimeError("A Network is required to do active scanning")
sdo_req = b"\x40\x00\x10\x00\x00\x00\x00\x00"
for node_id in range(1, limit + 1):
self.network.send_message(0x600 + node_id, sdo_req)
8 changes: 7 additions & 1 deletion canopen/objectdictionary/eds.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
from __future__ import annotations

import copy
import logging
import re
from configparser import NoOptionError, NoSectionError, RawConfigParser
from typing import TYPE_CHECKING

from canopen import objectdictionary
from canopen.objectdictionary import ObjectDictionary, datatypes
from canopen.sdo import SdoClient

if TYPE_CHECKING:
import canopen.network

Check warning on line 14 in canopen/objectdictionary/eds.py

View check run for this annotation

Codecov / codecov/patch

canopen/objectdictionary/eds.py#L14

Added line #L14 was not covered by tests


logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -173,7 +179,7 @@
return od


def import_from_node(node_id, network):
def import_from_node(node_id: int, network: canopen.network.Network):
""" Download the configuration from the remote node
:param int node_id: Identifier of the node
:param network: network object
Expand Down
9 changes: 7 additions & 2 deletions canopen/sync.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
from typing import Optional
from __future__ import annotations

from typing import Optional, TYPE_CHECKING

if TYPE_CHECKING:
import canopen.network

Check warning on line 6 in canopen/sync.py

View check run for this annotation

Codecov / codecov/patch

canopen/sync.py#L6

Added line #L6 was not covered by tests


class SyncProducer:
Expand All @@ -7,7 +12,7 @@
#: COB-ID of the SYNC message
cob_id = 0x80

def __init__(self, network):
def __init__(self, network: canopen.network.Network):
self.network = network
self.period: Optional[float] = None
self._task = None
Expand Down
9 changes: 7 additions & 2 deletions canopen/timestamp.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from __future__ import annotations

import struct
import time
from typing import Optional
from typing import Optional, TYPE_CHECKING

if TYPE_CHECKING:
import canopen.network

Check warning on line 8 in canopen/timestamp.py

View check run for this annotation

Codecov / codecov/patch

canopen/timestamp.py#L8

Added line #L8 was not covered by tests
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
import canopen.network
import canopen.network

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 following code is not a class so it's strictly not needed to have two lines according to black. Is your preference to always have two spaces below imports?

(PS! Can I encourage you to write a coding guideline document? It certainly would lessen your need to review and comment on styling.)

Copy link
Member

Choose a reason for hiding this comment

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

Is your preference to always have two spaces below imports?

Yes.

Sorry I won't have much time to invest for documentation in the coming weeks I'm afraid. Hope it gets better soon.



# 1 Jan 1984
Expand All @@ -17,7 +22,7 @@
#: COB-ID of the SYNC message
cob_id = 0x100

def __init__(self, network):
def __init__(self, network: canopen.network.Network):
self.network = network

def transmit(self, timestamp: Optional[float] = None):
Expand Down
2 changes: 1 addition & 1 deletion test/test_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ def test_scanner_reset(self):
self.assertListEqual(self.scanner.nodes, [])

def test_scanner_search_no_network(self):
with self.assertRaisesRegex(RuntimeError, "Network is required"):
with self.assertRaisesRegex(RuntimeError, "No actual Network object was assigned"):
self.scanner.search()

def test_scanner_search(self):
Expand Down
Loading