question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[QUESTION] Optional **kwargs

See original GitHub issue

I’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:closed
  • Created 4 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
dmontagucommented, Jan 16, 2020

@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.

1reaction
dmontagucommented, Jan 15, 2020

If you want access to the raw query parameters dict, this is available via request.query_params if you add request: starlette.requests.Request as an argument to your endpoint function. Let us know if that doesn’t answer your question.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found