Skip to content

NamedTuples defined via Python 3.6 variable annotations should allow methods #352

Closed
@roganov

Description

@roganov

Following code should work as expected

In [1]: from typing import NamedTuple

In [2]: class X(NamedTuple):
   ...:     x: int
   ...:     def double(self):
   ...:         return 2 * x
   ...:     

In [3]: X(1).double()

At the moment it throws AttributeError as there is no double method on the constructed namedtuple.

The workaround is to define methods on subclass and set __slots__ = (). But unless I'm missing something, there is no reason why methods shouldn't work as expected.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions