-
-
Notifications
You must be signed in to change notification settings - Fork 34.3k
gh-91803: Mock - fix error when using autospec methods with seal #92213
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
Changes from 6 commits
de27032
96c516a
0a4a359
7bda679
8f81849
1e1f45d
6992de7
36a47d0
dfe90da
b1b12ab
c535efb
dbb8bf9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -199,6 +199,7 @@ def ban(self): | |
| self.assertIsInstance(foo.Baz, mock.MagicMock) | ||
| self.assertIsInstance(foo.Baz.baz, mock.NonCallableMagicMock) | ||
| self.assertIsInstance(foo.Baz.ban, mock.MagicMock) | ||
| self.assertIsInstance(foo.bar2(), mock.Mock) | ||
cjw296 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| self.assertEqual(foo.bar1(), 'a') | ||
| foo.bar1.return_value = 'new_a' | ||
|
|
@@ -211,8 +212,6 @@ def ban(self): | |
| foo.foo() | ||
| with self.assertRaises(AttributeError): | ||
| foo.bar = 1 | ||
| with self.assertRaises(AttributeError): | ||
| foo.bar2() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think Nikita is largely on a break from open-source stuff right now FYI
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel this is an incorrect assert since |
||
|
|
||
| foo.bar2.return_value = 'bar2' | ||
| self.assertEqual(foo.bar2(), 'bar2') | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Fix an error when using a method of objects mocked with | ||
| :func:`unittest.mock.create_autospec` after it was sealed with | ||
| :func:`unittest.mock.seal` function. |
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.
This could use the
child_klassto construct the mock object instead ofMock. This can break code that depended on the return value being an instance ofchild_klass. Example case as below :Python 3.10
With patch
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.
That makes sense to me, I used Mock originally because I wasn't sure if MagicMock might "evade"
seal()if set explicitly, but I tested it and it does not:==> AttributeError: mock.foo().x
I've pushed the update to use
child_klass()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.
@akulakov - rather than just manually testing this, please can you add a test to the test suite to ensure it doesn't change in the future?
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.
@cjw296 that makes sense, I've expanded the unit test - thanks for noting this.