-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
io/msgpack: Raise a more informative exception on file not found #27162
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you add a whatsnew note in the bug fixes IO section, otherwise lgtm. CI is currently borked on 1 build, but ping when rest are green.
pandas/tests/io/test_packers.py
Outdated
@@ -934,3 +934,7 @@ def test_msgpack_period_freq(self): | |||
s = Series(np.random.rand(5), index=date_range('20130101', periods=5)) | |||
r = read_msgpack(s.to_msgpack()) | |||
repr(r) | |||
|
|||
def test_msgpack_file_not_found(self): | |||
bad_path = '1/2/3/4/path/does/not/exist' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you add the issue number here as a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed this test and added the issue number inline where the fix was done.
pandas/tests/io/test_packers.py
Outdated
|
||
def test_msgpack_file_not_found(self): | ||
bad_path = '1/2/3/4/path/does/not/exist' | ||
pytest.raises(read_msgpack, FileNotFoundError, bad_path) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use context manager or better still, i don't think you need to add a test.
changing TestCommonIOCapabilities.test_read_non_existant
and TestCommonIOCapabilities.test_read_expands_user_home_dir
in pandas/tests/io/test_common.py
would be sufficient.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, nice - looks like these tests handle this behavior already.
pandas/io/packers.py
Outdated
@@ -184,6 +184,8 @@ def read(fh): | |||
if exists: | |||
with open(path_or_buf, 'rb') as fh: | |||
return read(fh) | |||
else: | |||
return FileNotFoundError('{} not found'.format(path_or_buf)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
although this may simplify testing, i'd be inclined to just let Python raise the system specific exception.
i.e.
replace
# see if we have an actual file
if isinstance(path_or_buf, str):
try:
exists = os.path.exists(path_or_buf)
except (TypeError, ValueError):
exists = False
if exists:
with open(path_or_buf, 'rb') as fh:
return read(fh)
with
if isinstance(path_or_buf, str):
with open(path_or_buf, 'rb') as fh:
return read(fh)
and it raises (on windows)..
FileNotFoundError: [Errno 2] No such file or directory: 'this/path/does/not/exist'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed
As opposed to checking if it exists first.
Okay, requested fixes should be in place now:
|
@smspillaz looks like there are some merge conflicts - can you clean those up? |
Can be closed as #27201 was merged which fixes the same issue |
git diff upstream/master -u -- "*.py" | flake8 --diff