Title: unittest.mock.patch with autospec doesn't hold for classmethods nor staticmethods · Issue #102978 · python/cpython · GitHub
Open Graph Title: unittest.mock.patch with autospec doesn't hold for classmethods nor staticmethods · Issue #102978 · python/cpython
X Title: unittest.mock.patch with autospec doesn't hold for classmethods nor staticmethods · Issue #102978 · python/cpython
Description: Bug report Using patch with autospec=True does not work when a method is decorated with @classmethod or @staticmethod. The resulting mock can be called with any arguments without raising a TypeError. Example: from unittest.mock import pa...
Open Graph Description: Bug report Using patch with autospec=True does not work when a method is decorated with @classmethod or @staticmethod. The resulting mock can be called with any arguments without raising a TypeErro...
X Description: Bug report Using patch with autospec=True does not work when a method is decorated with @classmethod or @staticmethod. The resulting mock can be called with any arguments without raising a TypeErro...
Opengraph URL: https://github.com/python/cpython/issues/102978
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"unittest.mock.patch with autospec doesn't hold for classmethods nor staticmethods","articleBody":"# Bug report\r\n\r\nUsing `patch` with `autospec=True` does not work when a method is decorated with `@classmethod` or `@staticmethod`. The resulting mock can be called with any arguments without raising a `TypeError`.\r\n\r\nExample:\r\n```python\r\nfrom unittest.mock import patch\r\nimport pytest\r\n\r\nclass Foo:\r\n def foo(self):\r\n pass\r\n @staticmethod\r\n def bar():\r\n pass\r\n @classmethod\r\n def baz(cls):\r\n pass\r\n\r\n@pytest.mark.parametrize(\"method\", [\"foo\", \"bar\", \"baz\"])\r\ndef test_foo(method: str):\r\n with patch.object(Foo, method, autospec=True):\r\n getattr(Foo(), method)(5)\r\n```\r\n\r\nThe only subtest that fails is `foo`. The other two pass, even though they're clearly being called incorrectly.\r\n\r\nIf you prefer not to use `pytest` to demo/repro this:\r\n\r\n```python\r\nwith patch.object(Foo, \"foo\", autospec=True):\r\n try:\r\n Foo().foo(5)\r\n except TypeError:\r\n print(\"Correctly raises on foo\")\r\n else:\r\n print(\"Incorrectly does not raise with foo\")\r\n\r\nwith patch.object(Foo, \"bar\", autospec=True):\r\n try:\r\n Foo().bar(5)\r\n except TypeError:\r\n print(\"Correctly raises on bar\")\r\n else:\r\n print(\"Incorrectly does not raise with bar\")\r\n\r\nwith patch.object(Foo, \"baz\", autospec=True):\r\n try:\r\n Foo().baz(5)\r\n except TypeError:\r\n print(\"Correctly raises on baz\")\r\n else:\r\n print(\"Incorrectly does not raise with baz\")\r\n```\r\n\r\nThis has output:\r\n```\r\nCorrectly raises on foo\r\nIncorrectly does not raise with bar\r\nIncorrectly does not raise with baz\r\n```\r\n\r\n# Your environment\r\n\r\n- CPython versions tested on: 3.10, 3.11\r\n- Operating system and architecture: macOS 12.6 with Apple M1 chip\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-103228\n* gh-103499\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/rileyjohngibbs","@type":"Person","name":"rileyjohngibbs"},"datePublished":"2023-03-23T21:52:34.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/102978/cpython/issues/102978"}
| 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:520066c2-5b3d-3802-24de-8a7cab517c5b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CAF8:31FE0A:2D3D9:40036:696A0BB0 |
| html-safe-nonce | 4d780061f0be84b57ed3ef362d9b28e1312df49fb3f61c8414b5c345eddeadb7 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDQUY4OjMxRkUwQToyRDNEOTo0MDAzNjo2OTZBMEJCMCIsInZpc2l0b3JfaWQiOiI4MTY2MDMwMzc5NzM5Mzg0NzUyIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 4fcc4fd742a2abccaeb88a189dd0068a313db8872a55a80733630c12f7115095 |
| hovercard-subject-tag | issue:1638390110 |
| 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/102978/issue_layout |
| twitter:image | https://opengraph.githubassets.com/73969f8e827e2dc99eb0db19c9555e8324d06263e300a4605a511746268a7f13/python/cpython/issues/102978 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/73969f8e827e2dc99eb0db19c9555e8324d06263e300a4605a511746268a7f13/python/cpython/issues/102978 |
| og:image:alt | Bug report Using patch with autospec=True does not work when a method is decorated with @classmethod or @staticmethod. The resulting mock can be called with any arguments without raising a TypeErro... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | rileyjohngibbs |
| hostname | github.com |
| expected-hostname | github.com |
| None | 699227a00bbb7fe1eec276d2ae1c3a93068bc5ba483bd9dc4b2a27a8f4f2f595 |
| 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 | 7266b2d935baa1c6474b16dd9feaa5ca30607261 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width