Replies: 1 comment 7 replies
-
I think I ran into it myself and was rather annoyed – but this is really a Python typing question: is there a way to tell the type checker that an iterator yields at least one value? cc @Tinche 😇 |
Beta Was this translation helpful? Give feedback.
7 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I was intrigued by the iterator + context manager based retry API offered by stamina (great library btw: love the docs, the overall APIs, the types-first approach 👍🏻), but after sprinkling it on some code I was playing with, I noticed this pattern:
But then type checkers rightfully complain on the last
print(foo)
call thatfoo
might be unbound. After all, nothing in the types guarantee that the async generator yields anything.So I have a few choices:
foo
with some default valueprint
statement (any any further code) into the context manager scopefetch_foo
specifically for this callsite and use thestamina
's decorator APINone of these are the end of the world, but it made me wonder if I'd ever use the iterator-based API again :) Has anyone else bumped into this? Is there a way to signal to a type checker that an iterator will definitely yield at least once?
Beta Was this translation helpful? Give feedback.
All reactions