Skip to content
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

How to access typing for different CAN bus interface types so Pylance can know which instance attributes exist? #1880

Open
ilanbiala opened this issue Oct 23, 2024 · 2 comments

Comments

@ilanbiala
Copy link

Currently instantiating a bus looks something like:

bus: can.bus.BusABC  = can.Bus(channel="can0", can_filters=[...], interface="socketcan", bitrate=250_000)

However, then bus.channel is unknown because it is not defined in can.bus.BusABC, but channel is a defined and used attributed in subclasses such as KvaserBus and SocketcanBus. Is there a more specific type that can be used, or something that exposes the channel argument provided when instantiating a CAN bus?

@zariiii9003
Copy link
Collaborator

I think you'd have to do if isinstance(bus, KvaserBus) or if hasattr(bus, "channel") for this.
We should probably add a channel property to BusABC,

@ilanbiala
Copy link
Author

Yeah, generally I'd rather avoid conditionals to appease Mypy/Pylance, etc. I skimmed through the code and it does seem like BusABC should have channel and that member value could be instantiated by each interface implementation? Only hiccup is that it seems like each implementation could have a different type for channel, e.g. SocketCAN expects channel is a str while Kvaser expects channel is an int.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants