Skip to content

lazy __signature__ property doesn't work well with typing.TYPE_CHECKING #636

@yiyunliu

Description

@yiyunliu

I found this issue when debugging the failed CI for #632. A simple way to reproduce this issue is to retrieve the signature for completion function from the llm handler:

from effectful.handlers.llm.completions import completion
completion.__signature__

and it would complain about 'ClientSession' not being defined.

After a quick git bisect, I found that the issue was introduced by the commit fa2e420, which makes signature a lazy property. The ClientSession forward reference comes from litellm.completion. ClientSession refers to a class from aiohttp and the forward reference is there to delay the import of aiohttp. Here's a smaller, self-contained example demonstrating what's going on.

#a.py 
from typing import TYPE_CHECKING
import b
from effectful.ops.syntax import defop

if TYPE_CHECKING:
    from b import A

@defop
def foo(a : 'A') -> int:
    return b.bar(a)

print(foo.__signature__)
#b.py
class A:
    pass

def bar(a : A):
    return 0

Here, mypy is happy with both files but __signature__ fails to resolve 'A' because the TYPE_CHECKING block can't be entered when effectful is running.The minimal example could have been made more realistic by adding another file in between so a.py doesn't have to import a potentially heavy library b at all until foo is called.

I'm not sure what the intended behavior should be here or how severe this issue is. In #632, implementing the completion operation triggers the error in the CI because that causes sphinx to inspect the signature of completion.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions