Type Checker yells at me when I use guidance with arguments
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'}}""")
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).
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).
Hopefully this is gone in the new release :) Thanks for pointing this problem out!
I'm getting this error in pycharm.
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'
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