Created
February 6, 2024 15:32
-
-
Save benoit74/4de80d512b158a090082d6bdfe85cd20 to your computer and use it in GitHub Desktop.
Add pyright ignore statements for every diagnosis encountered
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
import re | |
import json | |
def run_pyright(): | |
# Run Pyright and capture the output | |
result = subprocess.run(["pyright", "--outputjson"], capture_output=True, text=True) | |
return result.stdout | |
def add_pyright_ignore_comments(output): | |
# Parse Pyright output | |
# Example output: {"severity": "warning", "message": "Unused import", "range": {"start": {"line": 5, "character": 8}, "end": {"line": 5, "character": 17}}, "file": "example.py"} | |
warnings = [ | |
match.groupdict() | |
for match in re.finditer( | |
r'"severity": "(warning|error)".*?"file": "(.*?)".*?"start": {"line": (\d+),', | |
output, | |
) | |
] | |
data = json.loads(output) | |
# Add pyright: ignore comments to identified lines | |
for warning in data["generalDiagnostics"]: | |
filename = warning["file"] | |
line_number = warning["range"]["start"]["line"] | |
with open(filename, "r+") as file: | |
lines = file.readlines() | |
lines[line_number] = ( | |
lines[line_number].rstrip("\n") + " # pyright: ignore\n" | |
) | |
file.seek(0) | |
file.writelines(lines) | |
if __name__ == "__main__": | |
pyright_output = run_pyright() | |
add_pyright_ignore_comments(pyright_output) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment