smurfix
(Matthias Urlichs)
January 31, 2025, 7:04am
1
Hi,
I need to auto-create stub modules for a large embedded Python module (can’t load the thing into mypy …). So I’m looking for something that takes a function/module and returns the text I need to declare it. As in:
def fn(a:int,/,b:str,*,c:float|None=None) -> bool:
...
assert make_stub(fn).remove(" ") == "fn(a:int,/,b:str,*,c:float|None=None)->bool"
Does something like this exist? If not, fine, I know how to write it – but re-inventing the wheel is not on top of my list of priorities.
Monarch
(Monarch)
January 31, 2025, 8:20am
2
inspect.signature probably?
>>> def fn(a: int, /, b: str, *, c: float | None = None) -> bool: pass
>>> inspect.signature(fn)
<Signature (a: int, /, b: str, *, c: float | None = None) -> bool>
>>> def make_stub(f):
... name = f.__name__
... sig = inspect.signature(f)
... return f"def {name}{sig}: ..."
...
>>> make_stub(fn)
'def fn(a: int, /, b: str, *, c: float | None = None) -> bool: ...'
Jelle
(Jelle Zijlstra)
January 31, 2025, 3:08pm
3
You may also want to use mypy’s stubgen
tool.