decorest icon indicating copy to clipboard operation
decorest copied to clipboard

Add ability to use `kwargs` for use with decorator

Open sukram230799 opened this issue 2 years ago • 4 comments

Hello @bkryza

I have noticed that I'm unable to use keyword args for the decorators. I hope the examples provide an insight in what I want to achieve.

@GET('/users')
@query('search')
@query('limit')
def search_user(search, limit=10):
    pass

Ordered Parameters: works

client.search_user('123', 20)

I think this is one/the "decorest" way: works

client.search_user('123', query={'limit': 20})

But I would like to use keyword arguments: doesn't work

client.search_user('123', limit=20)
# or
client.search_user(search='123', limit=20)

I have submitted a possible fix for it. But I would consider it more of a workaround, as it manipulates a pass by reference array (kwargs). But I have submitted it anyway as a point to start a discussion.

Additionally I would like to add some test cases. Where should I put them?

Thanks Markus

  • Use both args_dict and kwargs for _merge_args(...)
  • Remove 'used' kwargs so that they don't collide with httpx/requests

sukram230799 avatar Jul 19 '22 13:07 sukram230799

Codecov Report

Merging #11 (7fa2def) into master (c85fab4) will decrease coverage by 0.18%. The diff coverage is 80.00%.

@@            Coverage Diff             @@
##           master      #11      +/-   ##
==========================================
- Coverage   94.08%   93.89%   -0.19%     
==========================================
  Files          16       16              
  Lines         930      934       +4     
==========================================
+ Hits          875      877       +2     
- Misses         55       57       +2     
Impacted Files Coverage Δ
decorest/request.py 92.52% <80.00%> (-0.81%) :arrow_down:

:mega: Codecov can now indicate which changes are the most critical in Pull Requests. Learn more

codecov-commenter avatar Jul 19 '22 13:07 codecov-commenter

@sukram230799 Thanks for the PR - again :-)

The code looks ok, could you please add a test case for this for instance by duplicating these test cases with kwargs parameters in the calls, you can call it for instance test_user_methods_with_kwargs_params...

One without typing enabled:

  • https://github.com/bkryza/decorest/blob/master/tests/petstore_test.py#L121-L163

and also this one to make sure it also works for clients with typing:

  • https://github.com/bkryza/decorest/blob/master/tests/petstore_test_with_typing.py#L115-L153

These test cases hopefully should cover args coming from both method as well as query decorators...

bkryza avatar Jul 20 '22 20:07 bkryza

Okay I have not considered the in path case. So that fails...

    @GET('user/{username}')
    def get_user(self, username):
        """Get user by user name."""

With the testcase

res = client.get_user(username='swagger')

I'll have a look in the code on how to fix that

sukram230799 avatar Jul 22 '22 16:07 sukram230799

I have found an even bigger flaw. What if we use a kwarg multiple times. I don't know what the use-case would be - but it would be valid code. As I currently delete the used kwargs we would run into a problem.

Back to the drawing board

sukram230799 avatar Jul 22 '22 17:07 sukram230799