python / python/mypy

Method argument errors refer to method's class, not callee's

Open
#14,896 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-error-reporting topic-newtype topic-usability
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

from typing import NewType
from typing_extensions import Self


class C:
    def foo(self: Self, other: Self) -> int: raise NotImplementedError


D = NewType('D', C)

d = D(C())
d.foo(42)  # E:  Argument 1 to "foo" of "C" has incompatible type "int"; expected "D"

This is pretty minor, but "foo" of "C" here feels misleading. Would it be clearer to name the instance's type?

Playground: https://mypy-play.net/?mypy=latest&python=3.11&gist=9b6fe04dddab8ed91a41c819bff3854a

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.

Research direction

Use the linked mypy-play reproducer with the NewType, Self, and d.foo(42) example as the starting point. Trace the diagnostic generation for the incompatible argument and confirm that the completed message identifies the callee's instance type rather than the method's class.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.