Skip to content

Commit 9e795de

Browse files
authored
refactor for mypy friendly version checks (#1371)
1 parent 88fc8e5 commit 9e795de

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

can/interfaces/__init__.py

+9-12
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
Interfaces contain low level implementations that interact with CAN hardware.
33
"""
44

5+
import sys
6+
from typing import Dict, Tuple
7+
58
# interface_name => (module, classname)
6-
BACKENDS = {
9+
BACKENDS: Dict[str, Tuple[str, ...]] = {
710
"kvaser": ("can.interfaces.kvaser", "KvaserBus"),
811
"socketcan": ("can.interfaces.socketcan", "SocketcanBus"),
912
"serial": ("can.interfaces.serial.serial_can", "SerialBus"),
@@ -29,27 +32,21 @@
2932
"socketcand": ("can.interfaces.socketcand", "SocketCanDaemonBus"),
3033
}
3134

32-
try:
35+
if sys.version_info >= (3, 8):
3336
from importlib.metadata import entry_points
3437

35-
try:
36-
entries = entry_points(group="can.interface")
37-
except TypeError:
38-
# Fallback for Python <3.10
39-
# See https://docs.python.org/3/library/importlib.metadata.html#entry-points, "Compatibility Note"
40-
entries = entry_points().get("can.interface", [])
41-
38+
entries = entry_points().get("can.interface", ())
4239
BACKENDS.update(
4340
{interface.name: tuple(interface.value.split(":")) for interface in entries}
4441
)
45-
except ImportError:
42+
else:
4643
from pkg_resources import iter_entry_points
4744

48-
entry = iter_entry_points("can.interface")
45+
entries = iter_entry_points("can.interface")
4946
BACKENDS.update(
5047
{
5148
interface.name: (interface.module_name, interface.attrs[0])
52-
for interface in entry
49+
for interface in entries
5350
}
5451
)
5552

0 commit comments

Comments
 (0)