|
3 | 3 | """
|
4 | 4 |
|
5 | 5 | import sys
|
6 |
| -from typing import Dict, Tuple |
| 6 | +from typing import cast, Dict, Tuple |
7 | 7 |
|
8 | 8 | # interface_name => (module, classname)
|
9 |
| -BACKENDS: Dict[str, Tuple[str, ...]] = { |
| 9 | +BACKENDS: Dict[str, Tuple[str, str]] = { |
10 | 10 | "kvaser": ("can.interfaces.kvaser", "KvaserBus"),
|
11 | 11 | "socketcan": ("can.interfaces.socketcan", "SocketcanBus"),
|
12 | 12 | "serial": ("can.interfaces.serial.serial_can", "SerialBus"),
|
|
35 | 35 | if sys.version_info >= (3, 8):
|
36 | 36 | from importlib.metadata import entry_points
|
37 | 37 |
|
38 |
| - entries = entry_points().get("can.interface", ()) |
39 |
| - BACKENDS.update( |
40 |
| - {interface.name: tuple(interface.value.split(":")) for interface in entries} |
41 |
| - ) |
| 38 | + # See https://docs.python.org/3/library/importlib.metadata.html#entry-points, "Compatibility Note". |
| 39 | + if sys.version_info >= (3, 10): |
| 40 | + BACKENDS.update( |
| 41 | + { |
| 42 | + interface.name: (interface.module, interface.attr) |
| 43 | + for interface in entry_points(group="can.interface") |
| 44 | + } |
| 45 | + ) |
| 46 | + else: |
| 47 | + # The entry_points().get(...) causes a deprecation warning on Python >= 3.10. |
| 48 | + BACKENDS.update( |
| 49 | + { |
| 50 | + interface.name: cast( |
| 51 | + Tuple[str, str], tuple(interface.value.split(":", maxsplit=1)) |
| 52 | + ) |
| 53 | + for interface in entry_points().get("can.interface", []) |
| 54 | + } |
| 55 | + ) |
42 | 56 | else:
|
43 | 57 | from pkg_resources import iter_entry_points
|
44 | 58 |
|
45 |
| - entries = iter_entry_points("can.interface") |
46 | 59 | BACKENDS.update(
|
47 | 60 | {
|
48 | 61 | interface.name: (interface.module_name, interface.attrs[0])
|
49 |
| - for interface in entries |
| 62 | + for interface in iter_entry_points("can.interface") |
50 | 63 | }
|
51 | 64 | )
|
52 | 65 |
|
|
0 commit comments