-
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
Improve overloads and return types for iterparse.__new__
#19
Comments
I still have some fresh memory having to make an abridged And yes, tagged union can be useful here, I'm sort of dumbfound it didn't come up while writing annotations. Still taking a few days off computer, when I'm back I'll look into it and make a better compromise between the extremes. |
@Daverball Finally fully remembered what happened when I tried implementing the stubs. It doesn't work as you had hoped. for event, value in iterator: At this line, the types of I would incorporate the change you suggested, but even with that, you need to write code in not-so-pythonic way: for item in iterator:
if item[0] == 'start-ns':
reveal_type(item[1])
elif item[0] == 'end-ns':
reveal_type(item[1])
|
@abelcheung You are correct, it is still an overall improvement though. We can profit from the additional overload, even if we unpack the tuple, before any type narrowing can occur. |
If we only pass in events that all have the same layout i.e.
_NoNSEventNames
, then we know that the resulting iterator always yields the event name as the first tuple element and an_Element
as the second one. Consider the following example which gives a broad type union that is annoying to deal withAdding a new overload for
_NoNSEventNames
which returnsiterparser[tuple[_NoNSEventNames, _Element]]
should fix this, but this only solves part of the issue.It should also be possible to improve things further by using a tagged union, rather than a tuple of unrelated unions, since we know the object that belongs to every event, i.e. rather than doing
tuple[_EventUnion, _ValueUnion]
you would do:With the tagged union type checkers can perform type narrowing on the value type when doing a simple string comparison on the event, so this would allow for the following:
The text was updated successfully, but these errors were encountered: