Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions fastcore/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,7 @@
'fastcore.pyparse.parse_py': ('pyparse.html#parse_py', 'fastcore/pyparse.py'),
'fastcore.pyparse.parse_py_file': ('pyparse.html#parse_py_file', 'fastcore/pyparse.py'),
'fastcore.pyparse._target_names': ('pyparse.html#_target_names', 'fastcore/pyparse.py'),
'fastcore.pyparse._is_patch_deco': ('pyparse.html#_is_patch_deco', 'fastcore/pyparse.py'),
'fastcore.pyparse.patch_targets': ('pyparse.html#patch_targets', 'fastcore/pyparse.py')},
'fastcore.script': { 'fastcore.script.store_true': ('script.html#store_true', 'fastcore/script.py'),
'fastcore.script.store_false': ('script.html#store_false', 'fastcore/script.py'),
Expand Down
2 changes: 1 addition & 1 deletion fastcore/py2pyi.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def _get_proc(node):
if isinstance(node, ast.ClassDef): return _proc_class
if not isinstance(node, functypes): return None
if not has_deco(node, 'delegates'): return _proc_body
if has_deco(node, 'patch'): return _proc_patched
if patch_targets(node): return _proc_patched
return _proc_func

# %% #cc1a0b3f
Expand Down
16 changes: 10 additions & 6 deletions fastcore/pyparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,21 @@ def _target_names(node):
if isinstance(node, (ast.Tuple, ast.List)): return [nm for e in node.elts for nm in _target_names(e)]
return [ast.unparse(node).strip()]

_PATCH_MODS = 'fastcore', 'fastcore.basics', 'fastcore.utils'

def _is_patch_deco(d):
return ((isinstance(fn := getattr(d, 'func', d), ast.Name) or (isinstance(fn, ast.Attribute) and ast.unparse(fn.value) in _PATCH_MODS))
and ((decor_id(d) == 'patch' and not getattr(d, 'args', ())) or (decor_id(d) == 'patch_to' and isinstance(d, ast.Call))))

def patch_targets(
o # `PyDef` or AST node to inspect
)->list[str]:
"Target class names for a `@patch` or `@patch_to` function, or `[]`"
node = o.node if isinstance(o, PyDef) else o
if not isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)): return []
if not (d := first(d for d in node.decorator_list if decor_id(d) in ('patch', 'patch_to'))): return []
if not (d := first(d for d in node.decorator_list if _is_patch_deco(d))): return []
if decor_id(d) == 'patch':
args = node.args.posonlyargs or node.args.args
return _target_names(args[0].annotation) if args and args[0].annotation else []
if isinstance(d, ast.Call):
arg = d.args[0] if d.args else first(k.value for k in d.keywords if k.arg == 'cls')
return _target_names(arg) if arg else []
return []
arg = args[0].annotation if args else None
else: arg = d.args[0] if d.args else first(k.value for k in d.keywords if k.arg == 'cls')
return _target_names(arg) if arg else []
2 changes: 1 addition & 1 deletion ship/10_py2pyi.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@
" if isinstance(node, ast.ClassDef): return _proc_class\n",
" if not isinstance(node, functypes): return None\n",
" if not has_deco(node, 'delegates'): return _proc_body\n",
" if has_deco(node, 'patch'): return _proc_patched\n",
" if patch_targets(node): return _proc_patched\n",
" return _proc_func"
]
},
Expand Down
24 changes: 16 additions & 8 deletions ship/15_pyparse.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -471,28 +471,32 @@
" if isinstance(node, (ast.Tuple, ast.List)): return [nm for e in node.elts for nm in _target_names(e)]\n",
" return [ast.unparse(node).strip()]\n",
"\n",
"_PATCH_MODS = 'fastcore', 'fastcore.basics', 'fastcore.utils'\n",
"\n",
"def _is_patch_deco(d):\n",
" return ((isinstance(fn := getattr(d, 'func', d), ast.Name) or (isinstance(fn, ast.Attribute) and ast.unparse(fn.value) in _PATCH_MODS))\n",
" and ((decor_id(d) == 'patch' and not getattr(d, 'args', ())) or (decor_id(d) == 'patch_to' and isinstance(d, ast.Call))))\n",
"\n",
"def patch_targets(\n",
" o # `PyDef` or AST node to inspect\n",
")->list[str]:\n",
" \"Target class names for a `@patch` or `@patch_to` function, or `[]`\"\n",
" node = o.node if isinstance(o, PyDef) else o\n",
" if not isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)): return []\n",
" if not (d := first(d for d in node.decorator_list if decor_id(d) in ('patch', 'patch_to'))): return []\n",
" if not (d := first(d for d in node.decorator_list if _is_patch_deco(d))): return []\n",
" if decor_id(d) == 'patch':\n",
" args = node.args.posonlyargs or node.args.args\n",
" return _target_names(args[0].annotation) if args and args[0].annotation else []\n",
" if isinstance(d, ast.Call):\n",
" arg = d.args[0] if d.args else first(k.value for k in d.keywords if k.arg == 'cls')\n",
" return _target_names(arg) if arg else []\n",
" return []"
" arg = args[0].annotation if args else None\n",
" else: arg = d.args[0] if d.args else first(k.value for k in d.keywords if k.arg == 'cls')\n",
" return _target_names(arg) if arg else []"
]
},
{
"cell_type": "markdown",
"id": "6be191a9",
"metadata": {},
"source": [
"`patch_targets` reads the target class names of `@patch` and `@patch_to` functions (sync or async), expanding union (`A | B`) and tuple annotations and handling positional-only `self`:"
"`patch_targets` reads the target class names of `@patch` and `@patch_to` functions (sync or async), expanding union (`A | B`) and tuple annotations and handling positional-only `self`, ignoring non-fastcore decorators such as `@app.patch`:"
]
},
{
Expand All @@ -513,11 +517,15 @@
"def d(self): pass\n",
"@patch\n",
"def e(self): pass\n",
"@app.patch('/x')\n",
"async def f(req: Req): pass\n",
"@patch('pkg.fn')\n",
"def g(x: int): pass\n",
"def plain(): pass\n",
"class Cls: pass\n",
"'''\n",
"pm = parse_py(PSRC)\n",
"test_eq([patch_targets(d) for d in pm.defs], [['Foo'], ['A', 'mod.B'], ['X', 'Y'], ['Z'], [], [], []])\n",
"test_eq([patch_targets(d) for d in pm.defs], [['Foo'], ['A', 'mod.B'], ['X', 'Y'], ['Z'], [], [], [], [], []])\n",
"test_eq(patch_targets(pm.funcs[0].node), ['Foo'])"
]
},
Expand Down
Loading