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
Prevent prefix "called_" for methods on mock objects in safe mode #100690
Comments
|
Pinging @terryjreedy and @gpshead as requested in the discussion. |
|
To add a similar example Apache Airflow also fixed places where |
|
@tirkarthi Thanks, I'm going to add this name. |
|
My opinion: this can only happen in test suites that use |
|
I also think this would be a useful check to add. It's very common to use |
|
I categorized this request as a new feature, but the PR is marked bugfix with backports. Which should it be? (adding @cjw296 here who reviewed the PR) |
|
To me it also seemed like a new feature, which should probably go into |
|
I don't agree: there was code there to catch this kind of problem, but it was insufficient / buggy. I'm fine with backporting this to 3.10 and 3.11, but since the two of you feel otherwise, I'm fine to remove those labels. They can always be added back later. |
…en when using Mock (#100691) Mock objects which are not unsafe will now raise an AttributeError when accessing an attribute that matches the name of an assertion but without the prefix `assert_`, e.g. accessing `called_once` instead of `assert_called_once`. This is in addition to this already happening for accessing attributes with prefixes assert, assret, asert, aseert, and assrt.
test_clean_storage_unmount incorrectly used 'called_with' instead of 'assert_called_with' which just made a mock function call and didn't check or assert that the function was actually called. As a result this function didn't actually test anything, the expected parameters were not correct so they have been updated. An error to catch this accidental mistake was added in the recently released mock 5.0.1 (python/cpython#100690) AttributeError: 'called_with' is not a valid assertion. Use a spec for the mock if 'called_with' is meant to be an attribute. The mock library ships in python as unittest.mock but also publishes the latest code as the 'mock' python package so that older python versions can get the latest code. The stable branches currently install mock but the master branch does not, so this error does not actually appear on master but only the <=yoga stable branches currently. The test is however wrong either way. Change-Id: I3076778e8fc62c086651d29abb2c5a3d9921be97
* Update charm-cinder from branch 'master'
to ceafd9f67f3a5f404091d1f2fff7d2dcc9c71f47
- Fix missing assert_ prefix on called_with
test_clean_storage_unmount incorrectly used 'called_with' instead of
'assert_called_with' which just made a mock function call and didn't
check or assert that the function was actually called.
As a result this function didn't actually test anything, the expected
parameters were not correct so they have been updated.
An error to catch this accidental mistake was added in the recently
released mock 5.0.1 (python/cpython#100690)
AttributeError: 'called_with' is not a valid assertion.
Use a spec for the mock if 'called_with' is meant to be an attribute.
The mock library ships in python as unittest.mock but also publishes the
latest code as the 'mock' python package so that older python versions
can get the latest code. The stable branches currently install mock
but the master branch does not, so this error does not actually appear
on master but only the <=yoga stable branches currently. The test is
however wrong either way.
Change-Id: I3076778e8fc62c086651d29abb2c5a3d9921be97
test_clean_storage_unmount incorrectly used 'called_with' instead of 'assert_called_with' which just made a mock function call and didn't check or assert that the function was actually called. As a result this function didn't actually test anything, the expected parameters were not correct so they have been updated. An error to catch this accidental mistake was added in the recently released mock 5.0.1 (python/cpython#100690) AttributeError: 'called_with' is not a valid assertion. Use a spec for the mock if 'called_with' is meant to be an attribute. The mock library ships in python as unittest.mock but also publishes the latest code as the 'mock' python package so that older python versions can get the latest code. The stable branches currently install mock but the master branch does not, so this error does not actually appear on master but only the <=yoga stable branches currently. The test is however wrong either way. Change-Id: I3076778e8fc62c086651d29abb2c5a3d9921be97
test_clean_storage_unmount incorrectly used 'called_with' instead of 'assert_called_with' which just made a mock function call and didn't check or assert that the function was actually called. As a result this function didn't actually test anything, the expected parameters were not correct so they have been updated. An error to catch this accidental mistake was added in the recently released mock 5.0.1 (python/cpython#100690) AttributeError: 'called_with' is not a valid assertion. Use a spec for the mock if 'called_with' is meant to be an attribute. The mock library ships in python as unittest.mock but also publishes the latest code as the 'mock' python package so that older python versions can get the latest code. The stable branches currently install mock but the master branch does not, so this error does not actually appear on master but only the <=yoga stable branches currently. The test is however wrong either way. Change-Id: I3076778e8fc62c086651d29abb2c5a3d9921be97
test_clean_storage_unmount incorrectly used 'called_with' instead of 'assert_called_with' which just made a mock function call and didn't check or assert that the function was actually called. As a result this function didn't actually test anything, the expected parameters were not correct so they have been updated. An error to catch this accidental mistake was added in the recently released mock 5.0.1 (python/cpython#100690) AttributeError: 'called_with' is not a valid assertion. Use a spec for the mock if 'called_with' is meant to be an attribute. The mock library ships in python as unittest.mock but also publishes the latest code as the 'mock' python package so that older python versions can get the latest code. The stable branches currently install mock but the master branch does not, so this error does not actually appear on master but only the <=yoga stable branches currently. The test is however wrong either way. Change-Id: I3076778e8fc62c086651d29abb2c5a3d9921be97
test_clean_storage_unmount incorrectly used 'called_with' instead of 'assert_called_with' which just made a mock function call and didn't check or assert that the function was actually called. As a result this function didn't actually test anything, the expected parameters were not correct so they have been updated. An error to catch this accidental mistake was added in the recently released mock 5.0.1 (python/cpython#100690) AttributeError: 'called_with' is not a valid assertion. Use a spec for the mock if 'called_with' is meant to be an attribute. The mock library ships in python as unittest.mock but also publishes the latest code as the 'mock' python package so that older python versions can get the latest code. The stable branches currently install mock but the master branch does not, so this error does not actually appear on master but only the <=yoga stable branches currently. The test is however wrong either way. Change-Id: I3076778e8fc62c086651d29abb2c5a3d9921be97
test_clean_storage_unmount incorrectly used 'called_with' instead of 'assert_called_with' which just made a mock function call and didn't check or assert that the function was actually called. As a result this function didn't actually test anything, the expected parameters were not correct so they have been updated. An error to catch this accidental mistake was added in the recently released mock 5.0.1 (python/cpython#100690) AttributeError: 'called_with' is not a valid assertion. Use a spec for the mock if 'called_with' is meant to be an attribute. The mock library ships in python as unittest.mock but also publishes the latest code as the 'mock' python package so that older python versions can get the latest code. The stable branches currently install mock but the master branch does not, so this error does not actually appear on master but only the <=yoga stable branches currently. The test is however wrong either way. Change-Id: I3076778e8fc62c086651d29abb2c5a3d9921be97
test_clean_storage_unmount incorrectly used 'called_with' instead of 'assert_called_with' which just made a mock function call and didn't check or assert that the function was actually called. As a result this function didn't actually test anything, the expected parameters were not correct so they have been updated. An error to catch this accidental mistake was added in the recently released mock 5.0.1 (python/cpython#100690) AttributeError: 'called_with' is not a valid assertion. Use a spec for the mock if 'called_with' is meant to be an attribute. The mock library ships in python as unittest.mock but also publishes the latest code as the 'mock' python package so that older python versions can get the latest code. The stable branches currently install mock but the master branch does not, so this error does not actually appear on master but only the <=yoga stable branches currently. The test is however wrong either way. Change-Id: I3076778e8fc62c086651d29abb2c5a3d9921be97
Prevent prefix "called_" for methods on mock objects in safe mode
Pitch
In safe mode, there's already support for catching typos for accessing the assertion methods:
Given you have a valid assertion to check whether a mocked function has been called:
If you now want to check the arguments, and do not pay full attention, you can end up with a tautology like
The issue:
mock_foo.called_once_withis not a valid (assertion) method and therefore an instance of mock.Mock is returned. Because instances of mock.Mock evaluate to true, the assertion is equivalent toassert True.Like with the preventing the call of methods that start with
assertandassret(issue 21238) and also disallowing the typosasert,aseert, andassrt(#23165), this error will not cause a test failure.Analyzing public repositories on github.com, the Python standard library (thanks @terryjreedy for fixing it in #100647), and our internal code base revealed what seems to be a common source of errors. In our own code base, we have had more than 500 of these issues. More than 50% of those failed after fixing the assertion call, which could potentially have covered existing bugs by relying on bad tests.
Previous discussion
https://discuss.python.org/t/include-prefix-called-in-list-of-forbidden-method-prefixes-for-mock-objects-in-unsafe-mode/22249/4
Linked PRs
ATTRIB_DENY_LISTand make it immutable #100819The text was updated successfully, but these errors were encountered: