Skip to content

Support concatenation between lists of different types #5756

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

Closed
ktbarrett opened this issue Jul 10, 2021 · 5 comments
Closed

Support concatenation between lists of different types #5756

ktbarrett opened this issue Jul 10, 2021 · 5 comments

Comments

@ktbarrett
Copy link
Contributor

In Python adding a list of strings and a list of ints results in a list of strings and ints, but currently this is not supported by typing.

a = list[int]()
b = list[str]()
reveal_type(a + b)  # error: Unsupported operand types for + ("List[int]" and "List[str]")

Instead, we could provide an overload such that the result of the operation becomes List[Union[int, str]].
This also has the nice effect where Union[str, str_subtype] can be reduced to str, so it works to implement automatic promotion as well.

@JelleZijlstra
Copy link
Member

See https://github.com/python/typeshed/pulls?q=is%3Apr++list+__add__+ for previous attempts in this area. There doesn't seem to be an issue about it yet though.

@Akuli
Copy link
Collaborator

Akuli commented Jul 10, 2021

When working with programs that use subprocess.call, this is the most annoying thing about typing in Python to me. But as you can hopefully see by the previous attempts here and in mypy, trying to fix this tends to break things in weird ways.

@ktbarrett
Copy link
Contributor Author

Just to be clear, the issue is with mypy's treatment of list and tuple literals where it isn't using typeshed's definition of list.__add__ and tuple.__add__ when handling literal expressions? For the fix to be made here, a similar and simultaneous fix to mypy would have to occur?

@JelleZijlstra
Copy link
Member

I'm not sure anyone figured out exactly what's going on.

@JelleZijlstra
Copy link
Member

Duplicate of #2383

@JelleZijlstra JelleZijlstra marked this as a duplicate of #2383 Jan 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants