Better error message for incompatible assignment to method

Open
#4,507 4 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
25/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
python
Domain
tooling

Research direction

Reproduce the reported NamedTuple example with mypy and inspect how the index field is checked against tuple's inherited index method. Determine whether the fix belongs in mypy or typeshed; done means the sample type-checks without errors and the regression is covered by an appropriate test.

Written by the indexing model from the issue text.

Description

feature priority-1-normal topic-usability

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]")
Dominant language
Python
Stars
20.6k
Forks
3.3k
Avg merge
1d 18h
Merged PRs (30d)
54

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from python/mypy

All issues in python/mypy

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.