python / python/mypy

Argument incompatibility not detected with multiple generic base classes

Offen
#6,184 10 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug priority-1-normal topic-inheritance
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

  • 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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginnen Sie mit dem TODO in mypy/checker.py ungefähr in Zeile 1634 und reproduzieren Sie das Problem anschließend mit dem minimalen Foo-, Bar- und Baz-Beispiel in diesem Bericht. Die Korrektur ist vollständig, wenn mypy das inkompatible Argument für Baz meldet, unabhängig davon, ob Foo oder Bar zuerst in der Liste der Basisklassen steht.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.