'Type warning on pandas to_csv method path_or_buf variable given an HTTP response

I'm passing a pandas dataframe as csv file to the HTTP response and the code is working well.

BUT I have here a warning on type hint on pandas' method to_csv on path_or_buf variable, Pycharm warns with :

Expected type 'Union[str, PathLike[str], WriteBuffer[bytes], WriteBuffer[str], None]',
 got 'HttpResponse' instead

This is the code used :

def get_dataframe_response(request: Request) -> HttpResponse:

    my_dataframe: DataFrame = pd.DataFrame(<my_data_here>)

    get_dataframe_response: HttpResponse = HttpResponse(content_type="text/csv")
    get_dataframe_response["Content-Disposition" = "attachment; my_filename.csv"
    my_dataframe.to_csv(path_or_buf=get_dataframe_response, index=False, header=True)

    return get_dataframe_response

-> Do you know what's causing this error or what should I use instead ?

I tried get_dataframe_response.content but had issues and code stopped working.

I saw pandas had issues even 6 months ago with type hints on path_or_buf variable already. https://youtrack.jetbrains.com/issue/PY-34790

But I want to make sure I use the right types.

I'm using : PyCharm 2021.3.2 (Professional Edition) on Ubuntu 20.04, Django 3.2 and pandas 1.4.1



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source