Skip to content

Commit

Permalink
Add better type checks for static type checking
Browse files Browse the repository at this point in the history
  • Loading branch information
olilag committed Sep 20, 2024
1 parent 0dc7b69 commit aca9b5c
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions src/susi_lib/word_finder_cli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import argparse
import sys
from typing import TextIO
from typing import Literal, NotRequired, TextIO, TypedDict, cast

from susi_lib.regex import Selection, create_regex

Expand All @@ -26,7 +26,19 @@ def _validate_input(wanted_letters: list[str], length: str | None):
return True, ""


def _translate(wanted_letters: list[str], length: int | tuple[int, int], file: TextIO):
class _TrDictReturn(TypedDict):
data: NotRequired[list[str]]
length: NotRequired[int | tuple[int, int]]
letters: NotRequired[str]
invert: NotRequired[bool]


def _translate(
wanted_letters: list[str], length: int | tuple[int, int], file: TextIO
) -> tuple[
list[tuple[str, Selection]],
_TrDictReturn,
]:
data = list(file)
file.close()

Expand All @@ -35,7 +47,7 @@ def _translate(wanted_letters: list[str], length: int | tuple[int, int], file: T
wl = wanted_letters[0]
return ([], {"data": data, "length": length, "letters": wl})
case _:
args = []
args: list[tuple[str, Selection]] = []
for group in wanted_letters:
if "." in group:
args.append((group, Selection.ANY))
Expand All @@ -46,7 +58,7 @@ def _translate(wanted_letters: list[str], length: int | tuple[int, int], file: T
return (args, {"data": data})


def main():
def main() -> Literal[0, 1]:
arg_parser = argparse.ArgumentParser(
description="Program for finding words using regular expressions."
)
Expand Down Expand Up @@ -76,10 +88,13 @@ def main():
args_parsed.input_file.close()
return 1

word_length = (
tuple(map(int, args_parsed.word_length.split("-")))
if args_parsed.word_length is not None
else len(args_parsed.wanted_letters[0])
word_length = cast(
int | tuple[int, int] | tuple[int],
(
tuple(map(int, args_parsed.word_length.split("-")))
if args_parsed.word_length is not None
else len(args_parsed.wanted_letters[0])
),
)
word_length = (
word_length[0]
Expand Down

0 comments on commit aca9b5c

Please sign in to comment.