21 questions
2
votes
1
answer
101
views
Type checker highlights 'Unexpected argument' for dynamically generated function
I'm trying to create a function dynamically. Here is an example:
import ast
import textwrap
from typing import Any, Callable, List, Union
def create_function(
func_name: str,
arg_types: List[...
0
votes
0
answers
82
views
Find a module's function using Python ast
There are variations, like
import ast
code = """
import datetime
datetime.datetime.now()
"""
tree = ast.parse(code)
print(ast.dump(tree, indent=2))
the ...
1
vote
1
answer
135
views
Visiting a assert statement within a functiondef using Python ast
I am currently doing some code manipulation and I am unable to visit an "Assert" statement within a "FunctionDef". How I visit an internal Assert node within a Function node?
Here ...
1
vote
1
answer
530
views
Using Python AST Module, parse callables used as arguments
I'm writing a script that will walk through various Python modules, and hunt for instances of calls to fx.bind(). The first argument to this function is a string representing the hotkey, and the ...
1
vote
2
answers
226
views
How do I find the line and column offsets for imported Python modules?
I have a class (based on this answer) that uses ast.NodeVisitor to get a list of modules imported by a Python file. However, I also want to return the line and column offsets for where the module ...
1
vote
1
answer
258
views
Importing AST modification
I'm attempting dynamic rewrite of pywin32 win32com.client module prior to import, the below seems to work - but I'm not happy with the 6 lines of code for importing (after the last comment). Can ...
2
votes
1
answer
582
views
ast nodes not preserving some properties (lineno or/and col_offset)
I'm trying to convert every break statement with exec('break') in a code. So far I've got this:
import ast
source = '''some_list = [2, 3, 4, 5]
for i in some_list:
if i == 4:
p = 0
...
2
votes
1
answer
518
views
Find end_lineno in ast.ImportFrom
In python 3.8 while doing ast.parse you get a end_lineno variable:
import ast
code_example = 'from typing import List, Dict'
parsed_tree = ast.parse(code_example)
for item in parsed_tree.body:
...
2
votes
1
answer
286
views
Replacing Python Call AST node with Try node
The system I work with allows users to specify a Python Boolean expression as a string (in a configuration file). The system takes the string, converts it to a Python AST object and then evaluates ...
2
votes
1
answer
847
views
Python: what is the ast.FunctionType in python ast?
I don't pretend to have understood everything in python ast, but FunctionType is something that bothered me.
mod = Module(stmt* body, type_ignore *type_ignores)
| Interactive(stmt* body)
...
13
votes
2
answers
4k
views
Generate .pyc from Python AST?
How would I generate a .pyc file from a Python AST such that I could import the file from Python?
I've used compile to create a code object, then written the co_code attribute to a file, but when I ...