python / python/mypy

Argument incompatibility not detected with multiple generic base classes

Open
#6,184 10 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

  • Are you reporting a bug, or opening a feature request?

Reporting a bug

  • Please insert below the code you are checking with mypy,
    or a mock-up repro if the source is private. We would appreciate
    if you try to simplify your case to a minimal repro.

Minimal repro:

from abc import ABCMeta, abstractmethod
from typing import Generic, TypeVar, Union

A = TypeVar('A')


class Base(Generic[A]):
    __metaclass__ = ABCMeta

    @abstractmethod
    def do_stuff(self, a):
        # type: (A) -> None
        pass


class Foo(Generic[A], Base[A]):
    pass


class Bar(Generic[A], Base[A]):
    pass


class Baz(Foo[str], Bar[Union[str, int]]):
    def do_stuff(self, a):
        # type: (str) -> None
        return None
  • What is the actual behavior/output?

No error is reported

  • What is the behavior/output you expect?

The definition of Baz.do_stuff() accepts a str, which is incompatible with Bar.do_stuff()'s abstract method definition (which accepts Union[str, int]).

This bug appears to be related to the following TODO comment: https://github.com/python/mypy/blob/d1c8f27f696012085ca904e96f8b93b92b74919e/mypy/checker.py#L1634

Interestingly, if I reverse the order of Foo and Bar in Baz's base class list, mypy reports the error.

  • What are the versions of mypy and Python you are using?
    Do you see the same issue after installing mypy from Git master?

https://github.com/python/mypy/tree/103f5f3ec3fcc8fba429edcc4ee61c1370153ff9

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

Start with the TODO in mypy/checker.py around line 1634, then reproduce the issue using the minimal Foo, Bar, and Baz example in this report. The fix is complete when mypy reports the incompatible argument for Baz regardless of whether Foo or Bar appears first in the base class list.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.