Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Object in localns but signature of inner function not resolved #93

Closed
lucyleeow opened this issue Nov 18, 2023 · 4 comments
Closed

Object in localns but signature of inner function not resolved #93

lucyleeow opened this issue Nov 18, 2023 · 4 comments

Comments

@lucyleeow
Copy link
Contributor

Description

When resolving the signature of a nested inner function, importing a signature object globally works but importing inside the outer function does not (even though this results in the object being in the local namespace of the outer function).

What I Did

from __future__ import annotations

from in_n_out import type_resolved_signature
from app_model import Action


def outer():
    from magicgui.widgets import FunctionGui
    def myfun(a: FunctionGui):
        if isinstance(a, FunctionGui):
            return a

    print(f'locals {locals()}')

    return Action(id='test', title='title', callback=myfun)

act = outer()
a = type_resolved_signature(act.callback, raise_unresolved_optional_args=False, raise_unresolved_required_args=False)
print(a)

gives:

locals {'myfun': <function outer.<locals>.myfun at 0x7f22eccf6280>, 'FunctionGui': <class 'magicgui.widgets._function_gui.FunctionGui'>}
(a: 'FunctionGui')

I have not yet looked into why this is happening but you may know already.

Hopefully I got this right and you would expect this to resolve.

@tlambert03
Copy link
Member

Actually I would not expect that to resolve. You've hit on one of the big issues in usage of type annotations at runtime.

This is a long and complicated topic with lots written elsewhere, so I'll just link to relevant peps and you should search them:

Long story short, that usage just won't be supported. You would need to not use __future__.annotations or import at the global level

@tlambert03
Copy link
Member

You can also read through pydantic/pydantic#2678

Point is, what we're trying to do here (using types at runtime) does have some limitations and caveats, and does kinda rely on a little "participation/cooperation" on the part of the end user, just as it does in pydantic

@lucyleeow
Copy link
Contributor Author

Okay interesting, thanks for the info, I'll close this!

@lucyleeow
Copy link
Contributor Author

Thinking about it more, of course it wouldn't work, the outer function is defined but never called until runtime

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

No branches or pull requests

2 participants