From a30cb97a1f6086c14ad4517d9de7613da35485e8 Mon Sep 17 00:00:00 2001 From: Johannes Valbjorn Date: Tue, 27 Feb 2018 08:40:17 +0100 Subject: [PATCH 1/2] request arg can be renamed --- sanicargs/__init__.py | 6 ++++-- tests/test_sanicargs.py | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/sanicargs/__init__.py b/sanicargs/__init__.py index 4012a67..7ac920c 100644 --- a/sanicargs/__init__.py +++ b/sanicargs/__init__.py @@ -48,6 +48,8 @@ async def generate_csv(request, query: str, businessunitid: str): (name, p.annotation, p.default) for name, p in notations.parameters.items() ] + request_arg_name = inspect.getfullargspec(func)[0][0] + async def inner(request, *old_args, **route_parameters): kwargs = {} @@ -57,8 +59,8 @@ async def inner(request, *old_args, **route_parameters): raw_value = request.args.get(name, None) # provided in route - if name in route_parameters or name=="request": - if name=="request": + if name in route_parameters or name==request_arg_name: + if name==request_arg_name: continue raw_value = route_parameters[name] diff --git a/tests/test_sanicargs.py b/tests/test_sanicargs.py index abc176d..6fe17a7 100644 --- a/tests/test_sanicargs.py +++ b/tests/test_sanicargs.py @@ -5,6 +5,8 @@ from sanicargs import parse_query_args, fields from sanic.websocket import WebSocketProtocol from sanic.exceptions import InvalidUsage +from sanic import request + import datetime @@ -34,13 +36,13 @@ async def test_date(request, test: datetime.date): @app.route("/list", methods=['GET']) @parse_query_args - async def test_list(request, test: fields.List[str] = None): + async def test_list(req, test: fields.List[str] = None): return response.json({'test': test}) @app.route("/all", methods=['GET']) @parse_query_args async def test_all( - request, + req: request, a: int, b: str, c: datetime.datetime, From e3b1deca00b8400a124ba5b0a64b7686fbd1f3fa Mon Sep 17 00:00:00 2001 From: Johannes Valbjorn Date: Thu, 1 Mar 2018 10:53:48 +0100 Subject: [PATCH 2/2] bump version to 1.1.0 and edit README --- HISTORY.rst | 7 ++++++- README.md | 6 ++++-- sanicargs/__version__.py | 2 +- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index e270965..f7954be 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -10,4 +10,9 @@ History ------------------ * added test suite -* added path_param type casting \ No newline at end of file +* added path_param type casting + +1.1.0(2018-03-01) +------------------ + +* request can be renamed and even type-annotated \ No newline at end of file diff --git a/README.md b/README.md index b4b2657..1b23a19 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ app = Sanic("test_sanic_app") @app.route("/me//birthdate", methods=['GET']) @parse_query_args -async def test_datetime(request, id: str, birthdate: datetime.datetime): +async def test_datetime(req, id: str, birthdate: datetime.datetime): return response.json({ 'id': id, 'birthdate': birthdate.isoformat() @@ -53,4 +53,6 @@ You need to apply the `parse_query_args` decorator as the first one executed whi ### `request` is mandatory! -You should always have request as the first argument in your function in order to use `parse_query_args` \ No newline at end of file +You should always have request as the first argument in your function in order to use `parse_query_args`. + +**Note** that `request` arg can be renamed and even type-annotated as long as it is the first arg. \ No newline at end of file diff --git a/sanicargs/__version__.py b/sanicargs/__version__.py index 75977e6..6df8ed0 100644 --- a/sanicargs/__version__.py +++ b/sanicargs/__version__.py @@ -1 +1 @@ -__version__ = '1.0.0' \ No newline at end of file +__version__ = '1.1.0' \ No newline at end of file