-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Add stubs for the pyserial package #9347
Conversation
This comment has been minimized.
This comment has been minimized.
I don't know how to fix the stubtest errors |
This comment has been minimized.
This comment has been minimized.
Looks like what's going on is that stubtest is trying to import I can fix stubtest to catch this, but that means you'll probably need to turn stubtest off for this to get merged, at least until the fix is present in a released version of mypy. You can do this by setting For the record, this is the result when I run it locally with the patch:
|
This came up in python/typeshed#9347
Thank you for investigating @hauntsaninja. I applied the mypy patch locally to fix the stubtest errors. Now stubtest passes at least on linux on my machine.
This will skip all stubtest right? Is there a way to tell stubtest to just skip the problematic file? |
Not for now, no. Part of the design of stubtest is that it will attempt to import every submodule of a package at runtime in order to compare it to the stub. Since this is the only way of getting CI to pass on this PR (at least until mypy 1.0 is released), I took the liberty of adding |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
I took a look at the mypy primer diff of home assistant and here are some notes/questions:
These are genuine typing mistakes caught by mypy.
These are caused by list variance.
These are flagged because
|
Thanks for the analysis! If you genuinely believe that almost everyone will use this for USB devices, I'm fine with using I'm happy with whatever you choose, but do add a comment :-) |
This comment has been minimized.
This comment has been minimized.
Thanks for the feedback. |
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR! I spot checked several things and they all looked good to me. Will merge tomorrow :-)
I'm not aware of any good documentation of the union with Any trick. I'm also not sure all type checkers handle it the exact same way (although I think both mypy and pyright do). |
This comment has been minimized.
This comment has been minimized.
Diff from mypy_primer, showing the effect of this PR on open source code: core (https://github.com/home-assistant/core)
+ homeassistant/components/usb/__init__.py:309: error: Argument 1 to "async_add_executor_job" of "HomeAssistant" has incompatible type "Callable[[bool], List[SysFS]]"; expected "Callable[..., List[ListPortInfo]]" [arg-type]
+ homeassistant/components/modem_callerid/config_flow.py:76: error: Argument 5 to "human_readable_device_name" has incompatible type "Union[int, Any]"; expected "Optional[str]" [arg-type]
+ homeassistant/components/modem_callerid/config_flow.py:77: error: Argument 6 to "human_readable_device_name" has incompatible type "Union[int, Any]"; expected "Optional[str]" [arg-type]
+ homeassistant/components/crownstone/config_flow.py:62: error: Argument 1 to "list_ports_as_str" has incompatible type "List[SysFS]"; expected "List[ListPortInfo]" [arg-type]
+ homeassistant/components/crownstone/config_flow.py:62: note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
+ homeassistant/components/crownstone/config_flow.py:62: note: Consider using "Sequence" instead, which is covariant
+ homeassistant/components/crownstone/config_flow.py:64: error: Argument 1 to "list_ports_as_str" has incompatible type "List[SysFS]"; expected "List[ListPortInfo]" [arg-type]
+ homeassistant/components/crownstone/config_flow.py:64: note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
+ homeassistant/components/crownstone/config_flow.py:64: note: Consider using "Sequence" instead, which is covariant
|
Thank you again! |
Some USB specific attributes in pyserial's [`serial.tools.list_port_common.ListPortInfo`](https://github.com/python/typeshed/blob/main/stubs/pyserial/serial/tools/list_ports_common.pyi#L11-L24) class are not always available. They depend on the USB device and its driver correctly reporting these attributes. I discovered this recently with a new device that does not report its serial number. Only the Vendor ID `vid` and Product ID `pid` are guaranteed (This can be seen [here](https://github.com/pyserial/pyserial/blob/master/serial/tools/list_ports_linux.py#L52-L62) where `vid` and `pid` are always cast as `int` while other attributes are left as `str | None` for USB devices). This is a follow up to python#9347 and the discussion at python#9347 (comment) (CC. @hauntsaninja)
Some USB specific attributes in pyserial's [`serial.tools.list_port_common.ListPortInfo`](https://github.com/python/typeshed/blob/main/stubs/pyserial/serial/tools/list_ports_common.pyi#L11-L24) class are not always available. They depend on the USB device and its driver correctly reporting these attributes. I discovered this recently with a new device that does not report its serial number. Only the Vendor ID `vid` and Product ID `pid` are guaranteed (This can be seen [here](https://github.com/pyserial/pyserial/blob/master/serial/tools/list_ports_linux.py#L52-L62) where `vid` and `pid` are always cast as `int` while other attributes are left as `str | None` for USB devices). This is a follow up to #9347 and the discussion at #9347 (comment)
Stubs for https://pypi.org/project/pyserial/. This PR pyserial/pyserial#591 adds basic stubs for the library but it has been open for more than a year with no comment from the maintainers of pyserial. The author of the PR says they lost interest in it and will not follow up.