Skip to content

Instantly share code, notes, and snippets.

@micimize
Last active September 9, 2022 20:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save micimize/a8be33e63bb77a7d45ae1e3980c375e7 to your computer and use it in GitHub Desktop.
Save micimize/a8be33e63bb77a7d45ae1e3980c375e7 to your computer and use it in GitHub Desktop.
It doesn't work because the note is only emitted in very simple cases
from sys import stdin
from sys import stdout
from typing import Final
from typing import Iterable
MISSING_RETURN_TYPE: Final = "error: Function is missing a return type annotation [no-untyped-def]\n"
USE_NONE: Final = 'note: Use "-> None" if function does not return a value\n'
def filter_non_returns(mypy_lines: Iterable[str]) -> Iterable[str]:
current_location = ""
is_current_location_missing_non_none_return = False
swallowed = 0
for line in mypy_lines:
try:
line_location, message = line.split(": ", 1)
except ValueError:
if line.startswith("Found"):
if is_current_location_missing_non_none_return:
yield f"{current_location}: {MISSING_RETURN_TYPE}"
_, error_count, rest = line.split(" ", 3)
yield f"Found {int(error_count) - swallowed} {rest}"
current_location = ""
is_current_location_missing_non_none_return = False
swallowed = 0
continue
yield line
continue
is_line_missing_return_message = message == MISSING_RETURN_TYPE
if line_location != current_location:
if is_current_location_missing_non_none_return:
yield f"{current_location}: {MISSING_RETURN_TYPE}"
current_location = line_location
is_current_location_missing_non_none_return = is_line_missing_return_message
if is_line_missing_return_message:
is_current_location_missing_non_none_return = is_line_missing_return_message
continue
if message == USE_NONE:
is_current_location_missing_non_none_return = False
swallowed += 2
continue
yield line
if __name__ == "__main__":
try:
for line in filter_non_returns(stdin):
stdout.write(line)
finally:
stdout.flush()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment