[QUESTION] Optional **kwargs
See original GitHub issueI’ve got a base class for my view that has a get(...)
method I used for many different endpoints. Just like in regular non-typed python I have a need for **kwargs that’s optional. I.e. certain views have querypamaters and others don’t and all with varying types.
The meat of the get(...)
is the same for all views hence I’m trying to stay DRY here but I’m new to python typing and FastAPI so I don’t know how to go about doing this without cluttering up my code just to handle each view’s get request.
Thanks!
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Calling a Python function with *args,**kwargs and optional ...
You can do that in Python 3. def func(a,b,*args,kw1=None,**kwargs):. The bare * is only used when you want to specify keyword only arguments...
Read more >10 Examples to Master *args and **kwargs in Python
Keywords arguments are optional (they take the default value if not specified). *args collects the positional arguments that are not explicitly ...
Read more >Using Python Optional Arguments When Defining Functions
In this tutorial, you'll learn about Python optional arguments and how to define ... create functions that accept any number of arguments using...
Read more >Python Optional Arguments: A How-To Guide
Optional arguments are arguments that do not need a value. On Career Karma, learn how to accept optional arguments in a Python function....
Read more >How To Use *args and **kwargs in Python 3
In this tutorial, we will cover the syntax of working with *args and **kwargs as parameters within functions to pass a variable number...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@Subaku Yeah, I’m not sure of a great way to do this. GraphQL might be relevant depending on what you are trying to do. In general, for type safety you’d probably need to specify the specific parameters you are willing to accept everywhere it differs. You can always take this non-type-safe approach though too and do manual validation in your endpoint code if that is preferable for some reason.
If you want access to the raw query parameters dict, this is available via
request.query_params
if you addrequest: starlette.requests.Request
as an argument to your endpoint function. Let us know if that doesn’t answer your question.