= Path('test_py2pyi.py') fn
Create delegated pyi
Setup
Basics
imp_mod
imp_mod (module_path, package=None)
Import dynamically the module referenced in fn
= imp_mod(fn)
mod = mod.A()
a a.h()
1
= _get_tree(mod) tree
AST.__repr__
AST.__repr__ ()
# for o in enumerate(tree.body): print(o)
= tree.body[4]
node node
def f(a: int, b: str='a') -> str:
"""I am f"""
return 1
isinstance(node, functypes)
True
has_deco
has_deco (node:Union[ast.FunctionDef,ast.AsyncFunctionDef], name:str)
Check if a function node node
has a decorator named name
= 'delegates'
nm has_deco(node, nm)
False
= tree.body[5]
node node
@delegates(f)
def g(c, d: X, **kwargs) -> str:
"""I am g"""
return 2
has_deco(node, nm)
True
Function processing
def _proc_body (node, mod): print('_proc_body', type(node))
def _proc_func (node, mod): print('_proc_func', type(node))
def _proc_class (node, mod): print('_proc_class', type(node))
def _proc_patched(node, mod): print('_proc_patched', type(node))
; _proc_mod(mod)
_proc_class <class 'ast.ClassDef'>
_proc_body <class 'ast.FunctionDef'>
_proc_func <class 'ast.FunctionDef'>
_proc_body <class 'ast.FunctionDef'>
_proc_class <class 'ast.ClassDef'>
_proc_class <class 'ast.ClassDef'>
_proc_patched <class 'ast.FunctionDef'>
_proc_patched <class 'ast.FunctionDef'>
_proc_body <class 'ast.FunctionDef'>
node.name
'g'
= getattr(mod, node.name)
sym sym
<function test_py2pyi.g(c, d: test_py2pyi.X, *, b: str = 'a') -> str>
= signature(sym)
sig print(sig)
(c, d: test_py2pyi.X, *, b: str = 'a') -> str
sig2str
sig2str (sig)
ast_args
ast_args (func)
= ast_args(sym)
newargs newargs
*, b: str='a' c, d: test_py2pyi.X,
node.args
**kwargs c, d: X,
= newargs
node.args node
@delegates(f)
def g(c, d: test_py2pyi.X, *, b: str='a') -> str:
"""I am g"""
return 2
_body_ellip(node) node
@delegates(f)
def g(c, d: test_py2pyi.X, *, b: str='a') -> str:
"""I am g"""
...
= _get_tree(mod)
tree = tree.body[5]
node node
@delegates(f)
def g(c, d: X, **kwargs) -> str:
"""I am g"""
return 2
_update_func(node, sym) node
def g(c, d: test_py2pyi.X, *, b: str='a') -> str:
"""I am g"""
...
= _proc_mod(mod)
tree 5] tree.body[
_proc_class <class 'ast.ClassDef'>
_proc_class <class 'ast.ClassDef'>
_proc_class <class 'ast.ClassDef'>
_proc_patched <class 'ast.FunctionDef'>
_proc_patched <class 'ast.FunctionDef'>
def g(c, d: test_py2pyi.X, *, b: str='a') -> str:
"""I am g"""
...
Patch
= tree.body[9]
node node
@patch
@delegates(j)
def k(self: (A, B), b: bool=False, **kwargs):
return 1
= node.args.args[0].annotation ann
if hasattr(ann, 'elts'): ann = ann.elts[0]
= ann.id
nm nm
'A'
= getattr(mod, nm)
cls = getattr(cls, node.name) sym
sig2str(signature(sym))
"(self: (test_py2pyi.A, test_py2pyi.B), b: bool = False, *, d: str = 'a')"
_update_func(node, sym)
node
@patch
def k(self: (test_py2pyi.A, test_py2pyi.B), b: bool=False, *, d: str='a'):
...
= _proc_mod(mod)
tree 9] tree.body[
_proc_class <class 'ast.ClassDef'>
_proc_class <class 'ast.ClassDef'>
_proc_class <class 'ast.ClassDef'>
@patch
def k(self: (test_py2pyi.A, test_py2pyi.B), b: bool=False, *, d: str='a'):
...
Class and file
= _get_tree(mod)
tree = tree.body[7]
node node
class A:
@delegates(j)
def h(self, b: bool=False, **kwargs):
= 1
a return a
node.body
[@delegates(j)
def h(self, b: bool=False, **kwargs):
a = 1
return a]
= _proc_mod(mod)
tree 7] tree.body[
class A:
def h(self, b: bool=False, *, d: str='a'):
...
create_pyi
create_pyi (fn, package=None)
Convert fname.py
to fname.pyi
by removing function bodies and expanding delegates
kwargs
create_pyi(fn)
# fn = Path('/Users/jhoward/git/fastcore/fastcore/docments.py')
# create_pyi(fn, 'fastcore')
Script
py2pyi
py2pyi (fname:str, package:str=None)
Convert fname.py
to fname.pyi
by removing function bodies and expanding delegates
kwargs
Type | Default | Details | |
---|---|---|---|
fname | str | The file name to convert | |
package | str | None | The parent package |
replace_wildcards
replace_wildcards (path:str)
Expand wildcard imports in the specified Python file.
Type | Details | |
---|---|---|
path | str | Path to the Python file to process |