Skip to content

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

Closed
wants to merge 1 commit into from

Conversation

smspillaz
Copy link

@smspillaz smspillaz commented Jul 1, 2019

@jreback jreback added this to the 0.25.0 milestone Jul 1, 2019
Copy link
Contributor

@jreback jreback left a 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.

@@ -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'
Copy link
Contributor

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

Copy link
Author

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.


def test_msgpack_file_not_found(self):
bad_path = '1/2/3/4/path/does/not/exist'
pytest.raises(read_msgpack, FileNotFoundError, bad_path)
Copy link
Member

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.

Copy link
Author

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.

@@ -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))
Copy link
Member

@simonjayhawkins simonjayhawkins Jul 1, 2019

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'

Copy link
Author

Choose a reason for hiding this comment

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

Agreed

@simonjayhawkins simonjayhawkins added the Error Reporting Incorrect or improved errors from pandas label Jul 1, 2019
@jreback jreback removed this from the 0.25.0 milestone Jul 2, 2019
As opposed to checking if it exists first.
@smspillaz
Copy link
Author

Okay, requested fixes should be in place now:

  • Fixed CI
  • Removed test and instead amended TestIOCommon
  • Added whatsnew entry
  • Added comment in packers.py indicating bug

@WillAyd
Copy link
Member

WillAyd commented Jul 8, 2019

@smspillaz looks like there are some merge conflicts - can you clean those up?

@smspillaz
Copy link
Author

Can be closed as #27201 was merged which fixes the same issue

@smspillaz smspillaz closed this Jul 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Error Reporting Incorrect or improved errors from pandas
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Misleading error for pd.read_msgpack
4 participants