Regression since mypy 1.7 with functions that return a generic protocol
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
EDIT: I found a simpler example reproducing the regression, see below
The following code used to pass with mypy 1.6.1
from typing import (
Protocol,
TypeVar,
Callable,
Concatenate,
ParamSpec,
reveal_type,
)
X = TypeVar("X")
Y = TypeVar("Y")
P = ParamSpec("P")
I = TypeVar("I", contravariant=True)
O = TypeVar("O", covariant=True)
# A callable converting I to O with parameters P
ConvertorCallable = Callable[Concatenate[I, P], O]
# An object that can convert I to O with parameters P
class ConvertorProtocol(Protocol[I, P, O]):
def convert(self, __source: I, *args: P.args, **kwargs: P.kwargs) -> O:
...
# Decorator to convert a callable to a convertor
def convertor(
func: ConvertorCallable[X, P, Y],
) -> ConvertorProtocol[X, P, Y]:
class Convertor:
def convert(self, source: X, /, *args: P.args, **kwargs: P.kwargs) -> Y:
return func(source, *args, **kwargs)
return Convertor()
# A convertor that converts X to a list of X
@convertor
def as_list(source: X, repeat: int = 1) -> list[X]:
return [source] * repeat
if __name__ == "__main__":
result = as_list.convert(1, repeat=3)
reveal_type(result)
print(result)
However, it no longer passes with mypy 1.7.0 and later:
error: Argument 1 to "convert" of "ConvertorProtocol" has incompatible type "int"; expected Never [arg-type]
Note that this sample works in the pyright playground.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit dem eigenständigen Reproducer im Issue und führe ihn mit mypy 1.6.1 und 1.7.0 oder höher aus, um die Regression zu bestätigen. Verfolge die Inferenz generischer Protocols unter Einbeziehung von ParamSpec, TypeVar und Concatenate; abgeschlossen ist die Aufgabe, wenn das Beispiel für das Argument nicht mehr Never meldet und die Regression abgedeckt ist.
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
- 38/100