False positive: Callable arg types more strict than required
Offen
Dieses Issue hat noch niemand übernommen.
bug
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Struggled to name this properly but here's a repro:
import typing
# Want to accept any callable with a string arg
CallableT = typing.Callable[[str], None]
def func(callable_m: dict[str, CallableT]) -> None:
callable_m['first']("a")
def first(a: str) -> None:
return None
def second(a: str) -> None:
return None
def third(b: str) -> None:
return None
# This mapping will fail because the callables will have type:
# Callable[[Arg(str, 'a')], None]
failed_mapping = {
'first': first,
'second': second
}
func(failed_mapping)
# By renaming the arg in `third` from `a` to `b` everything is groovy
passing_mapping = {
'first': first,
'third': third
}
func(passing_mapping)
https://mypy-play.net/?mypy=latest&python=3.11&gist=ff039c923e370f79a680c463ef627810
Easy workaround with renaming the arg but is a bit of a pain
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 der bereitgestellten Python-Reproduktion und dem mypy-play-Link und verfolge dann die Prüfung der Callable-Kompatibilität, die Parameternamen vergleicht. Bestätige das Verhalten mit einem fokussierten Regressionstest; fertig ist die Änderung, wenn kompatible Callables wie first und second akzeptiert werden, ohne ihre Parameter umzubenennen.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- tooling
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100