Skip to content

Add support for collections.namedtuple #460

Closed

Description

@akaihola

namedtupletest.py:

from collections import namedtuple

Test:

$ mypy namedtupletest.py
/tmp/namedtupletest.py, line 1: Module has no attribute 'namedtuple'

Activity

  1. rockneurotiko commented on Sep 16, 2014

    @rockneurotiko
    Contributor

    It's not implemented yet.
    Code

  2. changed the title [-]Fails with collections.namedtuple[/-] [+]Add support for collections.namedtuple[/+] on Sep 22, 2014
  3. akaihola commented on Oct 10, 2014

    @akaihola
    Author

    I now noticed that some work-arounds are mentioned in the "Common issues" section of the "Creating stubs for Python modules" wiki page.

  4. JukkaL commented on Nov 8, 2014

    @JukkaL
    Collaborator

    The most primitive support would allow basic type checking with implicit Any item types:

    X = namedtuple('X', ['a', 'b'])
    x = X(1, 'x')  # Ok
    x.c  # Type error
    x.a + [1]  # No error, since a has implicit type Any

    We also need to support namedtuple as a base class:

    class X(namedtuple('X', ['a', 'b'])):
        ...

    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:

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

    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:

    X = namedtuple('X', ['a', 'b'])  # type: (int, str)
    class X(X):
        ...
  5. JukkaL commented on Nov 30, 2014

    @JukkaL
    Collaborator

    I'm working on this.

  6. JukkaL commented on Dec 1, 2014

    @JukkaL
    Collaborator

    I just pushed an experimental, partial implementation of namedtuple support.

    This is supported:

    from collections import namedtuple
    X = namedtuple('X', ['a', 'b'])
    x = X(1, 'x')  # Ok
    x = X(1, c='x')  # Error
    x.c  # Error
    x.a + [1]  # No error, since a has implicit type Any
    class Y(X): 
        """you can use a namedtuple class as a base class"""

    Also, you can define the types of named tuple items using typing.NamedTuple:

    from typing import NamedTuple
    X = NamedTuple('X', [('a', int), ('b', str)])
    x = X(1, 'x')  # Ok
    x = X(1, 2)  # Error

    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.

  7. JukkaL commented on Dec 18, 2014

    @JukkaL
    Collaborator

    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:

    from typing import NamedTuple
    class Foo(NamedTuple('Bar', [('item1', int), ('item2', str)])):    # OK!
        ...
  8. gvanrossum commented on Jan 7, 2015

    @gvanrossum
    Member

    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).

  9. JukkaL commented on May 12, 2015

    @JukkaL
    Collaborator

    Yeah, the namedtuple implementation is missing a few things. Added a new issue: #661

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions