Skip to content

ENH: Add support for third-party path-like objects by backporting os.fspath #12157

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 31, 2018

Conversation

eric-wieser
Copy link
Member

In response to #11348

fid = file.open("rb")
own_fid = True
else:
if hasattr(file, 'read'):
fid = file
own_fid = False
Copy link
Member Author

@eric-wieser eric-wieser Oct 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some alternatives to this block, and the similar ones below

This patch:

if hasattr(file, 'read'):
    fid = file
    own_fid = False
else:
    fid = open(os_fspath(file), "rb")
    own_fid = True

Alternative 1 (smaller diff);

if isinstance(file, os_PathLike):
    file = os_fspath(file)
if isinstance(file, basestring):
    fid = open(file, "rb")
    own_fid = True
else:
    fid = file
    own_fid = False

Alternative 2 (no duck-typing):

if isinstance(file, io.IOBase):
    fid = file
    own_fid = False
else:
    fid = open(os_fspath(file), "rb")
    own_fid = True

elif is_pathlib_path(filename):
if is_pathlib_path(filename):
# special case - if we were constructed with a pathlib.path,
# then filename is a path object, not a string
self.filename = filename.resolve()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the only remaining use of is_pathlib_path, which is needed for a weird special case.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we need is_pathlib_path anyway, PR #11348 more straight-forward and simpler.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is orthogonal to PR #11348 - this makes numpy work with backports of pathlib too / third-party objects implementing __fspath__ too.

@eric-wieser eric-wieser force-pushed the fspath branch 2 times, most recently from 34a4160 to a47aa9e Compare October 12, 2018 15:12
@tylerjereddy
Copy link
Contributor

Azure is missing from the CI list on this and some other recent PRs -- I'll open an issue.

@eric-wieser
Copy link
Member Author

Coverage failures are garbage, and caused by us not collecting coverage data in python < 3.6

@eric-wieser
Copy link
Member Author

@tylerjereddy: did you find a workaround to make the CI run?

@mattip
Copy link
Member

mattip commented Oct 16, 2018

Closing/reopening to restart CI

@mattip mattip closed this Oct 16, 2018
@mattip mattip reopened this Oct 16, 2018
@tylerjereddy
Copy link
Contributor

@eric-wieser No, but maybe Microsoft did something since their engineers were investigating. Looks like Azure is running on all recent PR triggers including updates / force pushes, etc.

@eric-wieser
Copy link
Member Author

eric-wieser commented Oct 31, 2018

Note that the only way to make CI pass here is to collect and merge coverage data on multiple python versions - it's impossible to exercise python3.5-only code if we only collect coverage data on 3.6. Perhaps we should exclude compat from the coverage report?

@tylerjereddy
Copy link
Contributor

Perhaps we should exclude compat from the coverage report?

It would be nice to cover the other Python versions, but not sure if it is worth the effort and / or risk that we break the fragile codecov stuff as happened with SciPy.

I'd probably lean toward leaving most files in the report but having the reviewer skeptical of cov-related failures, but probably not a big issue either way as long it is not a big slippery slope with progressive blacklisting of lots of files on the report.

@mattip
Copy link
Member

mattip commented Oct 31, 2018

At this point I prefer the current situation - when codecov fails the commiter/reviewer asks to ignore the fail.

@mattip
Copy link
Member

mattip commented Oct 31, 2018

LGTM - will merge soon if there are no other comments

@mattip mattip merged commit 95998ca into numpy:master Oct 31, 2018
@mattip
Copy link
Member

mattip commented Oct 31, 2018

Thanks Eric

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants