type hint/casting for parameterized values

15 views
Skip to first unread message

Chris Nyland

unread,
Jan 9, 2017, 2:22:03 PM1/9/17
to Falcon Framework
Hello

I have been surveying the different ways to create Rest APIs in Python and have settled on Falcon because of its minimalist approach and clean structure. However coming from Flask and other frameworks I miss being able to specify a type for URL parameters. Am I just missing something or does Falcon not provide for this kind of automatic conversion? If not is there there a way I could add it by subclassing and overriding a existing method. Especially since I could take advantage of Python3 new type hinting.


Kurt G. | @kgriffs

unread,
Jan 24, 2017, 1:23:07 PM1/24/17
to Falcon Framework
Hi Chris, we have this work in progress:


Type hinting could be another approach, where the framework would attempt to convert to the type specified for the on_*() parameters. This could perhaps be done in addition to supporting filters for more complex conversions. 

You could implement this in (at least) a few different ways without modifying the framework directly. Any of these could be published as a standalone package to PyPI as a Falcon add-on:
  • With a decorator on the resource class that introspects and wraps each on_*() responder method with code. This is probably the most performant strategy, since you only introspect the type hints once up front.
  • With a process_resource() middleware method. Would require introspecting the type hints for every call.
  • By overriding API._get_responder(). Call super and then transform the results before returning to the caller.
  • By subclassing the default router and overriding the find() method. You could also override add_route() to construct a conversion lookup table and attach it to the new node, so that find() doesn't have to introspect the type hints each time.
Otherwise, if you like, you are more than welcome to file an issue to have this added to the framework directly. We would need a volunteer from the community to take on the work.

@kgriffs
Reply all
Reply to author
Forward
0 new messages