python / python/typing

ParamSpec: No support for adding keyword-only argument

Offen
#1,009 2 Kommentare 17 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

topic: feature
Vorherrschende Sprache
Python
Sterne
1.8k
Forks
302
Ø Merge
23 Std.
Gemergte PRs (30 T.)
8

Beschreibung

I've been looking through PEP 612 but I just wasn't able to find any way to implement behavior that would allow me to do this:

P = ParamSpec("P")
R = TypeVar("R")

def foo(f: Callable[P, R]) -> Callable[P, List[R]]:  # Here, the parameters are actually P + optional kw-only int x = 3
    def inner(*args: P.args, x: int = 3, **kwargs: P.kwargs) -> List[R]:
        results = []
        for _ in range(x):
          ret = f(*args, **kwargs)
          results.append(ret)
        return results
    return inner

@foo
def bar(p: int):
    return p + 2

bar(5)  # Acceptable
bar(5, x=8)  # Should also be Acceptable

Proposed solution:

Callable[[P.args, x: int = 3, P.kwargs], List[R]]

EDIT: I didn't notice that this was already in rejected alternatives section (I didn't scroll that far), however I still think an implementation like this would be worth adding since I have already encountered the need for this twice and there's simply absolutely no way to handle it.

I'm fine with the proposed syntax suggestion of

Concatenate[("x", int), P]

Though it didn't mention anything about default values, but those could just be handled by a third value in that tuple. It would probably be a lot easier to implement than my proposed suggestion, though to be fair, at least to me, my suggestion looks a bit cleaner.

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

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

Lies zunächst die verworfenen Alternativen von PEP 612 sowie die bestehenden Spezifikationen für ParamSpec und Concatenate. Da das Issue keine Implementierungsdateien oder Tests nennt, ermittle zuerst die relevanten Einstiegspunkte der typing-spec; für den Abschluss sind eine abgestimmte Syntax und ein definiertes Verhalten für optionale keyword-only-Parameter erforderlich.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
tooling
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

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