python / python/mypy

Method overloads ignored when dispatched via protocols

Offen
#9,464 3 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug topic-overloads topic-protocols
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Bug Report

At the moment I'm dealing with a number of numpy functions which, effectively, serve as wrappers
around a set of matching ndarray methods. Taking a simplified version of argmax as example,
a custom SupportsArgmax Protocol is herein used for dispatching the internals of the argmax
function to the ndarray.argmax method.

As it turns out, this works perfectly fine when the ndarray.argmax mods does not contain any
overloads, but when it does then any overloads beyond the first are ignored.

from typing import Protocol, overload, TypeVar

# Step 1: Define the class
class ndarray:
    @overload
    def argmax(self, axis: None) -> int: ...
    @overload
    def argmax(self, axis: int) -> ndarray: ...


# Step 2: Define a Protocol for describing the classes' argmax method
T_contra = TypeVar("T_contra", contravariant=True)
T_co = TypeVar("T_co", covariant=True)

class SupportsArgmax(Protocol[T_contra, T_co]):
    def argmax(self, axis: T_contra) -> T_co: ...


# Step 3: Define a wrapper function around the classes' argmax method
T1 = TypeVar("T1")
T2 = TypeVar("T2")

def argmax(a: SupportsArgmax[T1, T2], axis: T1) -> T2: ...

Now, when an ndarray instance is passed to the argmax function I'd expect the
return type to be int if axis=None and ndarray if axis=int(...), just as is
described in the ndarray.argmax method:

array: ndarray

# note: Revealed type is 'builtins.int*'
reveal_type(argmax(array, axis=None))

# error: Cannot infer type argument 1 of "argmax"
# note: Revealed type is 'Any'
reveal_type(argmax(array, axis=0))

As can be seen above, the expected behavior is indeed observed when passing
through the first ndarray.argmax overload, but the presence of any subsequent
overloads seems to be ignored. Swapping the two overloads in the original
ndarray.argmax method only serves to reverse the issue.

Expected Behavior

Mypy successfully infers the return type, regardless of whether the protocol should
extract it from the methods' first overload or any subsequent ones.

Actual Behavior

Mypy fails for any method overload overload beyond the first.

Your Environment

  • Mypy version used: mypy 0.782
  • Mypy command-line flags: --show-error-codes (including or excluding --strict makes no difference here)
  • Mypy configuration options from mypy.ini (and other config files): n.a.
  • Python version used: Python 3.8.5
  • Operating system and version: macOS 10.15.6

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

Beginne damit, das minimale Protocol- und überladene ndarray.argmax-Beispiel aus dem Issue mit mypy zu reproduzieren, und überprüfe beide reveal_type-Aufrufe sowie den gemeldeten Inferenzfehler. Verfolge den Typprüfpfad für die Kompatibilität von Protocol-Methoden und die Auswahl überladener Methoden. Als erledigt gilt die Aufgabe, wenn sowohl Aufrufe mit axis=None als auch mit einer ganzzahligen Achse die in den Overloads beschriebenen Rückgabetypen inferieren.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
devtools
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

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