-
-
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
pyserial: make serial.threaded.ReaderThread generic in the type of Protocol #13425
Conversation
This comment has been minimized.
This comment has been minimized.
…otocol This change makes `serial.threaded.ReaderThread` generic in the type of the `serial.threaded.Protocol` that it wraps. Currently, `ReaderThread`'s `connect` and context-manager `__enter__` methods, along with the `protocol` instance property, return a `serial.threaded.Protocol`. However, it is usually the case that `ReaderThread` will wrap a specific subclass of `serial.threaded.Protocol`, which has additional protocol-specific methods defined on it. So the return value of `serial.threaded.Protocol` is not really useful and it needs to be cast to the actual `serial.threaded.Protocol` subclass type, so that other protocol methods can be called without causing a type-checker error. This change makes `ReaderThread` generic, and the wrapped `serial.threaded.Protocol` type a TypeVar, so that `ReaderThread` methods can return the specific `serial.threaded.Protocol` subclass rather than `serial.threaded.Protocol` itself.
This comment has been minimized.
This comment has been minimized.
I'm not sure if this will require users to change annotations using |
Here is a link to the implementation in case it helps anyone reviewing. |
Co-authored-by: Sebastian Rittau <[email protected]>
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
I'm not sure what would be the preferred way to deal with the F821 (undefined-name) linter error for Should I quote It seems like this should be fixed in Ruff so it is treated the same way as |
I have opened an issue in Ruff: astral-sh/ruff#15677 |
Also, I may not be understanding the linter config correctly but there is potentially an issue in the typeshed linter config Lines 36 to 38 in ce521e8
Line 5 in ce521e8
it seems actually Ruff is checking the F821 as well. I'm not sure if Ruff is actually checking unused-noqa (RUF100). The It looks like RUF100 in Ruff can replace NQA101, NQA102, and NQA103 from flake8-noqa. So we could possibly change linter config so that flake8 does not check NQA101, NQA102, and NQA103 but Ruff does check RUF100? |
To fix Ruff undefined-name (F821)
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! I'm not familiar with pyserial, but the changes as described by you make sense to me.
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
This change makes
serial.threaded.ReaderThread
generic in the type of theserial.threaded.Protocol
that it wraps.Currently,
ReaderThread
'sconnect
and context-manager__enter__
methods, along with theprotocol
instance property, return aserial.threaded.Protocol
. However, it is usually the case thatReaderThread
will wrap a specific subclass ofserial.threaded.Protocol
, which has additional protocol-specific methods defined on it. So the return value ofserial.threaded.Protocol
is not really useful and it needs to be cast to the actualserial.threaded.Protocol
subclass type, so that other protocol methods can be called without causing a type-checker error.This change makes
ReaderThread
generic, and the wrappedserial.threaded.Protocol
type a TypeVar, so thatReaderThread
methods can return the specificserial.threaded.Protocol
subclass rather thanserial.threaded.Protocol
itself.