python / python/mypy

A `class MyCallable(Protocol[T]): def __call__(self) -> T: ...` behaves differently, and worse, than a `Callable[[], T]` for the purposes of type inference.

Offen
#18,016 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

Bug Report

See the error case reported when this is checked with mypy --strict

from typing import Callable, Protocol, TypeVar

_T = TypeVar("_T", covariant=True)


class _Adaptable(Protocol[_T]):
    def __call__(self) -> _T: ...


_AdaptableCB = Callable[[], _T]
_R = TypeVar("_R")


def adapt_proto(a: _Adaptable[_R]) -> _R:
    return a()


def adapt_callable(a: _AdaptableCB[_R]) -> _R:
    return a()


class Concrete: ...


def factory() -> Concrete:
    return Concrete()

from enum import Enum, auto
class Strategy(Enum):
    callable_factory = auto()
    callable_type = auto()
    proto_factory = auto()
    proto_type = auto()

def multi_adapt(how: Strategy) -> Concrete:
    match how:
        case Strategy.callable_factory:
            return adapt_callable(factory)
        case Strategy.callable_type:
            return adapt_callable(Concrete)
        case Strategy.proto_factory:
            return adapt_proto(factory)
        case Strategy.proto_type:
            return adapt_proto(Concrete)

(A clear and concise description of what the bug is.)

To Reproduce

# Ideally, a small sample program that demonstrates the problem.
# Or even better, a reproducible playground link https://mypy-play.net/ (use the "Gist" button)

Expected Behavior

I would expect these to be identical, and the Protocol option not to degrade to Any.

Actual Behavior

main.py:44: error: Returning Any from function declared to return "Concrete"  [no-any-return]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 1.12.1
  • Mypy command-line flags: --strict
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: python 3.12

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

Reproduziere das Beispiel mit Python 3.12 unter Verwendung von mypy 1.12.1 und des Flags --strict, und vergleiche adapt_proto mit adapt_callable. Verfolge, warum die Protocol-Fälle Any ableiten, und betrachte das Issue als erledigt, sobald das Beispiel nicht mehr den Fehler no-any-return meldet und beide Formen konsistent Concrete ableiten.

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
Größtenteils klar
Anfängerfreundlichkeit
45/100

Neue Issues direkt in Ihr Postfach

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