-
-
Notifications
You must be signed in to change notification settings - Fork 343
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 generic typing support for Memory[Send/Receive]Channel #2549
Conversation
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## master #2549 +/- ##
==========================================
+ Coverage 89.26% 92.45% +3.19%
==========================================
Files 118 118
Lines 16334 16346 +12
Branches 1800 3155 +1355
==========================================
+ Hits 14580 15113 +533
+ Misses 1462 1104 -358
+ Partials 292 129 -163
|
9674e6b
to
f7df07b
Compare
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.
I've got a couple nitpicks re: types
cc @agronholm as you've had some opinions on types before (hopefully I'm not confusing you with someone else? My memory is fuzzy...) |
The previous typing PR used |
Pushed a commit with updates from the reviews, though I got stuck trying to resolve trio/_channel.py:100: error: Incompatible return value type
(got "Tuple[MemorySendChannel[SendType], MemoryReceiveChannel[ReceiveType]]",
expected "Tuple[MemorySendChannel[T], MemoryReceiveChannel[T]]") [return-value] and I'm in general quite confused how to write that the function, with the I looked at trio-typing, and they resort to making # written as a class so you can say open_memory_channel[int](5)
class open_memory_channel(Tuple[MemorySendChannel[T], MemoryReceiveChannel[T]]):
def __new__( # type: ignore[misc] # "must return a subtype"
cls, max_buffer_size: float
) -> Tuple[MemorySendChannel[T], MemoryReceiveChannel[T]]: ...
def __init__(self, max_buffer_size: float): ... which looks kinda hackish, but maybe is fine? (though this doesn't resolve my typing headaches, and mostly just makes it so you don't need to use |
I think this is because of |
Ah, that made me think of the super simple solution! return (
MemorySendChannel[T]._create(state),
MemoryReceiveChannel[T]._create(state),
) I should definitely write some test cases to check that stuff actually works though. |
This requires Python 3.8 at minimum, just FYI. |
ah, shoot |
Actually, no. The class level |
Yay! Replacing the I also wrote some tests using https://github.com/davidfritzsche/pytest-mypy-testing to check for expected mypy error outputs, but it's kinda broken and unmaintained, and I failed to get it working with skipping on earlier versions, so not gonna try and get that working in the CI. But I did get it passing locally at least. It might be worth trying to get trio-typings testing infrastructure replicated here. |
d5fd09f
to
9db5501
Compare
also rebased and squashed, but the return type of |
According to the docs, Maybe we should brainstorm a nicer interface for this? (we could call it a one-shot channel /s :^) But we should probably just make an issue instead of making one on the spot. |
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.
A couple last comments, I think everything else is done.
This is definitely above my paygrade in terms of experience or opinions on trio, so a separate issue to evaluate it sounds good to me if you want to tackle it. |
9db5501
to
8243d23
Compare
8243d23
to
798e87a
Compare
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.
Looks good, someone else should take a look and merge if it looks good to them too.
A big thank you to @A5rocks for the very helpful and detailed reviews! 🚀 |
Fixes #1327
I didn't figure out a good way to add tests for it, but I reproduced the error from the original PR, and it now works as expected (in a somewhat pared down variant of it):
One way would be to add type hints to the tests in
trio/tests/test_channel.py
, and also check thetrio.tests
submodule with mypy incheck.sh
As noted in https://github.com/python-trio/trio/blob/master/trio/_util.py#L251-L254
@generic_function
won't type-check, and I didn't figure out what mypy plugin / clever stub would enable that.I didn't manage to figure out what
trio.lowlevel.wait_task_rescheduled
actually returns, so it's possible that the return type onMemoryReceiveChannel.receive
is incorrect.I assumed that the co/contravariantness of the
TypeVar
s intrio/_abc.py
forSendChannel
andMemoryChannel
would apply for Memory[Send/Receive]Channel as well.While on it I also typed ~everything in
trio/_channel.py
. (though the__exit__
ones look kinda crazy 😅 )