guidance icon indicating copy to clipboard operation
guidance copied to clipboard

Type Checker yells at me when I use guidance with arguments

Open ibehnam opened this issue 2 years ago • 2 comments

The bug The message:

Module is not callablePyright[reportGeneralTypeIssues](https://github.com/microsoft/pyright/blob/main/docs/configuration.md#reportGeneralTypeIssues)
module guidance
(module) guidance

Example code used:

program = guidance("""Tweak this proverb to apply to model instructions instead.
                   
{{proverb}}
- {{book}} {{chapter}}:{{verse}}

UPDATED
Where there is no guidance{{gen 'rewrite' stop="\\n-"}}
- GPT {{gen 'chapter'}}:{{gen 'verse'}}""")

ibehnam avatar May 23 '23 22:05 ibehnam

Interesting. In this case that is not correct since the module is actually callable. Perhaps there is a way to add a comment that will override that for people using pyright?

You could also get around the type check by using guidance.Program instead of guidance (they are the same).

slundberg avatar May 24 '23 13:05 slundberg

My unfortunate workaround for this is: program: guidance.Program = guidance(...) # type: ignore

While it's fluent in a singleton context, extending Module via the __class__ attribute is fundamentally an impolite thing to do. Obeying the convention that modules are Modules makes static analysis significantly easier. Tools like Pyright that are not implemented in Python can't possibly model all of the effects that could result from dynamic module semantics (smells like halting problem to me).

https://discuss.python.org/t/pep-713-callable-modules/26127 is under consideration to address this in a different way (and https://discuss.python.org/t/pep-713-callable-modules/26127/59 is a counterargument providing an example of your approach).

justinbowes avatar May 30 '23 18:05 justinbowes

Hopefully this is gone in the new release :) Thanks for pointing this problem out!

marcotcr avatar Nov 14 '23 21:11 marcotcr

I'm getting this error in pycharm.

image

How can I get this approach to work in pycharm?

from guidance import Program


@Program(stateless=True)
def x(lm):
    return lm

results in:

ImportError: cannot import name 'Program' from 'guidance'

and

import guidance


@guidance.Program(stateless=True)
def x(lm):
    return lm

results in:

AttributeError: module 'guidance' has no attribute 'Program'

michael-newsrx avatar Apr 25 '24 16:04 michael-newsrx

My workaround for this issue is to use something like @guidance.guidance instead. Pylance is happy with that, and this is also more in line with common Python practice.

import guidance as guide

@guide.guidance(dedent=False)
def qa_bot(lm, query):
    lm += f"""
    Q: {query}
    A: {guide.gen(name="answer", stop="Q:")}"""
    return lm

tanders avatar Jun 27 '24 11:06 tanders