forked from kracekumar/python-typing-koans
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
executable file
·129 lines (96 loc) · 3.22 KB
/
cli.py
File metadata and controls
executable file
·129 lines (96 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python
from dataclasses import dataclass
from pathlib import Path
import click
from mypy import api
from rich.console import Console
from rich.table import Table
@dataclass
class Result:
result: str
error: str
exit_code: int
DIRS = ["koans/py"]
def run_mypy(path: str):
result, error, exit_code = api.run(
[
path,
"--strict",
"--disallow-any-expr",
"--disallow-any-explicit",
"--disallow-untyped-calls",
"--disallow-untyped-defs",
"--disallow-incomplete-defs",
"--disallow-any-expr",
"--pretty",
]
)
return Result(result=result, error=error, exit_code=exit_code)
def run_one(path):
console = Console()
console.rule("")
console.print(f"Running mypy on koan file {path}")
res = run_mypy(path)
display_result(console, path, res, display="all")
console.rule()
def display_summary(console: Console, run_result: dict[str, Result]):
total = len(run_result)
passed = sum(1 for r in run_result.values() if r.exit_code == 0)
failed = total - passed
table = Table(title="Koans Summary")
table.add_column("Status")
table.add_column("Count")
table.add_row("Passed", str(passed))
table.add_row("Failed", str(failed))
console.print(table)
def display_result(console: Console, file_name: str, result: Result, display="all"):
if display in ("all", "error") and result.exit_code != 0:
console.rule(f"[bold] mypy errors in koan file {file_name}")
if result.result:
console.print(result.result)
if result.error:
console.print(result.error)
console.rule("End")
if display in ("all",) and result.exit_code == 0:
console.print(f"No errors in koan file {file_name} :thumbsup: ")
############# Commands ####################
@click.group()
def cli():
pass
@cli.command(help="Display summary of all koans")
@click.option("--display-error", default=False, is_flag=True)
def summary(display_error):
console = Console()
console.rule()
dirs = DIRS
run_result: dict[str, Result] = {}
with console.status("Running mypy against all koan files ...", spinner="moon"):
for directory in dirs:
for py_file in sorted(Path(directory).rglob("*.py")):
name = str(py_file)
res = run_mypy(name)
if res.exit_code == 0:
emoji_text = ":thumbsup:"
else:
emoji_text = ":thumbsdown:"
console.print(f"[bold] Ran mypy in koan file: {name} {emoji_text}")
run_result[name] = res
display_summary(console, run_result)
if display_error:
for file_name, result in run_result.items():
display_result(console, file_name, result, display="error")
console.rule()
@cli.command()
@click.argument("path", required=True, type=click.Path())
def one(path):
run_one(path)
@cli.command()
def list():
console = Console()
console.rule()
for directory in DIRS:
for py_file in sorted(Path(directory).rglob("*.py")):
console.print(py_file)
console.rule()
if __name__ == "__main__":
cli()