-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
bpo-30587: Adds signature checking for mock autospec object method calls #1982
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
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). Unfortunately we couldn't find an account corresponding to your GitHub username on bugs.python.org (b.p.o) to verify you have signed the CLA (this might be simply due to a missing "GitHub Name" entry in your b.p.o account settings). This is necessary for legal reasons before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue. Thanks again to your contribution and we look forward to looking at it! |
|
Thanks for the PR. This doesn't seem to be a trivial change. Can you please open an issue at https://bugs.python.org, and then reference the issue number in the PR title. |
|
Hello. I actually reported a bug for this, but I didn't reference it in the commit message. Will do. |
654dfca to
92f4929
Compare
90d48a0 to
51a9270
Compare
|
@Mariatta Hi, can you take a look again? :) |
51a9270 to
db74a6d
Compare
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.
db74a6d to
13f993d
Compare
|
I'm taking a look at this. |
|
I'd like spec to have signature validation. I don't think the use case for "attribute validation only" (current spec behaviour) is very strong and I'd rather not add new keywords. The mock API is already too complex. I'll take the existing PR and modify it to use spec instead of adding the new autospec. |
|
@voidspace Any status update on this change? |
|
This PR is stale because it has been open for 30 days with no activity. |
|
I have a small issue with _get_class method. It doesn't seem to be part of any module/standard lib. |
|
This PR is stale because it has been open for 30 days with no activity. |
|
This PR is stale because it has been open for 30 days with no activity. |
|
@voidspace, would you mind to return to this PR? It would be great to have it finally merged in a worthy state after these three tumbly years. :) |
|
This PR is stale because it has been open for 30 days with no activity. |
|
@sobolevn (as a committer into |
|
This PR is stale because it has been open for 30 days with no activity. |
@arhadthedev - I don't see that this feedback was addressed, and a few others appear to have raised issues in follow up. It sounds like there's a good idea lurking here, but more discussion needs to happen on #74772 before a new PR is opened. |
Hello, I haven't followed this in quite a while, and I've remembered this recently when I hit this issue again. I technically agree with you, adding a new keyword may be a bit undesirable since it's already quite complex, but I'd have to say that changing the So, it's not really a new keyword, this PR would just make the API more uniform, bringing the I think we can reopen the PR. I can rebase it (though, I think it might be easier to cherry-pick the commit onto the current
|
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 unit tests and assertions.
Example:
Adds the
autospecargument to Mock, and itsmock_add_specmethod.Passes the
spec's attribute with the same name to the child mock (spec-ing the child), if the mock'sautospecisTrue.Sets
_mock_check_sigif the given spec is callable.Adds unit tests to validate the fact that the autospec method signatures are respected.
https://bugs.python.org/issue30587