Skip to content
This repository was archived by the owner on Jun 7, 2023. It is now read-only.

Commit 39d04af

Browse files
Improve python_check messaging
Include non-syntax errors Standardise message formats for syntax and other areas
1 parent 89d760e commit 39d04af

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

bookserver/routers/books.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from fastapi.templating import Jinja2Templates
2828
from jinja2.exceptions import TemplateNotFound
2929
from pydantic import constr
30-
from pyflakes import (reporter, checker)
30+
from pyflakes import checker as pyflakes_checker
3131

3232
# Local application imports
3333
# -------------------------
@@ -58,16 +58,14 @@
5858
tags=["books"],
5959
)
6060

61-
61+
62+
6263
@router.post("/python_check")
6364
async def python_check(request: Request):
6465
"""
6566
Takes a chunk of Python code and runs a syntax checker (currently
6667
Pyflakes) on it to provide more detailed advice than is available
6768
via Skulpt.
68-
69-
Caller must provide:
70-
* ``code`` -- the Python code to check
7169
"""
7270
code_bytes = await request.body()
7371
code = code_bytes.decode("utf-8")
@@ -77,13 +75,12 @@ async def python_check(request: Request):
7775
resultMessage = ""
7876
try:
7977
tree = ast.parse(code, filename=filename)
80-
w = checker.Checker(tree, filename=filename)
78+
w = pyflakes_checker.Checker(tree, filename=filename)
79+
w.messages.sort(key=lambda m: m.lineno)
80+
for m in w.messages:
81+
resultMessage = resultMessage + str(m) + "\n"
8182
except SyntaxError as e:
82-
textOut = io.StringIO()
83-
reporter = reporter.Reporter(textOut, textOut)
84-
reporter.syntaxError(filename, e.args[0], e.lineno, e.offset, e.text)
85-
textOut.seek(0)
86-
resultMessage = textOut.read()
83+
resultMessage = filename + ":" + str(e.lineno) + ":" + str(e.offset) + ": " + e.args[0] + "\n"
8784

8885
return resultMessage
8986

0 commit comments

Comments
 (0)