Title: Autospeccing with `unittest.mock` is less robust with PEP 649 · Issue #145754 · python/cpython · GitHub
Open Graph Title: Autospeccing with `unittest.mock` is less robust with PEP 649 · Issue #145754 · python/cpython
X Title: Autospeccing with `unittest.mock` is less robust with PEP 649 · Issue #145754 · python/cpython
Description: Bug report Bug description: from typing import TYPE_CHECKING from unittest.mock import create_autospec if TYPE_CHECKING: from foo import Foo def bar(x: Foo) -> None: pass create_autospec(bar) This will result in NameError: name 'Foo' is ...
Open Graph Description: Bug report Bug description: from typing import TYPE_CHECKING from unittest.mock import create_autospec if TYPE_CHECKING: from foo import Foo def bar(x: Foo) -> None: pass create_autospec(bar) This ...
X Description: Bug report Bug description: from typing import TYPE_CHECKING from unittest.mock import create_autospec if TYPE_CHECKING: from foo import Foo def bar(x: Foo) -> None: pass create_autospec(bar) Th...
Opengraph URL: https://github.com/python/cpython/issues/145754
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Autospeccing with `unittest.mock` is less robust with PEP 649","articleBody":"# Bug report\n\n### Bug description:\n\n```python\nfrom typing import TYPE_CHECKING\nfrom unittest.mock import create_autospec\n\nif TYPE_CHECKING:\n from foo import Foo\n\ndef bar(x: Foo) -\u003e None:\n pass\n\ncreate_autospec(bar)\n```\n\nThis will result in `NameError: name 'Foo' is not defined` because `_get_signature_object` in `unittest.mock` calls `inspect.signature` without explicitly passing `annotation_format`, so it defaults to `Format.VALUE`.\n\nSince `create_autospec` really shouldn't care whether or not the annotations can be evaluated at runtime on the given object, I would suggest switching over to `Format.FORWARDREF` or `Format.STRING`, so autospeccing continues to work in Python 3.14+ for people that used to rely on `from __future__ import annotations` and now want to switch to PEP 649 semantics, without needing to change their code.\n\n### CPython versions tested on:\n\n3.14\n\n### Operating systems tested on:\n\nLinux\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-145756\n* gh-146191\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/Daverball","@type":"Person","name":"Daverball"},"datePublished":"2026-03-10T14:39:22.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/145754/cpython/issues/145754"}
| 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:bf773308-c36f-8800-be5c-1aa3c8a55d56 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C4A6:3F60C6:7073942:9E14840:6A5074AD |
| html-safe-nonce | c737638febc4a53c00e8be0b468b2fc8946b1d9f2cae407ce10e041dec742c58 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNEE2OjNGNjBDNjo3MDczOTQyOjlFMTQ4NDA6NkE1MDc0QUQiLCJ2aXNpdG9yX2lkIjoiMTk0NjAzMTg4MDk2MTgxNTcyNiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 67c95901a2d451c96e455bf2e4278f171c3f049704ed4cb2e1a971275f9345f2 |
| hovercard-subject-tag | issue:4052263066 |
| 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/145754/issue_layout |
| twitter:image | https://opengraph.githubassets.com/5d6f7874fe2b5f4ca727b8001ad6c0a404bcb49161eec3a48b5c6ced19fbbb66/python/cpython/issues/145754 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/5d6f7874fe2b5f4ca727b8001ad6c0a404bcb49161eec3a48b5c6ced19fbbb66/python/cpython/issues/145754 |
| og:image:alt | Bug report Bug description: from typing import TYPE_CHECKING from unittest.mock import create_autospec if TYPE_CHECKING: from foo import Foo def bar(x: Foo) -> None: pass create_autospec(bar) This ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Daverball |
| hostname | github.com |
| expected-hostname | github.com |
| None | d6dc8294eb500fa36bbc57472d61fe87c522f9c3c1d64f70f4926f66a66a7efb |
| 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 | 7ac0ad2f2c7e4b9056617355fd9e33e22b0c8df9 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width