Search code examples
pythonpyright

Is it possible to call Pyright from code (as an API)?


It seems that Pyright (the Python type checker made by Microsoft) can only be used as a command line tool or from VS Code. But is it possible to call pyright from code (as an API)?

For example, mypy supports usage like:

import sys
from mypy import api

result = api.run("your code")

Solution

  • like @Grismar said, this might be an xy problem... if not, here is a general solution:

    import subprocess
    
    command = ['pyright', 'path/to/your/file.py']
    result = subprocess.run(command, capture_output=True, text=True)
    output = result.stdout
    
    print(output)