python / python/mypy

Argument incompatibility not detected with multiple generic base classes

Aperta
#6,184 10 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug priority-1-normal topic-inheritance
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

  • 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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia dal TODO in mypy/checker.py intorno alla riga 1634, quindi riproduci il problema usando l’esempio minimo con Foo, Bar e Baz presente in questo report. La correzione è completa quando mypy segnala l’argomento incompatibile per Baz indipendentemente dal fatto che Foo o Bar compaia per primo nell’elenco delle classi base.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.