Title: Prevent prefix "called_" for methods on mock objects in safe mode · Issue #100690 · python/cpython · GitHub
Open Graph Title: Prevent prefix "called_" for methods on mock objects in safe mode · Issue #100690 · python/cpython
X Title: Prevent prefix "called_" for methods on mock objects in safe mode · Issue #100690 · python/cpython
Description: 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: By default, accessing any attribute whose name starts with assert, assr...
Open Graph Description: 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: By default, accessing any attri...
X Description: 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: By default, acces...
Opengraph URL: https://github.com/python/cpython/issues/100690
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Prevent prefix \"called_\" for methods on mock objects in safe mode","articleBody":"# Prevent prefix \"called_\" for methods on mock objects in safe mode\r\n\r\n# Pitch\r\n\r\nIn safe mode, there's already support for catching typos for accessing the assertion methods:\r\n\r\n\u003e By default, accessing any attribute whose name starts with assert, assret, asert, aseert or assrt will raise an AttributeError.\r\n\r\nGiven you have a valid assertion to check whether a mocked function has been called:\r\n```\r\nassert mock_foo.called\r\n```\r\n\r\nIf you now want to check the arguments, and do not pay full attention, you can end up with a tautology like\r\n\r\n```\r\nassert mock_foo.called_once_with(param=\"test\")\r\n```\r\n\r\nThe 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 to `assert True`.\r\nLike with the preventing the call of methods that start with `assert` and `assret` ([issue 21238](https://bugs.python.org/issue21238)) and also disallowing the typos `asert`, `aseert`, and `assrt` (https://github.com/python/cpython/pull/23165), this error will not cause a test failure.\r\n\r\nAnalyzing public repositories on github.com, the Python standard library (thanks @terryjreedy for fixing it in https://github.com/python/cpython/pull/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.\r\n\r\n# Previous discussion\r\n\r\nhttps://discuss.python.org/t/include-prefix-called-in-list-of-forbidden-method-prefixes-for-mock-objects-in-unsafe-mode/22249/4\r\n\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-100691\n* gh-100819\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/cklein","@type":"Person","name":"cklein"},"datePublished":"2023-01-02T15:51:01.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":8},"url":"https://github.com/100690/cpython/issues/100690"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:56b37d0e-a617-c738-9705-38a473312980 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C7FE:13A5D2:36A6A4:484012:696B67AC |
| html-safe-nonce | c4c22e6df0392d5f25ff3a3389f407335e8a3d6432effa460f5bc127ff5cd2ac |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDN0ZFOjEzQTVEMjozNkE2QTQ6NDg0MDEyOjY5NkI2N0FDIiwidmlzaXRvcl9pZCI6IjMyMjU3NzI3MDU5Njg3NzcxMzIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 97160fe85ca2f248766f84e7b87cc3049c8e9518edbb0e57add4bb7b2f65ca77 |
| hovercard-subject-tag | issue:1516523659 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/100690/issue_layout |
| twitter:image | https://opengraph.githubassets.com/ad89bca543c63a820f0d308c694a8b515dd789fb0e4fde81da5ce99a1140fe93/python/cpython/issues/100690 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/ad89bca543c63a820f0d308c694a8b515dd789fb0e4fde81da5ce99a1140fe93/python/cpython/issues/100690 |
| og:image:alt | 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: By default, accessing any attri... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | cklein |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5f99f7c1d70f01da5b93e5ca90303359738944d8ab470e396496262c66e60b8d |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 82560a55c6b2054555076f46e683151ee28a19bc |
| ui-target | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width