Skip to content

TYP: to_markdown fails mypy check if using pathlib.Path #46211

Closed
@alexpovel

Description

@alexpovel

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

from pathlib import Path

from pandas import DataFrame

file = Path("table.md")
df = DataFrame({"A": [1, 2], "B": [3, 4]})
df.to_markdown(file)

Issue Description

This was kind of addressed in #43605 , but that issue is closed and I still stumbled over it. The snippet from the example runs fine, producing a table.md with:

|    |   A |   B |
|---:|----:|----:|
|  0 |   1 |   3 |
|  1 |   2 |   4 |

The documentation states:

buf: str, Path or StringIO-like, optional, default None

Buffer to write to. If None, the output is returned as a string.

In the above snippet, a pathlib.Path object was passed and it worked. However, the source still reads:

buf: IO[str] | str | None = None,

which was described as "too strict". And indeed, the above fails mypy (version 0.910):

$ mypy file.py
file.py:7: error: Argument 1 to "to_markdown" of "DataFrame" has incompatible type "Path"; expected "Optional[IO[str]]"
Found 1 error in 1 file (checked 1 source file)

Expected Behavior

For not only the code to run successfully, but also type checks to pass.

Installed Versions

INSTALLED VERSIONS

commit : 06d2301
python : 3.9.10.final.0
python-bits : 64
OS : Linux
OS-release : 5.10.16.3-microsoft-standard-WSL2
Version : #1 SMP Fri Apr 2 22:23:49 UTC 2021
machine : x86_64
processor :
byteorder : little
LC_ALL : None
LANG : C.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.4.1
numpy : 1.22.2
pytz : 2021.3
dateutil : 2.8.2
pip : 21.2.4
setuptools : 58.1.0
Cython : None
pytest : 5.4.3
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : 1.1
pymysql : None
psycopg2 : None
jinja2 : 3.0.3
IPython : 8.1.1
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.5.1
numba : None
numexpr : None
odfpy : None
openpyxl : 3.0.9
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.6.1
sqlalchemy : None
tables : None
tabulate : 0.8.9
xarray : None
xlrd : None
xlwt : None
zstandard : None

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