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

NamedTuple doesn't accept field called count #3950

Closed
Daenyth opened this issue Sep 12, 2017 · 3 comments
Closed

NamedTuple doesn't accept field called count #3950

Daenyth opened this issue Sep 12, 2017 · 3 comments

Comments

@Daenyth
Copy link

Daenyth commented Sep 12, 2017

from typing import *
class Foo(NamedTuple):
    count: int
foo.py:3: error: Incompatible types in assignment (expression has type "int", base class "tuple" defined the type as Callable[[Tuple[Any, ...], Any], int])
@ilevkivskyi
Copy link
Member

This is not a bug, tuple has a method named count and here you are trying to override it in an incompatible way, so this is a Liskov violation.

@PeterJCLaw
Copy link
Contributor

PeterJCLaw commented May 10, 2021

Just hit this and couldn't work out what the underlying issue was. I looked in the docs for both typing.NamedTuple and collections.namedtuple and neither mentions that having a field called count isn't supported. Found this issue on my way to report a bug.

There are a some other things which conspire to make this harder to understand:
Firstly the older spelling is accepted by mypy:

from typing import NamedTuple
Foo = NamedTuple('Foo', (
    ('count', int),
))

Secondly, in either spelling the type according to reveal_type(Foo(9).count) is builtins.int.

Finally the code does work and seems to behave as expected.

I appreciate that this may not be something which mypy can allow, however if that is the case please could it:

  • be consistent about the error between spellings
  • have a clearer error message

Regarding the clarity of this: while I can now (that I understand the issue) see what mypy was saying, I didn't initially understand that the assignment issue was to do with the assignment of the member count into the class. It felt like I was declaring a class member, which is conceptually different (even if it's actually an assignment).

Would an error message like the following be possible here (emphasis to show changes):

Incompatible types in class member declaration (expression has type "int", base class "tuple" defined the member type as "Callable[[Tuple[int, ...], Any], int]") [assignment]

Maybe even add something like:

Hint: are you overriding a method with an attribute?

If there's scope to change the error code from assignment to perhaps member-declaration (or something) that might also help? Not sure if that would be too breaking though.

For comparison, the method override error message is (to me) much clearer:

Signature of "count" incompatible with supertype "tuple" [override]

@PeterJCLaw
Copy link
Contributor

PeterJCLaw commented May 10, 2021

Ah, I see there's an open issue for the message: #4507
Cross linking -- #9043 seems to be a duplicate of this issue

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

3 participants