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-37828: Fix default mock_name in unittest.mock.assert_called error #16166

Merged
merged 6 commits into from
Sep 17, 2019

Conversation

categulario
Copy link
Contributor

@categulario categulario commented Sep 15, 2019

I used parenthesis to change precedence so the correct message is displayed. This is my very fist contribution to python (:

https://bugs.python.org/issue37828

@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 this contribution by verifying everyone involved has signed the PSF contributor agreement (CLA).

CLA Missing

Our records indicate the following people have not signed the CLA:

@categulario

For legal reasons we need all the people listed to sign the CLA 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 the contribution, we look forward to reviewing it!

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.

Quite a nice fix @categulario!

Would be great if you can add a test to make sure this does not happen again. You can do so by adding a test like

def test_assert_called_exception_message():
  with self.assertRaisesRegexp(AssertionError, "Expected 'mock' to have been called"):
      Mock().assert_called()
  with self.assertRaisesRegexp(AssertionError, "Expected 'test_name' to have been called"):
      Mock(name="test_name").assert_called()

Also don't forget to go through the process for the CLA so the contribution can be accepted.

You should also include a news entry, see here.

This patch should be probably backported as well (for a core dev to mark as such).

categulario and others added 3 commits September 15, 2019 16:09
* add missing `self` parameter to test case
* use `assertRaisesRegex` instead of `assertRaisesRegexp`
@categulario
Copy link
Contributor Author

Thanks for the review @mariocj89!

  • I added the test in a follow up commit
  • I signed it the CLA like an hour ago, I'm told it takes a day to settle
  • I just did the blurb thing
  • It works in my computer haha

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.

I've made some suggestions over the what's new but the code looks good to me!

@@ -396,6 +396,13 @@ def _check(mock):
_check(mock)


def test_assert_called_exception_message(self):
with self.assertRaisesRegex(AssertionError, "Expected 'mock' to have been called"):
Copy link
Member

Choose a reason for hiding this comment

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

Nit : Please follow PEP 8 guidelines to ensure the line length is not more than 80 characters here and in below. You can probably have it as a format string with message and fill in the mock name like below.

message = "Expected '{0}' to have been called"

with self.assertRaisesRegex(AssertionError, message.format('mock')):
    Mock().assert_called()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! What is the preferred way to run pep8 agains the modified code?

Copy link
Member

Choose a reason for hiding this comment

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

I only do manual checking. There are some parts of the code that predates PEP8 and PEP8 is enforced only for new code so using pep8 libraries on whole file might give some false information.

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.

Thanks @categulario !

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.

LGTM. Thanks :)

@mariocj89
Copy link
Contributor

ping @pablogsal

Copy link
Member

@pablogsal pablogsal left a comment

Choose a reason for hiding this comment

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

LGTM

Thanks @tirkarthi and @mariocj89 for the reviews!

@miss-islington
Copy link
Contributor

Thanks @categulario for the PR, and @pablogsal for merging it 🌮🎉.. I'm working now to backport this PR to: 3.8.
🐍🍒⛏🤖

@miss-islington
Copy link
Contributor

I'm having trouble backporting to 3.8. Reason: 'Error 110 while writing to socket. Connection timed out.'. Please retry by removing and re-adding the needs backport to 3.8 label.

@miss-islington
Copy link
Contributor

Thanks @categulario for the PR, and @pablogsal for merging it 🌮🎉.. I'm working now to backport this PR to: 3.8.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Sep 17, 2019
…pythonGH-16166)

In the format string for assert_called the evaluation order is incorrect and hence for mock's without name, 'None' is printed whereas it should be 'mock' like for other messages. The error message is ("Expected '%s' to have been called." % self._mock_name or 'mock').
(cherry picked from commit 5f5f11f)

Co-authored-by: Abraham Toriz Cruz <awonderfulcode@gmail.com>
@bedevere-bot
Copy link

GH-16229 is a backport of this pull request to the 3.8 branch.

miss-islington added a commit that referenced this pull request Sep 17, 2019
…GH-16166)

In the format string for assert_called the evaluation order is incorrect and hence for mock's without name, 'None' is printed whereas it should be 'mock' like for other messages. The error message is ("Expected '%s' to have been called." % self._mock_name or 'mock').
(cherry picked from commit 5f5f11f)

Co-authored-by: Abraham Toriz Cruz <awonderfulcode@gmail.com>
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.

7 participants