Confusing error when assigning to method defined in base class

Open
#2,800 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

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

Research direction

Start by reproducing both examples and tracing the assignment check for a method inherited from a base class. Compare it with normal override checking, then verify that compatible assignments pass and that any remaining argument-name diagnostic includes Arg('name', ...).

Written by the indexing model from the issue text.

Description

bug false-positive priority-1-normal

The error on the last line of this program doesn't make sense, since the types seems compatible:

class C:
    def f(self, x: int) -> None: pass
class D(C):
    def g(self, y: object) -> None: pass
    f = g  # Incompatible types in assignment (expression has type Callable[[object], None], 
           # base class "C" defined the type as Callable[[int], None])

There are actually two issues going on. If we update the code a little, the error message looks more reasonable:

class C:
    def f(self, x: int) -> None: pass
class D(C):
    def g(self, y: int) -> None: pass
    f = g  # Incompatible types in assignment (expression has type Callable[[Arg('y', int)], None], 
           # base class "C" defined the type as Callable[[Arg('x', int)], None])

Here's my analysis:

  • Both the examples should probably be okay. We shouldn't do strict callable argument name checking here, for consistency with normal override checks. If we'd use a normal override (def f(...)), there would be no error.
  • The error message in the first example is not good. We should include Arg('name', ...) in the message if the error is due to an argument name mismatch.
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.