Can't resolve generic type from return type of callable passed as (default) argument?
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
UPDATE: Probably a dup of #3737 and #10504.
What I want to do is define a function that takes a callable that returns a generic as an argument where that generic influences the return type of the function. Something like this:
def doit(constructor: Callable[..., T]) -> List[T]:
...
I can't seem to figure out a way to get that done, though, especially when I am supplying a default argument for the callable. Here are my test cases:
from typing import Callable, List, Protocol, TypeVar
_T = TypeVar("_T")
# error: Incompatible default for argument "constructor" (default has type "Type[str]", argument has type "Callable[[int], _T]")
def generate(n: int, constructor: Callable[[int], _T] = str) -> List[_T]:
return [constructor(i) for i in range(n)]
# error: Need type annotation for "res" (hint: "res: List[<type>] = ...")
res = generate(5) # res should be of type List[str]
assert res == ["0", "1", "2", "3", "4"]
assert generate(5, float) == [0.0, 1.0, 2.0, 3.0, 4.0]
_T_co = TypeVar("_T_co", covariant=True)
class Constructor(Protocol[_T_co]):
def __call__(self, n: int) -> _T_co:
...
def generate2(n: int, constructor: Constructor[_T] = str) -> List[_T]:
return [constructor(i) for i in range(n)]
# error: Need type annotation for "res2" (hint: "res2: List[<type>] = ...")
res2 = generate2(5, float) # res2 should be of type List[float]
assert res2 == [0.0, 1.0, 2.0, 3.0, 4.0]
Runtime:
% python --version
Python 3.9.6
% python test.py
% mypy --version
mypy 0.910
% mypy --config-file=/dev/null test.py
/dev/null: No [mypy] section in config file
test.py:6: error: Incompatible default for argument "constructor" (default has type "Type[str]", argument has type "Callable[[int], _T]")
test.py:10: error: Need type annotation for "res" (hint: "res: List[<type>] = ...")
test.py:24: error: Need type annotation for "res2" (hint: "res2: List[<type>] = ...")
Found 3 errors in 1 file (checked 1 source file)
I tried searching for a similar issue, but was unable to find what I was looking for.
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
Die Reproduktion befindet sich in test.py; prüfe zuerst die zugehörigen Issues #3737 und #10504 und führe dann mypy --config-file=/dev/null test.py aus. Erledigt bedeutet, dass der aufrufbare Default akzeptiert wird und die generate-Aufrufe List[str] und List[float] ohne die gemeldeten Fehler ableiten.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- compilers, devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 25/100