Skip to content

CLN: Refactor open into context manager in io tests #21139

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
May 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions pandas/tests/io/test_packers.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,8 @@ def test_string_io(self):
with ensure_clean(self.path) as p:

s = df.to_msgpack()
fh = open(p, 'wb')
fh.write(s)
fh.close()
with open(p, 'wb') as fh:
Copy link
Member

Choose a reason for hiding this comment

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

Do we have a name used more often than not for the file handle? I notice in this change we have two different ones. The last file I touched they were all as f: - if that's used more often than not then let's update these to stay consistent

Copy link
Contributor

Choose a reason for hiding this comment

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

f or fh is prob ok

Copy link
Member

Choose a reason for hiding this comment

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

@WillAyd : I think this is one of those gray areas where consistency and semantics are somewhat at odds. It's true that using as f would render it consistent, but names like outfile (as below) have their merit as providing more information about the file's usage.

fh.write(s)
result = read_msgpack(p)
tm.assert_frame_equal(result, df)

Expand Down
5 changes: 2 additions & 3 deletions pandas/tests/series/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,8 @@ def test_from_csv(self):
series_h = self.read_csv(path, header=0)
assert series_h.name == "series"

outfile = open(path, "w")
outfile.write("1998-01-01|1.0\n1999-01-01|2.0")
outfile.close()
with open(path, "w") as outfile:
outfile.write("1998-01-01|1.0\n1999-01-01|2.0")

series = self.read_csv(path, sep="|")
check_series = Series({datetime(1998, 1, 1): 1.0,
Expand Down