classification
Title: New NamedTuple syntax silently ignores method definitions
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.6
process
Status: closed Resolution: duplicate
Dependencies: Superseder:
Assigned To: Nosy List: elazar, gvanrossum, levkivskyi, rhettinger
Priority: normal Keywords:

Created on 2017-01-24 07:14 by elazar, last changed 2017-01-24 08:41 by levkivskyi. This issue is now closed.

Messages (2)
msg286147 - (view) Author: Elazar Gershuni (elazar) * Date: 2017-01-24 07:14
The following does not work as expected:
```
from typing import NamedTuple

class A(NamedTuple):
    a: int

    def __repr__(self):
        return 'some A'

    def spam(self):
        print('spam!')

>>> a = A(5)
>>> repr(a)  # should be 'some A'
'A(a=5)'
>>> a.spam()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'A' object has no attribute 'spam'
```
msg286153 - (view) Author: Ivan Levkivskyi (levkivskyi) * (Python committer) Date: 2017-01-24 08:41
This has been already reported in https://github.com/python/typing/issues/352 and fixed in https://hg.python.org/cpython/rev/f100619e7137 and https://github.com/python/typing/pull/364

Now adding new methods works but overwriting existing special attributes raises AttributeError:

class A(NamedTuple):
    x: int
    def spam(self):  # this works
        ...
    def _fields(self):  # this is an error (and also for __repr__ etc)

If you think that overwriting all special attributes should be allowed (or only some of them) then we could discuss this at python/typing tracker.
History
Date User Action Args
2017-01-24 08:41:54levkivskyisetstatus: open -> closed

nosy: + levkivskyi
messages: + msg286153

resolution: duplicate
stage: resolved
2017-01-24 07:14:54elazarcreate