Skip to content

Modified __add__ method in tuple class #3252

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

Merged
merged 3 commits into from
Sep 30, 2019

Conversation

erictraut
Copy link
Contributor

Modified add method in tuple class to allow it to accept tuples with different generic parameter types. This allows, for example:
a = (1, )
b = a + (2.4, )

…ith different generic parameter types. This allows, for example:

a = (1, )
b = a + (2.4, )
@@ -867,7 +867,7 @@ class tuple(Sequence[_T_co], Generic[_T_co]):
def __le__(self, x: Tuple[_T_co, ...]) -> bool: ...
def __gt__(self, x: Tuple[_T_co, ...]) -> bool: ...
def __ge__(self, x: Tuple[_T_co, ...]) -> bool: ...
def __add__(self, x: Tuple[_T_co, ...]) -> Tuple[_T_co, ...]: ...
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would make more sense to use a second type variable here und return a tuple that is an union of both. I have not checked, but the current version should just return a Tuple[int, ...] for your example.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm unclear on what you mean by "union of both". Are you proposing something like this?

def __add__(self, x: Tuple[_T2_co, ...]) -> Tuple[Union[_T_co, _T2_co], ...]: ...

That doesn't look right to me. Maybe I'm misunderstanding.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is what I meant, but it might not be the right solution. Let's think this through: typing basically supports two kinds of tuples: "immutable lists" (PEP 484 calls them "arbitrary-length homogeneous tuples", but that's a mouthful), e.g. Tuple[T, ...] and "real tuples", e.g. Tuple[int, str, float]. The current stub only supports the former case, while your example would need the latter.

I don't think we have any way to support the latter case and keep type safety. Tuple[...] is not supported syntax according to PEP 484 and mypy complains about it. It's basically a one-element tuple, but ... is not a valid type.

My suggestion above is not ideal, because it will allow any type at any position and would mean the user needed to add type check when accessing them. E.g. x[0] in x = ("",) + (3,) would be Union[str, int] when it is obviously str.

The best I can think of is to add an overload like def __add__(self, x: tuple) -> tuple: ..., which will not keep any element types, but at least does accept your example. All elements of the resulting tuple would be Any.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation. Yeah, since there's currently no way to represent the concatenation of two homogeneous tuples, the best we can do is to fall back on an untyped tuple. I suppose we could consider ways to augment the standard (e.g. a spread/unpack operator that can be used within type arg lists), but that's a longer discussion.

@srittau
Copy link
Collaborator

srittau commented Sep 24, 2019

Maybe it would also make sense to just special-case int/float tuples.

Copy link
Collaborator

@srittau srittau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but I'd like to hear the opinion of @JelleZijlstra or someone else from the mypy team.

Copy link
Member

@ilevkivskyi ilevkivskyi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM (but you will need to copy the change to __builtins__.pyi). Hopefully @JelleZijlstra will have time to get this through the sync.

@erictraut
Copy link
Contributor Author

I'm not sure what you mean by "copy the change to __builtins__.pyi. Is that something I need to do before this PR is approved?

@gvanrossum
Copy link
Member

gvanrossum commented Sep 29, 2019 via email

@erictraut
Copy link
Contributor Author

Got it — thanks for the explanation. I've updated __builtin__.pyi accordingly.

@erictraut
Copy link
Contributor Author

Looks like the mypy tests need to be tweaked for this change. I'm not sure how you normally coordinate dependent changes between the mypy and typeshed repos. Could someone familiar with the process help resolve?

@gvanrossum
Copy link
Member

Yeah, this happens occasionally, and it's a pain. :-)

IIRC you have to prepare a PR for mypy that adjusts the tests there, link the two tests to each other via cross-references in GitHub, and then someone with commit privileges in both repos will merge both, ignoring the test failures. After landing both I believe it will also be necessary to do a third PR, a so-called "typeshed sync" where we move the commit hash of the typeshed submodule in the mypy repo.

@ilevkivskyi
Copy link
Member

I can do this tomorrow morning if no one else will do it before.

@JelleZijlstra
Copy link
Member

I can do it shortly (together with the other mypy/typeshed sync I promised to do).

@ilevkivskyi
Copy link
Member

I can do it shortly (together with the other mypy/typeshed sync I promised to do).

Great, thank you! 👍

JelleZijlstra added a commit to JelleZijlstra/mypy that referenced this pull request Sep 30, 2019
Needed for python/typeshed#3252.

Tested by pulling in the typeshed branch in the subproject.
@JelleZijlstra
Copy link
Member

Just to summarize for myself, I need to merge the following:

A few of these still have CI running, so I'll wait for those to finish in case something unexpected is wrong. Afterwards, I'll merge all of the open PRs, which will break both mypy and typeshed CI, and then do a typeshed sync in mypy to get things back to green.

@JelleZijlstra JelleZijlstra merged commit 468d13a into python:master Sep 30, 2019
JelleZijlstra added a commit to python/mypy that referenced this pull request Sep 30, 2019
Needed for python/typeshed#3252.

Tested by pulling in the typeshed branch in the subproject.
SergeyZh pushed a commit to JetBrains/intellij-community that referenced this pull request Oct 14, 2019
Excluding python/typeshed#3252

GitOrigin-RevId: f6d802b1a50d6071f1a1027017750e4dc65ece26
SergeyZh pushed a commit to JetBrains/intellij-community that referenced this pull request Jan 10, 2020
Excluding python/typeshed#3252

GitOrigin-RevId: 705c358d0112ca475be82659745a1190dd5410d3
SergeyZh pushed a commit to JetBrains/intellij-community that referenced this pull request Jan 30, 2020
Excluding python/typeshed#3252

GitOrigin-RevId: bd535fbff565aef2ba42fd99a7d4a5c7f5c770a8
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

Successfully merging this pull request may close these issues.

6 participants