1

I want to validate if python function calls are valid without executing them.

Let's say I have the following code:

def func(a, b):
    print(a, b)

cond = True

if cond:
    func(1, 2)
else:
    func(1)

If cond = True, then everything will run just fine, but if cond = False, then it will fail throwing the following error:

TypeError: func() takes exactly 2 arguments (1 given)

I need to know if all function calls are valid, without having to call any function.

4

1 Answer 1

2

Such errors can be checked beforehand using mypy.

  1. Install mypy python3 -m pip install -U mypy.
  2. For checking Python3 code run mypy code.py, and for Python2 run mypy --py2 code.py.
  3. It will throw the following error:
code.py:9: error: Too few arguments for "func"
Found 1 error in 1 file (checked 1 source file)
Sign up to request clarification or add additional context in comments.

Comments

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.