-
-
Notifications
You must be signed in to change notification settings - Fork 31.1k
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
As per python/cpython#100690 , these assertions were a no-op and Python 3.12 guards against this.
Adds explicit support for Python 3.12. These are the minimal changes I needed for `pytest` to pass on 3.12 and macOS: * Replaces deprecated [`datetime.datetime.utcnow`](https://docs.python.org/3/library/datetime.html#datetime.datetime.utcnow) * Replaces `called_once_with` with `assert_called_once_with`, which now [raises](python/cpython#100690) `AttributeError: 'called_once_with' is not a valid assertion` * Bumps `pytest` due to `DeprecationWarning: ast.Str is deprecated and will be removed in Python 3.14; use ast.Constant instead` pytest-dev/pytest#10977 * Refreshes the lockfile depends on #1393
Python v3.12 and later raise an exception for this line with the following message: """ AttributeError: 'called_once_with' is not a valid assertion. Use a spec for the mock if 'called_once_with' is meant to be an attribute.. Did you mean: 'assert_called_once_with'? """ A bit of research returned commit 1d4d677d1c90 ("gh-100690: Raise an AttributeError when the assert_ prefix is forgotten when using Mock (#100691)") from CPython git history, which in turns points at python/cpython#100690. This GitHub issue mentions that called_once_with returns a Mock object instead of doing a proper assertion. This test is basically a no-op. Therefore, let's fix the test by doing a proper assertion. Fixes: 94d64c8 ("test: image: improve code coverage by testing BaseImage") Signed-off-by: Quentin Schulz <[email protected]>
Python v3.12 and later raise an exception for this line with the following message: """ AttributeError: 'called_once_with' is not a valid assertion. Use a spec for the mock if 'called_once_with' is meant to be an attribute.. Did you mean: 'assert_called_once_with'? """ A bit of research returned commit 1d4d677d1c90 ("gh-100690: Raise an AttributeError when the assert_ prefix is forgotten when using Mock (#100691)") from CPython git history, which in turns points at python/cpython#100690. This GitHub issue mentions that called_once_with returns a Mock object instead of doing a proper assertion. This test is basically a no-op. Therefore, let's fix the test by doing a proper assertion. Fixes: 94d64c8 ("test: image: improve code coverage by testing BaseImage") Signed-off-by: Quentin Schulz <[email protected]>
Use assert_called_with (python/cpython#100690)
Prevent prefix "called_" for methods on mock objects in safe mode. See python/cpython#100690 Signed-off-by: Silvano Cirujano Cuesta <[email protected]>
Prevent prefix "called_" for methods on mock objects in safe mode. See python/cpython#100690 Signed-off-by: Silvano Cirujano Cuesta <[email protected]>
Prevent prefix "called_" for methods on mock objects in safe mode. See python/cpython#100690 Signed-off-by: Silvano Cirujano Cuesta <[email protected]>
Subprocesses have changed where exception messages are stored. From stdout exception messages are moved to stderr and we need to adapt. Additionally, we use `called_once`` for one test for a mock where we need to migrate to `assert_called_once_with` as for python 12 called_once is gone: python/cpython#100690 Signed-off-by: Martin Vrachev <[email protected]>
* Fix unit tests caused by internal python changes Subprocesses have changed where exception messages are stored. From stdout exception messages are moved to stderr and we need to adapt. Additionally, we use `called_once`` for one test for a mock where we need to migrate to `assert_called_once_with` as for python 12 called_once is gone: python/cpython#100690 Signed-off-by: Martin Vrachev <[email protected]> * Fix "ValueError: stderr not separately captured" This error is given by subprocess when we want to access the stderr of the test_result object returned from click.invoke. This is fixed by using "mix_stderr" on CliRunner level. https://click.palletsprojects.com/en/latest/api/#testing Signed-off-by: Martin Vrachev <[email protected]> --------- Signed-off-by: Martin Vrachev <[email protected]>
# What this PR does - bumps `uwsgi` to latest version (`2.0.26`), which unblocks us from bumping Python to 3.12 - bumps Python to 3.12.3 - refactor the Snyk GitHub Actions workflow to use the composable actions for installed frontend and backend dependencies - fixes several `AttributeError`s in our tests that went from a warning to an error in Python 3.12 (see python/cpython#100690) # Which issue(s) this PR closes Closes #4358 Closes #4387
`called_with` was likely not the right call to make, since this would return a mock instance. Instead, use `assert_called_with`. See: python/cpython#100690
Python 3.12 has removed long-deprecated unittest features [1], therefore some of the existing unit test fail when run on Python 3.12. This patch replaces the 'assertEquals' with 'assertTrue' or 'assertFalse', depending on the usage. This patch also fixes the 'test_amqp_changed' and 'test_amqp_departed' test methods which improperly called 'called_with' which is no longer a valid assertion method [2]. And in fact on Python < 3.12 the 'test_amqp_changed' and 'test_amqp_departed' pass their assertions, when they actually should fail due to the out-of-sync with 'amqp-relation-changed' and 'amqp-relation-departed' hook code. In this case the 'called_with' is replaced with 'assert_called_once'. [1] https://docs.python.org/3.12/whatsnew/3.12.html#id3 [2] python/cpython#100690 Change-Id: I90b400f6bafe8f03f04f991d9fe58635d19b8b2e Signed-off-by: Marcin Wilk <[email protected]>
* Update charm-neutron-openvswitch from branch 'master' to 63465640fef8d545e8038f1960c17da0201bc03d - Fix Python 3.12 unittest compatibility Python 3.12 has removed long-deprecated unittest features [1], therefore some of the existing unit test fail when run on Python 3.12. This patch replaces the 'assertEquals' with 'assertTrue' or 'assertFalse', depending on the usage. This patch also fixes the 'test_amqp_changed' and 'test_amqp_departed' test methods which improperly called 'called_with' which is no longer a valid assertion method [2]. And in fact on Python < 3.12 the 'test_amqp_changed' and 'test_amqp_departed' pass their assertions, when they actually should fail due to the out-of-sync with 'amqp-relation-changed' and 'amqp-relation-departed' hook code. In this case the 'called_with' is replaced with 'assert_called_once'. [1] https://docs.python.org/3.12/whatsnew/3.12.html#id3 [2] python/cpython#100690 Change-Id: I90b400f6bafe8f03f04f991d9fe58635d19b8b2e Signed-off-by: Marcin Wilk <[email protected]>
temp_queue downloads were reverted in 32.0.2, and while the test was partially kept, it didn't do anything, which was made obvious by python/cpython#100690, when the call to `called_once_with` started raising.
temp_queue downloads were reverted in 32.0.2, and while the test was partially kept, it didn't do anything, which was made obvious by python/cpython#100690, when the call to `called_once_with` started raising.
temp_queue downloads were reverted in 32.0.2, and while the test was partially kept, it didn't do anything, which was made obvious by python/cpython#100690, when the call to `called_once_with` started raising.
…ert mock_foo.called_once_with(...)" Refer to this: python/cpython#100690
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_with
is 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
assert
andassret
(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_LIST
and make it immutable #100819The text was updated successfully, but these errors were encountered: