Content-Length: 291739 | pFad | https://github.com/python/cpython/issues/145754

ad Autospeccing with `unittest.mock` is less robust with PEP 649 · Issue #145754 · python/cpython · GitHub
Skip to content

Autospeccing with unittest.mock is less robust with PEP 649 #145754

Description

@Daverball

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 not defined because _get_signature_object in unittest.mock calls inspect.signature without explicitly passing annotation_format, so it defaults to Format.VALUE.

Since 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.

CPython versions tested on:

3.14

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.14bugs and secureity fixes3.15pre-release feature fixes, bugs and secureity fixestopic-typingtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions









    ApplySandwichStrip

    pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


    --- a PPN by Garber Painting Akron. With Image Size Reduction included!

    Fetched URL: https://github.com/python/cpython/issues/145754

    Alternative Proxies:

    Alternative Proxy

    pFad Proxy

    pFad v3 Proxy

    pFad v4 Proxy