0

I would like to build something into my Python module that can throw a warning if the user has not done something correctly. For example if the user has a while loop with nothing in it that will block threads, or a user does not initialize something correctly.

Currently I am loading the python file before it runs with a "pre-runtime" script to check these warnings. However it still requires the user to run this pre-check script. I would like to figure out how to do this in my main module. Aside from loading the python script as a string and then parsing it I can't think of a good way to do this.

For example if the user runs this code

import myModule
test = myModule.module()

print("Hello")

while 1:
    True

They would get

>> Warning, you used a print statement, they have no power here. 

Or the alternative example in the other direction

import myModule
test = myModule.module()

They would get

>> Warning there is no while True loop, your code will just exit
8
  • Most IDEs will check this automatically for you... Commented Apr 23, 2021 at 12:34
  • Yes they will check normally errors that are pythonic. I am looking for special case handling related to this specific application. As a super basic example let's say I never want a user to use "print("hello")", again silly example but exactly the kind of case I am looking at. Python will not catch this as an error but in the context of my system it is an error. Commented Apr 23, 2021 at 12:37
  • 1
    So you're trying to look for usage errors? Commented Apr 23, 2021 at 12:40
  • 1
    if you are sure you are going to input a valid python code, something from docs.python.org/3/library/ast.html can help Commented Apr 23, 2021 at 12:42
  • 1
    BTW, edit your question elaborating exactly what you want, instead of in the comment section. Commented Apr 23, 2021 at 13:03

1 Answer 1

1

I am just writing this as a solution because I found this interesting, there may be better ways to accomplish.

check.py

import ast

with open('temp.py') as file:
    module_node = ast.parse(file.read())

for node in ast.walk(module_node):
    # check if there is `while True` loop
    if isinstance(node, ast.While) and isinstance(node.test, ast.Constant) and node.test.value is True:
        print('There is a `while True` in the code', node.lineno)

    # check if there is `print` call in the code
    elif isinstance(node, ast.Call) and node.func.id == 'print':
        print('There is a `print` call in the code', node.lineno)

temp.py

import foo # check.py does not run this import

while 1 > 2:
    while True:
        print('some text', '12321')

Output

There is a `while True` in the code 4
There is a `print` call in the code 5

So you get the idea, you just add in conditions in the for loop and have a message printed. Again, there may be better alternatives.

Sign up to request clarification or add additional context in comments.

2 Comments

Very cool, this is exactly the solution I was looking for. Thanks for pointing out ast, it is a very cool module. Then I can probably direct os.path.basename(__file__) to open the script automatically so the user doesn't have to import it. Solution found! Thanks!
glad to know this was helpful, can you mark this as accepted then @Meozaa?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.