Seite 1 von 1

URL-Routing mit Funktionsannotationen

Verfasst: Sonntag 6. August 2017, 15:37
von nezzcarth
Hallo :)

Ich hätte heute eine etwas experimentelle Frage:
Einige Webframeworks verwenden ja Dekoratoren, um Handler für URLs/URL-Muster zu registrieren. Letztens dachte ich, dass das ja theoretisch auch mit Funktionsannotationen (für den Rückgabewert) gehen müsste:

Code: Alles auswählen

def hello_world(*args, **kwargs) -> '/hello_world':
    return "Hello"                         
Ein kurzes Proof-of-Concept (unter Zuhilfenahme von 'werkzeug' für den WSGI-Kram und 'inspect') hat ergeben, dass das prinzipiell funktionieren würde. Allerdings sehe ich keine echten Vorteile (gegenüber Dekoratoren), ein darauf basierendes Routingsystem tatsächlich zu verwenden, wohl aber ein paar negative Punkte. Trotzdem wäre meine Frage: Ist euch diese Verwendung von Annotations schon mal untergekommen, bzw. wird etwas in der Art tatsächlich irgendwo verwendet?

Re: URL-Routing mit Funktionsannotationen

Verfasst: Sonntag 6. August 2017, 16:08
von BlackJack
@nezzcarth: Nachdem Guido sich dann doch mal recht definitiv dazu geäussert hat das die Annotationen für Typinformationen verwendet werden sollen, denke ich mal, das das keiner mehr für etwas anderes verwenden wird.

Ich find's auch optisch genau verkehrt herum, denn es ist ja keine Abbildung Funktion → URL sondern URL → Funktion, somit ist der ”Pfeil” (``->``) falsch herum.

Re: URL-Routing mit Funktionsannotationen

Verfasst: Sonntag 6. August 2017, 16:31
von nezzcarth
@BlackJack:
Danke für deine Einschätzung.

Ich hatte in Erinnerung, dass Annotations primär für Typen gedacht sind (anders als in PEP 3107 vorgeschlagen, das noch von "[…] arbitrary metadata annotations to Python functions […]" spricht). Dass es definitive Aussagen dazu gibt, war mir nicht bekannt.

Den Einwand mit der Abbildung hatte ich nicht bedacht; da ist natürlich was dran :)

Re: URL-Routing mit Funktionsannotationen

Verfasst: Sonntag 20. August 2017, 09:16
von noisefloor
Hallo,
Nachdem Guido sich dann doch mal recht definitiv dazu geäussert hat
Du hast doch bestimmt auch einen passenden Link dazu, oder? :-)

Gruß, noisefloor

Re: URL-Routing mit Funktionsannotationen

Verfasst: Sonntag 20. August 2017, 11:08
von BlackJack
@noisefloor: Aus folgender E-Mail [Python-ideas] Proposal: Use mypy syntax for function annotations:
AFAIK in practice there is little use of function annotations in
mainstream code, and I propose a conscious change of course here by stating
that annotations should be used to indicate types and to propose a standard
notation for them.

(We may have to have some backwards compatibility provision to avoid
breaking code that currently uses annotations for some other purpose.
Fortunately the only issue, at least initially, will be that when running
mypy to type check such code it will produce complaints about the
annotations; it will not affect how such code is executed by the Python
interpreter. Nevertheless, it would be good to deprecate such alternative
uses of annotations.)
Und das `types`-Modul ist ja nun in der Standardbibliothek. Sowie PEP 484 — Type Hints von Guido.

Ich glaube nicht das jetzt noch jemand Funktions-Annotationen für etwas anderes als Typhinweise verwenden wird.

Re: URL-Routing mit Funktionsannotationen

Verfasst: Sonntag 20. August 2017, 17:42
von DasIch
Apistar ist relativ neu und nutzt Annotations für Dependency Injection.