Skip to content

Conversation

@Otto-AA
Copy link
Collaborator

@Otto-AA Otto-AA commented Jan 31, 2026

Draft for fixing #463

Python 3.14 supports deferred annotation loading. However, the inspect.signature raises an Error when trying to compute non-existing lazy-loaded annotations (which happens with if TYPE_CHECKING imports)

def foo(x: HelloWorld): pass
def bar(x: dict[str, int]): pass

# that's what we currently do
mutated_foo.__signature__ = signature(foo) # NameError: name 'HelloWorld' is not defined

# we can work around this by loading the annotation as a string, instead of evaluating it
from inspect import signature, Format

# using Format.STRING successfuly get's the format even if HelloWorld does not exist
print(signature(foo, annotation_format=Format.STRING)) # (x: 'HelloWorld')
# but it also stringifies the annotation, which we previously did not do
print(signature(bar, annotation_format=Format.STRING)) # (x: 'dict[str, int]')
print(signature(bar)) # (x: dict[str, int])

I guess changing from dict[str, int] to 'dict[str, int]' is no problem.

But we likely also want to set the mutated_foo.__annotations__ attribute?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants