ofajardo/pyreadr

Allow Python 3's pathlib.Path as an alternative to str

vaneseltine opened this issue · 2 comments

Describe the issue

In four places, pyreadr.py requires that paths are provided as str, and throws PyreadrError if provided with a pathlib.Path

To Reproduce

Using Python 3:

>>> from pathlib import Path
>>> import pyreadr
>>> input_file = Path("./spam.rdata")
>>> pyreadr.read_r(input_file)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "[...]/pyreadr/pyreadr.py", line 44, in read_r
    raise PyreadrError("path must be a string!")
pyreadr.custom_errors.PyreadrError: path must be a string!

Expected behavior

pathlib has been part of the standard library since 3.4, and pathlib.Path("./breakfast.rdata") should be as valid a path specification as "./breakfast.rdata" for all supported versions of Python 3.

Solutions

Given that:

  • pyreadr continues to support 2.7, which does not include pathlib
  • the path is must ultimately go as a string to librdata, which we leave alone
  • any pathlib backport would add an external dependency, which is not desired

...the main options appear to be:

  1. Expand the path handling to allow pathlib.Path specifically in addition to str
  2. path = str(path) to convert whatever is provided into a string

Either would work but follow somewhat different interface philosophies.

Thanks for the suggestion. I solved it recently in my other library Pyreadstat, so I should be able to reproduce that here as well. Will do when I get a bit of time.

fixed in version 0.4.2