Skip to content

Excel function is partially unknown if optional packages aren't installed #1771

Description

@umichfagan

Describe the bug
My specific issue was: DataFrame.to_excel() is partially unknown unless the optional Excel packages are installed.

To Reproduce
I am using basedpyright on strict mode. If I don't have all of xlsxwriter, pyxlsb, and odfpy installed, then the function to_excel() is reported as partially unknown.

def streaming_download(
    data: pandas.DataFrame, filename: str, filetype: str
) -> flask.Response:
    headers = {
        "Content-Disposition": f"attachment; filename={filename}.{filetype}",
    }

    def generate():
        stream = io.BytesIO()
        if filetype == "csv":
            data.to_csv(stream, index=False)
        else:
            data.to_excel(stream, index=False)
        yield stream.getvalue()

    return flask.Response(generate(), headers=headers)

error: Type of "to_excel" is partially unknown
Type of "to_excel" is "(excel_writer: str | PathLike[str] | WriteExcelBuffer | ExcelWriter[Workbook | Unknown], sheet_name: str = "Sheet1", na_rep: str = "", float_format: str | None = ..., columns: str | Sequence[str] | None = ..., header: bool | list[str] = True, index: bool = True, index_label: str | Sequence[str] | None = ..., startrow: int = 0, startcol: int = 0, engine: str | None = ..., merge_cells: bool | Literal['columns'] = True, inf_rep: str = "inf", freeze_panes: tuple[int, int] | None = ...) -> None" (reportUnknownMemberType)

Specs:

# MacOS Tahoe
# python 3.14
pandas==3.0.2
openpyxl==3.1.5
basedpyright==1.39.3
pandas-stubs==3.0.3.260530

# to fix...
# xlsxwriter==3.2.9
# pyxlsb==1.0.10
# odfpy==1.4.1

Additional context
I only found the intended fix (adding these three packages) because of this comment on issue #1710.

I think it'd be great to have this behavior documented somewhere (maybe it already is and I couldn't find it?). If there are other optional dependencies, those would be great to have documented too.

I also wonder if this topic could be approached differently -- maybe something like how pandas specifies optional extras (e.g. pandas-stubs[excel] so that it is bundled together? Just an idea. Thanks for the work.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions