Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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: 6 additions & 0 deletions can/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,12 @@ def _create_bus_config(config: dict[str, Any]) -> typechecking.BusConfig:
if "fd" in config:
config["fd"] = config["fd"] not in (0, False)

if "state" in config and not isinstance(config["state"], can.BusState):
try:
config["state"] = can.BusState[config["state"]]
except KeyError as e:
raise ValueError("State config not valid!") from e

return cast("typechecking.BusConfig", config)


Expand Down
2 changes: 1 addition & 1 deletion doc/interfaces/pcan.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Here is an example configuration file for using `PCAN-USB <https://www.peak-syst
[default]
interface = pcan
channel = PCAN_USBBUS1
state = can.bus.BusState.PASSIVE
state = PASSIVE
bitrate = 500000

``channel`` (default ``"PCAN_USBBUS1"``)
Expand Down
23 changes: 23 additions & 0 deletions test/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,29 @@ def test_bit_timing_fd_cfg(self):
assert timing.data_tseg2 == 10
assert timing.data_sjw == 10

def test_state_with_str(self):
can_cfg = _create_bus_config(
{
**self.base_config,
"state": "PASSIVE",
}
)
state = can_cfg["state"]
assert isinstance(state, can.BusState)
assert state == can.BusState.PASSIVE
Copy link
Collaborator

Choose a reason for hiding this comment

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

nitpick: enums should be compared by identity (state is can.BusState.PASSIVE)


def test_state_with_enum(self):
expected_state = can.BusState.PASSIVE
can_cfg = _create_bus_config(
{
**self.base_config,
"state": expected_state,
}
)
state = can_cfg["state"]
assert isinstance(state, can.BusState)
assert state == expected_state
Copy link
Collaborator

Choose a reason for hiding this comment

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

same here



class TestChannel2Int(unittest.TestCase):
def test_channel2int(self) -> None:
Expand Down
Loading