Skip to content
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

Merged
merged 7 commits into from May 23, 2019
Merged

bpo-37008: make mock_open handle able to honor next() #13492

merged 7 commits into from May 23, 2019

Conversation

Anvil
Copy link
Contributor

@Anvil Anvil commented May 22, 2019

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

@the-knights-who-say-ni
Copy link

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
before our records are updated.

You can check yourself to see if the CLA has been received.

Thanks again for your contribution, we look forward to reviewing it!

@mariocj89
Copy link
Contributor

mariocj89 commented May 22, 2019

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 testmock.py file.

You also need to add a news entry for this change, something in the lines of:

Add support for calling next with the mock resulting from :func:unittest.mock.mock_open.

Great fix by the way! :)

Copy link
Member

@tirkarthi tirkarthi left a 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

@Anvil
Copy link
Contributor Author

Anvil commented May 22, 2019

@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.

@Anvil
Copy link
Contributor Author

Anvil commented May 22, 2019

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

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)

Copy link
Member

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

Copy link
Contributor Author

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.

Copy link
Contributor Author

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() ?

Copy link
Member

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.

Copy link
Contributor Author

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. :)

Copy link
Member

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.

@Anvil
Copy link
Contributor Author

Anvil commented May 22, 2019

I've added tests to both testmock.py and testwith.py.

@@ -0,0 +1,2 @@
Add support for calling `next` with the mock resulting from
Copy link
Contributor

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`

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My bad. Fixed.

Copy link
Contributor

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

Copy link
Contributor

@asvetlov asvetlov left a comment

Choose a reason for hiding this comment

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

LGTM

@miss-islington
Copy link
Contributor

Thanks @Anvil for the PR 🌮🎉.. I'm working now to backport this PR to: 3.7.
🐍🍒⛏🤖

@asvetlov
Copy link
Contributor

Thanks!

@bedevere-bot
Copy link

GH-13521 is a backport of this pull request to the 3.7 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request May 23, 2019
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>
miss-islington added a commit that referenced this pull request May 23, 2019
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>
@tirkarthi
Copy link
Member

@cjw296 __next__ doesn't seem to exist on python 2 and this might need to be python 3 only code during backport. Perhaps the test could be marked as python 3 only such in the backport test suite.

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

python /tmp/foo.py
Traceback (most recent call last):
  File "/tmp/foo.py", line 3, in <module>
    mocked_open = mock.mock_open(read_data='one\n')
  File "/Users/karthikeyansingaravelan/stuff/python/27-venv/lib/python2.7/site-packages/mock/mock.py", line 2567, in mock_open
    handle.__next__.side_effect = _next_side_effect
  File "/Users/karthikeyansingaravelan/stuff/python/27-venv/lib/python2.7/site-packages/mock/mock.py", line 695, in __getattr__
    raise AttributeError("Mock object has no attribute %r" % name)
AttributeError: Mock object has no attribute '__next__'

@Anvil Anvil deleted the next_mock_open branch May 23, 2019 10:47
@Anvil
Copy link
Contributor Author

Anvil commented May 23, 2019

Thank you everyone.

@asvetlov
Copy link
Contributor

@tirkarthi good point.
I think this is not a reason to reject the PR though :)

@tirkarthi
Copy link
Member

@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 :)

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

Successfully merging this pull request may close these issues.

None yet

7 participants