Skip to content
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

Added draft chapter to typing spec for named tuples #1614

Merged
merged 5 commits into from
Feb 11, 2024

Conversation

erictraut
Copy link
Collaborator

No description provided.

@erictraut erictraut marked this pull request as draft January 30, 2024 04:03
Copy link
Member

@JelleZijlstra JelleZijlstra left a comment

Choose a reason for hiding this comment

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

Looks good, a few editorial issues

Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

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

Question: should Unpack[] be mentioned here?

@erictraut
Copy link
Collaborator Author

@sobolevn, you asked about Unpack[]. Can you elaborate on that? What do you have in mind?

@sobolevn
Copy link
Member

sobolevn commented Feb 2, 2024

@erictraut typing.py mentions that you can unpack tuples: https://github.com/python/cpython/blob/1aec0644447e69e981d582449849761b23702ec8/Lib/typing.py#L1587-L1597

And since namedtuple is compatible with tuple, I think that it should be explicit that you cannot unpack namedtuples. Or can you?

One more related thing: https://peps.python.org/pep-0646/ does not mention Unpack[tuple] at all 🤔

@erictraut
Copy link
Collaborator Author

@sobolevn, "unpacking a tuple" is different from using the special form Unpack. The Unpack special form can be used only in certain type annotation contexts and only with an unpacked TypeVarTuple or tuple according to PEP 646.

For example, this is allowed:

from typing import Unpack

def func[*Ts](x: tuple[int, Unpack[tuple[str, ...]]], *args: Unpack[Ts]) -> tuple[Unpack[Ts]]: ...

This is equivalent to:

def func[*Ts](x: tuple[int, *tuple[str, ...]], *args: *Ts) -> tuple[*Ts]: ...

The Unpack special form doesn't have anything to do with named tuples.

Named tuples, like regular tuples, can be unpacked using the unpack operator or through a destructuring assignment.

NT = namedtuple("NT", "x y z")

my_tuple = (1, 2, 3)
my_named_tuple = NT(1, 2, 3)

t1 = [*my_tuple]
t2 = [*my_named_tuple]

a, b, c, = my_tuple
x, y, z = my_named_tuple

@sobolevn
Copy link
Member

sobolevn commented Feb 2, 2024

@erictraut yes, this is exactly why I am asking.

A named tuple is a subtype of a tuple

You can unpack tuple, some people might think that if you can unpack a tuple, you might also unpack its subtypes, for example, a namedtuple: Unpack[NT].

Is it worth mentioning in the docs that you cannot do that, what do you think?

@erictraut
Copy link
Collaborator Author

As I said, "unpacking a tuple" isn't the same as using the Unpack special form in a type annotation. You seem to be confusing the two concepts. The Unpack special form cannot be used with anything other than a TypeVarTuple or a tuple as explained in PEP 646 (and in the TypeVarTuple and tuple chapters in the typing spec). I think it would be odd for the "named tuple" chapter to say anything about the Unpack special form.

@erictraut erictraut marked this pull request as ready for review February 11, 2024 03:02
@erictraut erictraut merged commit 12eb6c6 into python:main Feb 11, 2024
4 checks passed
@erictraut erictraut deleted the namedtuples_spec branch February 11, 2024 03:02
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.

7 participants