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
bpo-37008: make mock_open handle able to honor next() #13492
Conversation
Hello, and thanks for your contribution! I'm a bot set up to make sure that the project can legally accept your contribution by verifying you have signed the PSF contributor agreement (CLA). Our records indicate we have not received your CLA. For legal reasons we need you to sign this before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue. If you have recently signed the CLA, please wait at least one business day You can check yourself to see if the CLA has been received. Thanks again for your contribution, we look forward to reviewing it! |
Can you add a test that reproduces the issue today and shows it is fixed (to make sure this does not break in the future). The example you have in the issue is good, you can just add it to the You also need to add a news entry for this change, something in the lines of:
Great fix by the way! :) |
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.
Please also test __next__
along with next(mock)
and also your next
calls after exhausting state[0]
raises StopIteration
so add a test for that too.
I would prefer having these tests grouped under the class for reference
class TestMockOpen(unittest.TestCase): |
This is a code change and would require CLA. Please sign it. Change requires a NEWS entry : https://devguide.python.org/committing/?highlight=news#what-s-new-and-news-entries
Thanks
@mariocj89 @tirkarthi thank you for your support and your advice. It's been appreciated. I've updated the NEWS and the unittests. Sorry about the push force, btw, I realized in the mean time i was still using an old user.email to commit and had to amend the commits. |
CLA has been signed, btw. |
def test_mock_open_using_next(self): | ||
mocked_open = mock.mock_open(read_data='1st line\n2nd line\n3rd line') | ||
f1 = mocked_open('a-name') | ||
self.assertEqual(next(f1), '1st line\n') |
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.
Personally, grouping the expected data with variables from assert calls reads nice.
line1 = next(f1)
line2 = f1.__next__()
# line 3
self.assertEqual(line1, '1st line\n')
# assert for line 2
# assert for line 3
Also add a test for StopIteration
after exhausting f1. You can use
with self.assertRaises(StopIteration):
next(f1)
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.
Would prefer moving it to TestMockOpen
for these tests but would wait for other's opinion on this
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've applied the changed you've suggested and also added the missing StopIteration test.
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.
Also, would it be more consistent to test __next__()
prior to next()
?
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.
Sorry I implied moving the testmock test to testwith. It seems like two copies.
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.
Yeah, I've understood that :) but since I see some other already-redundant tests in both files and and since @mariocj89 said testmock and you said testwith.py, I've added both. No offense but I dont know either of you two so I honestly dont who is right. :)
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.
Well, it was just an opinion of mine that to have tests grouped under the class. It's up to the core dev who will be merging this.
I've added tests to both |
@@ -0,0 +1,2 @@ | |||
Add support for calling `next` with the mock resulting from |
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.
Sorry, I should have been more specific. This should probably be :func:`next`
or double backtick.
I'd suggest :func:`next`
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.
My bad. Fixed.
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.
Looking great IMHO! Thanks, great fix for mock_open
.
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.
LGTM
Thanks @Anvil for the PR 🌮🎉.. I'm working now to backport this PR to: 3.7. |
Thanks! |
GH-13521 is a backport of this pull request to the 3.7 branch. |
I've reported the issue on https://bugs.python.org/issue37008 and now I'm trying to bring a solution to this minor issue. I think it could be trivially backported to 3.7 branch. https://bugs.python.org/issue37008 (cherry picked from commit 394119a) Co-authored-by: Damien Nadé <Anvil@users.noreply.github.com>
I've reported the issue on https://bugs.python.org/issue37008 and now I'm trying to bring a solution to this minor issue. I think it could be trivially backported to 3.7 branch. https://bugs.python.org/issue37008 (cherry picked from commit 394119a) Co-authored-by: Damien Nadé <Anvil@users.noreply.github.com>
@cjw296 import mock
mocked_open = mock.mock_open(read_data='one\n')
f1 = mocked_open('a-name')
line1 = f1.__next__() mock with this patch applied to backport
|
Thank you everyone. |
@tirkarthi good point. |
@asvetlov Chris is maintaining the mock backport in PyPI that supports Python 2 and hence I was just wanted to make a note of this for Python 2. I am happy with this improving Python 3 :) |
I've reported the issue on https://bugs.python.org/issue37008 and now I'm trying to bring a solution to this minor issue.
I think it could be trivially backported to 3.7 branch.
https://bugs.python.org/issue37008