Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Any way to declare type of path parameters ? #3400

Closed
buxx opened this issue Nov 20, 2018 · 4 comments
Closed

Any way to declare type of path parameters ? #3400

buxx opened this issue Nov 20, 2018 · 4 comments
Labels

Comments

@buxx
Copy link

buxx commented Nov 20, 2018

Story

When declare a path with regex, parameters can't be delcared with a type.

Actual behaviour

All path parameters are string.

Steps to reproduce

from aiohttp import web


async def handle(request):
    name = request.match_info.get('id', 42)
    text = str(type(name))
    return web.Response(text=text)

app = web.Application()
app.add_routes([web.get('/', handle),
                web.get(r'/{id:\d+}', handle)])

web.run_app(app)
$ http :8080/42
HTTP/1.1 200 OK
Content-Length: 13
Content-Type: text/plain; charset=utf-8
Date: Tue, 20 Nov 2018 16:33:58 GMT
Server: Python/3.7 aiohttp/3.4.4

<class 'str'>

My question/issue

It exist a way to declare the type of path parameters ? I would like obtain an int in this example.

@aio-libs-bot
Copy link

GitMate.io thinks the contributor most likely able to help you is @asvetlov.

Possibly related issues are #752 (There is no way to 'del_route'), #15 (No way to close a response on a timeout), #1325 ([QUESTION] WebSocketResponse timeout parameter), #977 (Change connector's limit parameter), and #836 (Provide a way to associate context with requests).

@aio-libs-bot aio-libs-bot added bug question StackOverflow labels Nov 20, 2018
@asvetlov
Copy link
Member

match_info always contains strings. URL path is a string, path part is a string too.

If you need to cast a parameter to another type -- please do it (and handle conversion errors) explicitly.
Like

str_id = request.match_info.get('id', '42')
try:
   int_id = int(str_id)
except ValueError:
   ...

Extra benefit is that explicit type casting works very well with mypy type checker.

@buxx
Copy link
Author

buxx commented Nov 21, 2018

Okay. Thank's for your time @asvetlov .

@buxx buxx closed this as completed Nov 21, 2018
@lock
Copy link

lock bot commented Nov 21, 2019

This thread has been automatically locked since there has not been
any recent activity after it was closed. Please open a new issue for
related bugs.

If you feel like there's important points made in this discussion,
please include those exceprts into that new issue.

@lock lock bot added the outdated label Nov 21, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Nov 21, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants