You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I just pushed an experimental, partial implementation of namedtuple support.
This is supported:
fromcollectionsimportnamedtupleX=namedtuple('X', ['a', 'b'])
x=X(1, 'x') # Okx=X(1, c='x') # Errorx.c# Errorx.a+ [1] # No error, since a has implicit type AnyclassY(X):
"""you can use a namedtuple class as a base class"""
Also, you can define the types of named tuple items using typing.NamedTuple:
A bunch of stuff is still broken or unsupported, but this should be good enough for (most) library stubs that need namedtuple. I'm going to continue working on this.
I don't see NamedTuple defined in the https://github.com/JukkaL/typing project yet. mypy's own typing.py seems to have diverged (e.g. it has Callable, mypy still has Function, but mypy has NamedTuple).
Activity
rockneurotiko commentedon Sep 16, 2014
It's not implemented yet.
Code
[-]Fails with collections.namedtuple[/-][+]Add support for collections.namedtuple[/+]akaihola commentedon Oct 10, 2014
I now noticed that some work-arounds are mentioned in the "Common issues" section of the "Creating stubs for Python modules" wiki page.
JukkaL commentedon Nov 8, 2014
The most primitive support would allow basic type checking with implicit
Anyitem types:We also need to support
namedtupleas a base class:The above features would allow running mypy on code that uses named tuples and perform rudimentary type checking.
The next step (potentially as a separate issue) would be to allow specifying item types. Here is a potential syntax:
Coming up with a syntax for the base class case is more difficult. Suggestions are welcome, or we can require the code to be rewritten like this:
JukkaL commentedon Nov 30, 2014
I'm working on this.
JukkaL commentedon Dec 1, 2014
I just pushed an experimental, partial implementation of
namedtuplesupport.This is supported:
Also, you can define the types of named tuple items using
typing.NamedTuple:A bunch of stuff is still broken or unsupported, but this should be good enough for (most) library stubs that need
namedtuple. I'm going to continue working on this.JukkaL commentedon Dec 18, 2014
This is now implemented. It's not quite perfect yet, but it should be usable and I'll file issues for the remaining limitations (or just fix them).
Now you can also use named tuples in the base class list:
gvanrossum commentedon Jan 7, 2015
I don't see NamedTuple defined in the https://github.com/JukkaL/typing project yet. mypy's own typing.py seems to have diverged (e.g. it has Callable, mypy still has Function, but mypy has NamedTuple).
JukkaL commentedon May 12, 2015
Yeah, the
namedtupleimplementation is missing a few things. Added a new issue: #661