python / python/mypy

Unknown `DefaultNamedArg` raises type incompatibility errors

Offen
#9,385 1 Kommentar 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug topic-calls topic-type-variables
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

🐛 Bug Report

DefaultNamedArg may cause signature compatibility errors. Observed when a function has a default keyword argument in signature and is passed to be used as a function without that particular keyword argument.

To Reproduce

from typing import List, AnyStr
from glob import glob
import itertools as it
def expand_patterns(
        pats: List[AnyStr],
        sort=True
):
    paths = it.chain(map(glob, pats))
    # Argument 1 to "map" has incompatible type "Callable[[AnyStr, DefaultNamedArg(bool, 'recursive')], List[AnyStr]]"; expected "Callable[[AnyStr], List[AnyStr]]"
    if sort:
        paths = sorted(paths)
    return paths

Expected Behavior

Not sure if this ought to happen, but I'd expect a function like foo(x: int, y='y') to be peacefully passable to a function expecting Callable[[int], Any].

Actual Behavior

# Argument 1 to "map" has incompatible type "Callable[[AnyStr, DefaultNamedArg(bool, 'recursive')], List[AnyStr]]"; expected "Callable[[AnyStr], List[AnyStr]]"

Your Environment

  • Mypy version used: 0.782
  • Mypy command-line flags: Default of VSCode
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: 3.7.7
  • Operating system and version: Windows 10, 2004

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 bereitgestellte Python-Beispiel mit glob, map, Callable-Kompatibilität und DefaultNamedArg zu reproduzieren. Verfolge, wie mypy die Callable-Signaturen vergleicht, und überprüfe anschließend, dass ein standardmäßiges Schlüsselwortargument gemäß der vorgesehenen Kompatibilitätsregel behandelt wird und die gemeldete Inkompatibilität behoben ist.

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
35/100

Neue Issues direkt in Ihr Postfach

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