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 |
| 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.
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.
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.
I've decided not to, because this can be quite complex / slow with no real users asking for it.
Co-authored-by: Ilya Priven <ilya.konstantinov@gmail.com>
|
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_typesforgot about the second way we representtupleinside.Closes #15443