-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
bpo-32092: Mock patch fix autospec #4476
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
base: main
Are you sure you want to change the base?
Conversation
d59fe18
to
797f190
Compare
Or, just merge the 2 and close out the older one? |
Hm, tbh, I'd like to keep it simple, since they're both addressing different issues. Anyways, I guess I'll just separate this patch from the other. We'll deal with the merge conflict when it happens. |
Actually, nevermind, the unit tests are not passing without the 1st patch. :) |
alright then. |
Mock can accept an spec object / class as argument, making sure that accessing attributes that do not exist in the spec will cause an AttributeError to be raised, but there is no guarantee that the spec's methods signatures are respected in any way. This creates the possibility to have faulty code with passing unittests and assertions. Example: from unittest import mock class Something(object): def foo(self, a, b, c, d): pass m = mock.Mock(spec=Something) m.foo() Adds the autospec argument to Mock, and its mock_add_spec method. Passes the spec's attribute with the same name to the child mock (spec-ing the child), if the mock's autospec is True. Sets _mock_check_sig if the given spec is callable. Adds unit tests to validate the fact that the autospec method signatures are respected.
Currently, when patching methods with autospec, their self / cls arguments are not consumed, causing call asserts to fail (they expect an instance / class reference as the first argument). Example: from unittest import mock class Something(object): def foo(self, a, b, c, d): pass patcher = mock.patch.object(Something, 'foo', autospec=True) patcher.start() s = Something() s.foo()
797f190
to
60bbe51
Compare
@claudiubelu, please resolve the merge conflicts. Thank you! |
@claudiubelu - it looks like #1982 is with @voidspace and this PR depends on it? Would it be okay to close that one until #1982 is resolved? |
This PR is stale because it has been open for 30 days with no activity. |
bpo-32092: Fixes mock.patch autospec self / cls argument consumption issue
https://bugs.python.org/issue32092