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

Better error message for incompatible assignment to method #4507

Open
anxolerd opened this issue Jan 24, 2018 · 4 comments
Open

Better error message for incompatible assignment to method #4507

anxolerd opened this issue Jan 24, 2018 · 4 comments

Comments

@anxolerd
Copy link

Note

I am not sure whether it should be reported here or to the typeshed repo. Reporting here for now. If this is a typeshed issue, I'll gladly open issue there and close this one.

If there is something special in index field in NamedTuple, please point out to the corresponding documentation as I failed to find anything on topic.

Description

Type

bug

Code to check

from typing import NamedTuple


class Foo(NamedTuple):
    index: str

Actual behavior

test.py:5: error: Incompatible types in assignment (expression has type "str", base class "tuple" defined the type as "Callable[[Tuple[str, ...], Any, int, int], int]")

Expected behavior

No errors while typechecking

Environment

$ mypy -V
mypy 0.560
$ python -V
Python 3.6.4

Behavior is still the same when using mypy from master:

$ mypy -V
mypy 0.570-dev-7192a7578fc47530bbb5382984d289bffa72b30b
$ mypy test.py
test.py:5: error: Incompatible types in assignment (expression has type "str", base class "tuple" defined the type as "Callable[[Tuple[str, ...], Any, int, int], int]")
@anxolerd
Copy link
Author

Well, as my colleagues just pointed, NamedTuple is a descendant of tuple class which has instance-method index:

In [1]: dir(tuple())
Out[1]:
['__add__',
 ...
 '__subclasshook__',
 'count',
 'index']

Closing this as it is not a bug. Thanks to @vmagamedov for explanations.

@gvanrossum
Copy link
Member

gvanrossum commented Jan 24, 2018 via email

@ethanhs
Copy link
Collaborator

ethanhs commented Jan 24, 2018

Agreed. Callable types in base classes should probably have their own special clearer message.

@ethanhs ethanhs reopened this Jan 24, 2018
@ethanhs ethanhs changed the title Weird typing of NamedTuple index field Better error message for incompatible assignment to method Jan 24, 2018
pdarragh added a commit to pdarragh/sweetpea-py that referenced this issue Mar 24, 2021
According to python/mypy#4507, the issue is that `NamedTuple` inherits from
`tuple`, and `tuple` defines a base method `tuple.count`. This causes a problem
for mypy, and currently the best solution is to just pick a different name.
@PeterJCLaw
Copy link
Contributor

(Cross posted from #3950 (comment))

My experience was that I didn't initially understand that the assignment issue was to do with the assignment of the member (in my case count) into the class. It felt like I was declaring a class member, which feels conceptually different (even if it's technically 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]

candleindark added a commit to candleindark/datalad-registry that referenced this issue Apr 25, 2023
mypy doesn't seem to work well with `NamedTuple`.
For more info, checkout
python/mypy#9043
python/mypy#4507
tianyizheng02 added a commit to pittcsc/PittAPI that referenced this issue Aug 18, 2024
Add None to some type annotations in gym.py in order to fix some mypy
errors.

Rename some fields in the Gym NamedTuple class in order to clarify their
meanings and to fix a mypy error. The "count" field specifically causes
a mypy error because mypy interprets the field's name as the "count"
method that NamedTuple inherits from the base tuple class. According to
the mypy devs, this is expected behavior and is not a bug:
- python/mypy#4507
- python/mypy#17047
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants