-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Correctly narrow types for tuple[type[X], ...]
#15691
Conversation
This comment has been minimized.
This comment has been minimized.
|
This comment has been minimized.
This comment has been minimized.
Analyzing primer output: the only idea that I have right now is that previosly it was |
@@ -7118,6 +7118,8 @@ def flatten_types(t: Type) -> list[Type]: | |||
t = get_proper_type(t) | |||
if isinstance(t, TupleType): | |||
return [b for a in t.items for b in flatten_types(a)] | |||
elif is_named_instance(t, "builtins.tuple"): | |||
return [t.args[0]] |
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.
Why is it correct to use all args in former case but just first arg in second case?
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.
Because tuple
has only one arg. FWIW the only real question here is whether we need to recurse, i.e. also support:
tuple[tuple[type[A], type[B]], ...]
tuple[tuple[type[A], ...], ...]
But TBH I don't really care, because it would be a corner case to already rare special case.
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 decided not to, because this can be quite complex / slow with no real users asking for it.
Co-authored-by: Ilya Priven <[email protected]>
Thanks @ilevkivskyi and @ikonst for the review :) |
Diff from mypy_primer, showing the effect of this PR on open source code: pandas (https://github.com/pandas-dev/pandas)
+ pandas/core/arrays/datetimelike.py:595: error: Argument 1 to "Timestamp" has incompatible type "object"; expected "integer[Any] | float | str | date | datetime | datetime64" [arg-type]
|
flatten_types
forgot about the second way we representtuple
inside.Closes #15443