imp_mod
def imp_mod(
module_path, package:NoneType=None
):Import dynamically the module referenced in fn
Import dynamically the module referenced in fn
Check if a function node node has a decorator named name
parent_node = copy.deepcopy(tree.body[7])
patched_node = copy.deepcopy(tree.body[10])
test_is(has_deco(patched_node, "patch"), True)
test_eq(str(patched_node.args.args[0].annotation), parent_node.name)
_clean_patched_node(patched_node)
test_is(has_deco(patched_node, "patch"), False)
test_eq(patched_node.args.args[0].annotation, None)empty_cls1, empty_cls2, empty_cls3 = ast.parse('''
class A:
"""An empty class."""
class B:
pass
class C:
...
''').body
test_is(_is_empty_class(empty_cls1), True)
test_is(_is_empty_class(empty_cls2), True)
test_is(_is_empty_class(empty_cls3), True)
non_empty_cls, empty_func = ast.parse('''
class A:
a = 1
def f():
...
''').body
test_is(_is_empty_class(non_empty_cls), False)
test_is(_is_empty_class(empty_func), False)DeprecationWarning: ast.Ellipsis is deprecated and will be removed in Python 3.14; use ast.Constant instead
if isinstance(child, ast.Expr) and isinstance(child.value, (ast.Ellipsis, ast.Str)): return True
<ipython-input-1-9bb9d38d0e66>:7: DeprecationWarning: ast.Str is deprecated and will be removed in Python 3.14; use ast.Constant instead
if isinstance(child, ast.Expr) and isinstance(child.value, (ast.Ellipsis, ast.Str)): return True
# we could have reused `parent_node` and `patched_node` from the previous cells.
# copying them here allows us to run this cell multiple times which makes it a little easier to write tests.
parent_node = copy.deepcopy(tree.body[7])
patched_node = copy.deepcopy(tree.body[11])
test_eq(len(parent_node.body),1)
_add_patched_node_to_parent(patched_node, parent_node)
test_eq(len(parent_node.body),2)
test_eq(parent_node.body[-1], patched_node)
# patched node replaces an existing class method (A.h)
patched_h_node = ast.parse("""
@patch
def h(self: A, *args, **kwargs):
...
""", mode='single').body[0]
_add_patched_node_to_parent(patched_h_node, parent_node)
test_eq(len(parent_node.body), 2)
test_eq(parent_node.body[0], patched_h_node)
# patched node is added to an empty class
empty_cls, patched_node = ast.parse('''
class Z:
"""An empty class."""
@patch
def a(self: Z, *args, **kwargs):
...
''').body
test_eq(len(empty_cls.body), 1)
test_ne(empty_cls.body[0], patched_node)
_add_patched_node_to_parent(patched_node, empty_cls)
test_eq(len(empty_cls.body), 1)
test_eq(empty_cls.body[0], patched_node)DeprecationWarning: ast.Ellipsis is deprecated and will be removed in Python 3.14; use ast.Constant instead
if isinstance(child, ast.Expr) and isinstance(child.value, (ast.Ellipsis, ast.Str)): return True
<ipython-input-1-9bb9d38d0e66>:7: DeprecationWarning: ast.Str is deprecated and will be removed in Python 3.14; use ast.Constant instead
if isinstance(child, ast.Expr) and isinstance(child.value, (ast.Ellipsis, ast.Str)): return True
_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'>
DeprecationWarning: ast.Ellipsis is deprecated and will be removed in Python 3.14; use ast.Constant instead
if isinstance(child, ast.Expr) and isinstance(child.value, (ast.Ellipsis, ast.Str)): return True
DeprecationWarning: ast.Ellipsis is deprecated and will be removed in Python 3.14; use ast.Constant instead
if isinstance(child, ast.Expr) and isinstance(child.value, (ast.Ellipsis, ast.Str)): return True
<function test_py2pyi.g(c, d: test_py2pyi.X, *, b: str = 'a') -> str>
DeprecationWarning: ast.Str is deprecated and will be removed in Python 3.14; use ast.Constant instead
stidx = 1 if isinstance(n.body[0], ast.Expr) and isinstance(n.body[0].value, ast.Str) else 0
DeprecationWarning: ast.Str is deprecated and will be removed in Python 3.14; use ast.Constant instead
stidx = 1 if isinstance(n.body[0], ast.Expr) and isinstance(n.body[0].value, ast.Str) else 0
_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'>
DeprecationWarning: ast.Str is deprecated and will be removed in Python 3.14; use ast.Constant instead
stidx = 1 if isinstance(n.body[0], ast.Expr) and isinstance(n.body[0].value, ast.Str) else 0
<ipython-input-1-9bb9d38d0e66>:7: DeprecationWarning: ast.Ellipsis is deprecated and will be removed in Python 3.14; use ast.Constant instead
if isinstance(child, ast.Expr) and isinstance(child.value, (ast.Ellipsis, ast.Str)): return True
DeprecationWarning: ast.Str is deprecated and will be removed in Python 3.14; use ast.Constant instead
stidx = 1 if isinstance(n.body[0], ast.Expr) and isinstance(n.body[0].value, ast.Str) else 0
<ipython-input-1-9bb9d38d0e66>:7: DeprecationWarning: ast.Ellipsis is deprecated and will be removed in Python 3.14; use ast.Constant instead
if isinstance(child, ast.Expr) and isinstance(child.value, (ast.Ellipsis, ast.Str)): return True
Convert fname.py to fname.pyi by removing function bodies and expanding delegates kwargs
DeprecationWarning: ast.Str is deprecated and will be removed in Python 3.14; use ast.Constant instead
stidx = 1 if isinstance(n.body[0], ast.Expr) and isinstance(n.body[0].value, ast.Str) else 0
<ipython-input-1-9bb9d38d0e66>:7: DeprecationWarning: ast.Ellipsis is deprecated and will be removed in Python 3.14; use ast.Constant instead
if isinstance(child, ast.Expr) and isinstance(child.value, (ast.Ellipsis, ast.Str)): return True
Convert fname.py to fname.pyi by removing function bodies and expanding delegates kwargs
Expand wildcard imports in the specified Python file.