I use a custom exception handler with Fast API:
class CustomException(Exception):
def __init__(self, err_message: str):
self.err_message = err_message
@app.exception_handler(CustomException)
async def custom_exception_handler(request: Request, exc: CustomException):
return JSONResponse(content={"error": exc.err_message})
Pycharm put the 'request: Request' as a weak warning:
''Parameter 'request' value is not used'' since it's not used in the code.
However, if I remove the parameter, I get a Fast API error when running the code. So I wonder if this is a PyCharm 'bug', if we can call it that way ?
TL;DR:
You use the _
as a replacement of the name or a prefix
Answer:
For that you don't disable the warning as suggest in the comments, it's is considered a bad practice to disable warnings because they exist for a reason. A better approach is to do some root cause analysis understand the warning by googling why and find a proper code change that removes it.
Working with linters in general some times it will be possible to think that there is no way to fix a warning or a suggestion other than causing another one to pop up. But you are getting there: you have to pick a solution that enhances code readability and maintainability without breaking any more rules or the actual software
In general in python there are some methods that will require parameters even if you don't use it because the above infrastructure using them is expecting to recieve a method with a certain signature template. For example when defining serverless lambda functions it is expected to have a event
and a context
but you might as well not have any relevant information in the incoming event or the execution context to use.
You have to learn how to discard parameters while keeping them. For that you use the
_
as a replacement of the name or a prefix
def lambda_example(event, context):
"""This raises a warning ⚠️"""
print(event)
def second_example(event,_):
"""This does not ✔️"""
print(event)
def third_example(_event,_context):
"""This does not either ✔️"""
print("this does not care about it's parameters")