Is there a possibility to check on Runtime errors? It is clear from a string that pri1nt is a function and it is not defined in this string.
import ast
def is_valid_python(code):
try:
ast.parse(code)
except SyntaxError:
return False
return True
mycode = 'pri1nt("hello world")'
is_valid_python(mycode) # true
exec(mycode) # NameError: name 'pri1nt' is not defined
pr1ntis a perfectly valid name in Python; the fact that it is not defined means there would be a NameError. But generally this kind of static analysis is hard to do in Python without actually executing the code.astmodule you can do some of the stuff that I imagine you try to achieve, e.g. check if names can resolve.from os import environ; print(environ['FOO'])produce a key error? Nobody knows, unless you run the code.