python / python/mypy

False positive error "incompatible type"

Open
#13,566 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-inheritance
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

For this code:

from typing import Any, Generic, TypeVar

T = TypeVar('T')


class EtcdValueWatcherTyped(Generic[T]):
    def __init__(self):
        pass


class EtcdValueWatcher(EtcdValueWatcherTyped[dict[str, Any]]):
    pass


class SettingsWatcher(EtcdValueWatcher):
    def __init__(self, arg1: str):
        self.__arg1 = arg1
        EtcdValueWatcher.__init__(self)


def main():
    watcher = SettingsWatcher('/v1/qwe')

mypy outputs error:

test.py: note: In member "__init__" of class "SettingsWatcher":
test.py:19: error: Argument 1 to "__init__" has incompatible type "SettingsWatcher"; expected "EtcdValueWatcherTyped[T]"  [arg-type]
            EtcdValueWatcher.__init__(self)
                                      ^
Found 1 error in 1 file (checked 1 source file)

which is false positive.

  • Mypy version used: mypy 0.971 (compiled: yes)
  • Mypy command-line flags: mypy --config-file ./mypy.ini ./test.py
  • Mypy configuration options from mypy.ini (and other config files):
[mypy]
ignore_missing_imports = True
show_error_codes = True
show_error_context = True
pretty = True
no_site_packages = False
  • Python version used: Python 3.10.6
  • Operating system and version: Fedora 35

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

Run the supplied Python 3.10 reproducer with mypy 0.971 and confirm the false-positive [arg-type] error on the explicit EtcdValueWatcher.init call. Investigate generic inheritance and constructor checking in the type checker, then add a regression test based on this example. Done means the valid call produces no incompatible-type error.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.