Skip to content

[TYPING] pandas.io.formats.format._binify annotation is inconsistent with DataFrameFormatter.__init__ #28843

Closed
@zero323

Description

@zero323

Problem description

pd.io.formats.format._binify defines line_width as Union[np.int32, int]:

def _binify(cols: List[np.int32], line_width: Union[np.int32, int]) -> List[int]:

however it is inconsistent with DataFrameFormatter.__init__

class DataFrameFormatter(TableFormatter):

and DataFrameFormatter._join_multiline

col_bins = _binify(col_widths, lwidth)

which take and pass as-is Optional[int].

This leads to Mypy error:

pandas/io/formats/format.py:871: error: Argument 2 to "_binify" has incompatible type "Optional[int]"; expected "Union[int32, int]"

Providing default in _join_multiline, i.e.

lwidth = self.line_width or DEFAULT_LINE_WIDTH

could be reasonable fix. One could also ignore the call:

col_bins = _binify(col_widths, lwidth)  # type: ignore

but that leaves possible correctness problem.

Expected Output

Passing Mypy type check

Output of pd.show_versions()

INSTALLED VERSIONS
------------------
commit           : bee17d5f4c99e6d1e451cf9a7b6a1780aa25988d
python           : 3.7.3.final.0
...

Additionally

mypy==0.730

CC @WillAyd

Metadata

Metadata

Assignees

No one assigned

    Labels

    Typingtype annotations, mypy/pyright type checking

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions