1515from threading import RLock
1616from random import randint
1717
18- from can import CanError
18+ from can import CanOperationError
1919from can .bus import BusABC
2020from can .message import Message
21+ from can .typechecking import AutoDetectedConfig
2122
2223logger = logging .getLogger (__name__ )
2324
2425
2526# Channels are lists of queues, one for each connection
2627if TYPE_CHECKING :
27- # https://mypy.readthedocs.io/en/stable/common_issues .html#using-classes-that-are-generic-in-stubs-but-not-at-runtime
28+ # https://mypy.readthedocs.io/en/stable/runtime_troubles .html#using-classes-that-are-generic-in-stubs-but-not-at-runtime
2829 channels : Dict [Optional [Any ], List [queue .Queue [Message ]]] = {}
2930else :
3031 channels = {}
@@ -81,12 +82,12 @@ def __init__(
8182 self .channel .append (self .queue )
8283
8384 def _check_if_open (self ) -> None :
84- """Raises CanError if the bus is not open.
85+ """Raises :class:`~can.CanOperationError` if the bus is not open.
8586
8687 Has to be called in every method that accesses the bus.
8788 """
8889 if not self ._open :
89- raise CanError ( "Operation on closed bus" )
90+ raise CanOperationError ( "Cannot operate on a closed bus" )
9091
9192 def _recv_internal (
9293 self , timeout : Optional [float ]
@@ -116,8 +117,9 @@ def send(self, msg: Message, timeout: Optional[float] = None) -> None:
116117 bus_queue .put (msg_copy , block = True , timeout = timeout )
117118 except queue .Full :
118119 all_sent = False
120+
119121 if not all_sent :
120- raise CanError ("Could not send message to one or more recipients" )
122+ raise CanOperationError ("Could not send message to one or more recipients" )
121123
122124 def shutdown (self ) -> None :
123125 if self ._open :
@@ -131,7 +133,7 @@ def shutdown(self) -> None:
131133 del channels [self .channel_id ]
132134
133135 @staticmethod
134- def _detect_available_configs ():
136+ def _detect_available_configs () -> List [ AutoDetectedConfig ] :
135137 """
136138 Returns all currently used channels as well as
137139 one other currently unused channel.
@@ -146,7 +148,9 @@ def _detect_available_configs():
146148 available_channels = list (channels .keys ())
147149
148150 # find a currently unused channel
149- get_extra = lambda : "channel-{}" .format (randint (0 , 9999 ))
151+ def get_extra ():
152+ return f"channel-{ randint (0 , 9999 )} "
153+
150154 extra = get_extra ()
151155 while extra in available_channels :
152156 extra = get_extra ()
0 commit comments