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

Annotating namedtuples item types #21

Closed
JukkaL opened this issue Oct 25, 2014 · 14 comments
Closed

Annotating namedtuples item types #21

JukkaL opened this issue Oct 25, 2014 · 14 comments

Comments

@JukkaL
Copy link
Contributor

JukkaL commented Oct 25, 2014

There should be a way of annotating the item types of named tuples. Here is my suggested syntax:

X = namedtuple('X', ['a', 'b']) # type: (int, str)

The type of a would be int and the type of b would be str.

@mvitousek
Copy link

This reads to me more like an annotation for a normal tuple -- I would expect that something with type (int, str) would be a tuple, not a named tuple, and I would expect that any assignment with a # type: on it really does mean that the assignee has the type specified. What about having typing provide a typednamedtuple (or similar) that takes the expected types as part of its parameters, e.g.

X = typednamedtuple('X', {'a':int, 'b':int})

This would also help out tools like jython and reticulated that currently are unequipped to parse comment typings.

@gvanrossum
Copy link
Member

The problem is that dicts are unordered, while namedtuple requires the
fields to be ordered.

On Thu, Nov 6, 2014 at 4:14 PM, Michael Vitousek notifications@github.com
wrote:

This reads to me more like an annotation for a normal tuple -- I would
expect that something with type (int, str) would be a tuple, not a named
tuple, and I would expect that any assignment with a # type: on it really
does mean that the assignee has the type specified. What about having
typing provide a typednamedtuple (or similar) that takes the expected
types as part of its parameters, e.g.

X = typednamedtuple('X', {'a':int, 'b':int})

This would also help out tools like jython and reticulated that currently
are unequipped to parse comment typings.


Reply to this email directly or view it on GitHub
#21 (comment).

--Guido van Rossum (python.org/~guido)

@mvitousek
Copy link

Ah, right, of course. The same principle could still work with

X = typednamedtuple('X', ['a', 'b'], [int, str])

perhaps.

@JukkaL
Copy link
Contributor Author

JukkaL commented Nov 9, 2014

Maybe we should call it NamedTuple, similar to List vs. list?

Another syntax idea:

X = NamedTuple('X', [('a', int), ('b', str)])

This syntax is arguably more readable if the tuple has many items, as it's obvious which types map to which items.

@gvanrossum
Copy link
Member

+1 on both points.
On Nov 8, 2014 6:13 PM, "Jukka Lehtosalo" notifications@github.com wrote:

Maybe we should call it NamedTuple, similar to List vs. list?

Another syntax idea:

X = NamedTuple('X', [('a', int), ('b', str)])

This syntax is arguably more readable if the tuple has many items, as it's
obvious which types map to which items.


Reply to this email directly or view it on GitHub
#21 (comment).

@JukkaL
Copy link
Contributor Author

JukkaL commented Dec 1, 2014

I've started implementing my last idea (NamedTuple) for mypy. I don't have much experience with it yet, but it looks pretty reasonable so far.

@ambv
Copy link
Contributor

ambv commented Jan 7, 2015

Typing namedtuples is a good idea.
X = NamedTuple('X', [('a', int), ('b', str)]) is the obvious solution here but the syntax is punctuation heavy. Be sure to support the alternative namedtuple syntax that was X = namedtuple('x', 'a b'). Then you can just do X = NamedTuple('X', 'a:int, b:str').

@gvanrossum
Copy link
Member

Jukka has just implemented this for mypy. See
https://github.com/JukkaL/typing/issues/7#issuecomment-68984316 .

(Hm, we have too many repos with issue trackers where we have these
discussions. Let's try to move discussions about what things should look
like to the typehinting repo.)

On Tue, Jan 6, 2015 at 11:58 PM, Łukasz Langa notifications@github.com
wrote:

Typing namedtuples is a good idea.
X = NamedTuple('X', [('a', int), ('b', str)]) is the obvious solution
here but the syntax is punctuation heavy. Be sure to support the
alternative namedtuple syntax that was X = namedtuple('x', 'a b'). Then
you can just do X = NamedTuple('X', 'a:int, b:str').


Reply to this email directly or view it on GitHub
#21 (comment).

--Guido van Rossum (python.org/~guido)

@ambv
Copy link
Contributor

ambv commented Jan 8, 2015

Agreed to keep discussions in one place. Context-switching between the repos slows us down.

@gvanrossum
Copy link
Member

This is also implemented in my typing.py.

@refi64
Copy link

refi64 commented Mar 26, 2015

I'm late to this discussion...but, wasn't there recently a discussion on making dicts in CPython ordered by default like PyPy does? If that were to happen, could the the dict-based syntax for namedtuples be supported?

@gvanrossum
Copy link
Member

Yes, but that's a big if -- I don't know who's actually working on that. If dicts become ordered we can support that -- but even so, I'd like to support the list-of-tuples form for compatibility with Python 3.2-3.4.

@gvanrossum
Copy link
Member

Closing, it's now also described in the PEP.

@gvanrossum gvanrossum reopened this Mar 26, 2015
@gvanrossum
Copy link
Member

Oops.

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

5 participants