-
Notifications
You must be signed in to change notification settings - Fork 5
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
Fix typing and a few bugs #14
Fix typing and a few bugs #14
Conversation
4e0ab72
to
9b1d8df
Compare
Actually, same true for many uses of aclosing(), but Pyright's not complaining about those, so ignoring for now.
I.e. because the input could be None
Plus a couple of typos I happened to notice.
9b1d8df
to
1a42eaa
Compare
I force-pushed to add tests to each of the bugfix commits. The one about some iterators not having an Please let me know if you're comfortable with this |
Overall looks great. I have some questions though. You added import 'as' to all from x import y. I am not up to date on what this achieves. Can you provide some source for this? I don't think I am completely sold on the safe_aclosing concept. One of the main pain points I had with async iterators throughout my projects was the unpredictability of them, if you didn't force them to explicitly close, so it is just something I got into the habit of forcing myself to think about. And I guess my worry is, if someone is really creating manual async iterators that don't support aclose, are these people sure that they actually know what they are doing? Do I want to take that chance, or am I fine with my code just failing in this case? The patch adds a not insignificant amount of extra code, for a corner case that we might not ever run into. |
@andersea This "redundant import" form signals type-checkers that these symbols are intended as public interface (i.e. it's ok to import them from this module even though that's not where they were originally defined). Without this, type checkers will complain about imports from here (can confirm that Pyright and MyPy both do). See https://typing.readthedocs.io/en/latest/source/libraries.html#library-interface-public-and-private-symbols (the alternative is to define an |
@andersea That's an understandable concern - in fact I agree, as I've run into similar issues myself in the past. The problem is that as it is, all of the uses of The alternative is to change the type hints instead: rather than having all of the inputs annotated as Of course, none of this is our fault. This was a miss when the language maintainers defined the async iteration protocol, but we're stuck with the consequences. Users could import the new protocol from Slurry to work around the above problem, but IMO, requiring people to break the common convention if they want proper type-checkability is pretty onerous and a bit too opinionated. I would suggest letting me remove those new protocols and switch back to the standard ones. Instead we can just document the need for |
e688d43
to
98a65f3
Compare
- So now aclose() is explicitly required. - Since we effectively had an in-project `aclosing()` implementation, also removed async_generator dep. - The new, better-annotated `aclosing()` triggered Pyright to alert on a few places where it was being applied to an iterable instead of an iterator. Fixed those, too.
98a65f3
to
06d5e2e
Compare
I am sorry. I tried looking through this, but there are just too many changes, for me to be able to mentalize it. Can we focus on one (general) issue at a time per pr, please? Like, if there are obvious typing changes that doesn't significantly change the underlying code. For example fixing marking the package as typed in a separate pr? Cleaning up type hints that doesn't change the (possibly broken?) aclosing semantics in another. I am thinking all the no-brainer stuff. Then maybe we can deal with the aclosing issue in the end? Edit: If possible, I would like to go module by module, even if we don't completely fix one issue with a single pr. I apologize but I just don't have the headspace right now to look through the entire code for each pr. |
@andersea No worries, I get it. Following all of the typing implications at once did, indeed, balloon a bit. :P Yes, I'll close this PR and try to break it down more in the next few days. It will mean that each step along the way still won't pass a type checker (which is the current state), but they'll be more manageable. |
I needed type annotations for Slurry to make the type checks in my own project happy. I'd been using my own type stubs for a couple of days, but thought I'd contribute back.
These typing fixes include a few that were tripping up my thing plus everything reported by Pyright in standard mode (only with
slurry/
, not the tests). A few of the issues noticed by Pyright were actually bugs, so I took the liberty of fixing those, as well. See commit messages for more info.A couple of things I didn't do, but I'd be happy to if you're open to it. They're actually kinda related and I'd like to do both:
I spotted a (rare) bug that Pyright didn't explicitly warn about:- Edit: ended up fixing in later commitsaclosing()
is used in many places on async iterators, but not all async iterators have anaclose()
method. This will error if someone passes a source that doesn't. This is admittedly unusual because they're almost always from generator functions or comprehensions (and more importantly this is the case for memory receive channels, which covers 99% of the time), but it's a non-zero chance kind of problem. I would fix it by creating asafe_aclosing()
context manager to replaceaclosing()
. It would have no dependency onaclosing()
, which has the side benefit that theasync_generator
package dependency would no longer be needed at all. That would be good because...There are many errors right now, mostly coming from the fact that the- Edit: ended up fixing in later commitsasync_generator
package is untyped (and there are no type stubs available for it). It's only used foraclosing()
, which is just a few lines anyway.Edit: Oh, and another rare bug I didn't fix:
3.
sources are (async-)iterated directly in many places, but they typed as iterables, not iterators. This isn't usually a problem because most iterables act as their own iterators, but not all. Iteration (with or without- Edit: ended up fixing in later commitsaclose()
wrapping it) will error in those cases. If the typing claimsAsyncIterable
, then anyAsyncIterable
should be supported.Btw, I suspect that fixing the lack of typing on
aclose()
mentioned in item 2 would result in Pyright properly warning about both of the bugs. In any case, they're not regressions, and as I said, quite rare to strike anyway. IMO they don't need to hold up a release or anything.