Skip to content

Commit 1044c30

Browse files
committed
fix a lot of typing issues
1 parent a560f79 commit 1044c30

File tree

14 files changed

+67
-54
lines changed

14 files changed

+67
-54
lines changed

bin/config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import re
66
from pathlib import Path
77
from collections.abc import Mapping, Sequence
8-
from typing import Final, Literal, Optional
8+
from typing import Any, Final, Literal, Optional
99

1010
SPEC_VERSION: Final[str] = "2023-07-draft"
1111

@@ -104,7 +104,7 @@
104104

105105
args = argparse.Namespace()
106106

107-
DEFAULT_ARGS: Final[Mapping] = {
107+
DEFAULT_ARGS: Final[Mapping[str, Any]] = {
108108
"jobs": (os.cpu_count() or 1) // 2,
109109
"time": 600, # Used for `bt fuzz`
110110
"verbose": 0,

bin/constraints.py

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import re
22
from collections import defaultdict
3+
from typing import Optional
34

45
import latex
56
import validate
67
from colorama import Fore, Style
8+
from problem import Problem
79

810
# Local imports
911
from util import *
@@ -16,7 +18,9 @@
1618
"""
1719

1820

19-
def check_validators(problem):
21+
def check_validators(
22+
problem: Problem,
23+
) -> tuple[set[int | float], list[str | tuple[int | float, str, int | float]]]:
2024
in_constraints: validate.ConstraintsDict = {}
2125
ans_constraints: validate.ConstraintsDict = {}
2226
problem.validate_data(validate.Mode.INPUT, constraints=in_constraints)
@@ -27,10 +31,10 @@ def check_validators(problem):
2731
log("No constraint validation of answer values found in answer or output validators.")
2832
print()
2933

30-
validator_values = set()
34+
validator_values: set[int | float] = set()
3135
validator_defs: list[str | tuple[int | float, str, int | float]] = []
3236

33-
def f(cs):
37+
def f(cs: validate.ConstraintsDict) -> None:
3438
for loc, value in sorted(cs.items()):
3539
name, has_low, has_high, vmin, vmax, low, high = value
3640
validator_defs.append((low, name, high))
@@ -45,12 +49,12 @@ def f(cs):
4549
return validator_values, validator_defs
4650

4751

48-
def check_statement(problem, language):
52+
def check_statement(problem: Problem, language: str) -> tuple[set[int | float], list[str]]:
4953
statement_file = problem.path / latex.PdfType.PROBLEM.path(language)
5054
statement = statement_file.read_text()
5155

52-
statement_values = set()
53-
statement_defs = []
56+
statement_values: set[int | float] = set()
57+
statement_defs: list[str] = []
5458

5559
defines = ["\\def", "\\newcommand"]
5660
sections = ["Input", "Output", "Interaction"]
@@ -67,15 +71,16 @@ def check_statement(problem, language):
6771
}
6872
relations = re.compile(r"(<=|!=|>=|<|=|>)")
6973

70-
def math_eval(text):
74+
def math_eval(text: str) -> Optional[int | float]:
7175
try:
7276
# eval is dangerous, but on the other hand we run submission code so this is fine
7377
text = text.replace("^", "**")
74-
return eval(text, {"__builtin__": None})
78+
value = eval(text, {"__builtin__": None})
79+
return value if value is isinstance(value, (int, float)) else None
7580
except (SyntaxError, NameError, TypeError, ZeroDivisionError):
7681
return None
7782

78-
def constraint(text):
83+
def constraint(text: str) -> None:
7984
# handles $$math$$
8085
if len(text) == 0:
8186
return
@@ -132,13 +137,13 @@ def constraint(text):
132137
in_io = False
133138
end = None
134139

135-
def matches(text):
140+
def matches(text: str) -> bool:
136141
nonlocal pos
137142
if pos + len(text) > len(statement):
138143
return False
139144
return statement[pos : pos + len(text)] == text
140145

141-
def parse_group():
146+
def parse_group() -> str:
142147
nonlocal pos
143148
assert statement[pos] == "{"
144149
next = pos + 1
@@ -155,7 +160,7 @@ def parse_group():
155160
pos = next
156161
return name
157162

158-
def parse_command():
163+
def parse_command() -> str:
159164
nonlocal pos
160165
assert statement[pos] == "\\"
161166
next = pos + 1
@@ -251,16 +256,16 @@ def parse_command():
251256
return statement_values, statement_defs
252257

253258

254-
def check_constraints(problem):
259+
def check_constraints(problem: Problem) -> bool:
255260
validator_values, validator_defs = check_validators(problem)
256-
statement_values = defaultdict(set)
257-
statement_defs = defaultdict(set)
261+
statement_values: dict[int | float, set[str]] = defaultdict(set)
262+
statement_defs: dict[str, set[str]] = defaultdict(set)
258263
for lang in problem.statement_languages:
259264
values, defs = check_statement(problem, lang)
260-
for entry in values:
261-
statement_values[entry].add(lang)
262-
for entry in defs:
263-
statement_defs[entry].add(lang)
265+
for value_entry in values:
266+
statement_values[value_entry].add(lang)
267+
for def_entry in defs:
268+
statement_defs[def_entry].add(lang)
264269

265270
# print all the definitions.
266271
value_len = 12

bin/export.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1+
import config
12
import datetime
3+
import re
4+
import shutil
25
import sys
6+
import util
37
import yaml
4-
import re
58
import zipfile
6-
import config
7-
import util
89
from pathlib import Path
9-
from typing import Optional
10+
from typing import Any, Optional
1011

1112
from contest import *
1213
from latex import PdfType

bin/fuzz.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import run
44
import random
55
import generate
6+
import signal
67
import time
78
import threading
9+
from pathlib import Path
810

911
import parallel
1012
from util import *

bin/generate.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1+
import collections
12
import random
23
import re
3-
import shutil
4-
import collections
54
import secrets
5+
import shutil
6+
import sys
7+
import time
68

79
from collections.abc import Callable, Sequence
810
from colorama import Fore, Style
911
from pathlib import Path, PurePosixPath
10-
from typing import Final, Optional, overload
12+
from typing import Any, Final, Optional, overload
1113

1214
import config
1315
import parallel

bin/interactive.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1+
import os
12
import signal
2-
import time
33
import subprocess
44
import sys
55
import threading
6+
import time
7+
from pathlib import Path
68
from typing import Final, Literal, Optional, TYPE_CHECKING
79

810
import config
9-
from util import *
1011
import validate
12+
from util import *
1113
from verdicts import Verdict
1214

1315
if TYPE_CHECKING:

bin/problem.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import datetime
22
import re
3+
import shutil
34
import sys
45
import threading
56

bin/program.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
import stat
44
import subprocess
55
import threading
6-
from typing import Final, TYPE_CHECKING
7-
86
from colorama import Fore
7+
from pathlib import Path
8+
from typing import Final, Optional, TYPE_CHECKING
99

10+
import config
1011
from util import *
1112

1213
if TYPE_CHECKING: # Prevent circular import: https://stackoverflow.com/a/39757388

bin/skel.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import shutil
1+
import os
22
import datetime
33
import re
4+
import shutil
5+
from pathlib import Path
46

57
# Local imports
68
import config

bin/slack.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import config
12
from util import *
23

34
# Perform slack actions for the selected problems (all, or the selected/current one).

0 commit comments

Comments
 (0)