Skip to content

Commit

Permalink
[#215][Flashcards/Python] Migrate from type_enforced to mypy.
Browse files Browse the repository at this point in the history
NOTE: We deleted the type `field.grp`. It was too costly to retain it in
this commit. There is a chance we will have to introduce it for #75 or
something else.
  • Loading branch information
pishoyg committed Aug 31, 2024
1 parent 40719db commit 5aeb7f5
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 168 deletions.
30 changes: 7 additions & 23 deletions flashcards/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
import json
import os
import re
import typing

import deck
import enforcer
import field
import type_enforced

import utils

Expand All @@ -22,14 +21,12 @@
DICT_WIDTH = "1000px"


@type_enforced.Enforcer(enabled=enforcer.ENABLED)
def crum(
deck_name: str,
deck_id: int,
dialect_cols: list[str],
force_front: bool = True,
) -> deck.deck:
@type_enforced.Enforcer(enabled=enforcer.ENABLED)
def roots_col(
col_name: str,
line_br: bool = False,
Expand Down Expand Up @@ -66,7 +63,6 @@ def root_appendix(
# This replaces all Coptic words, regardless of whether they
# represent plain text. Coptic text that occurs inside a tag (for example
# as a tag property) would still get wrapped inside this <span> tag.
@type_enforced.Enforcer(enabled=enforcer.ENABLED)
def cdo(text: str) -> str:
return COPTIC_WORD_RE.sub(
r'<span class="coptic">\1</span>',
Expand All @@ -77,19 +73,17 @@ def cdo(text: str) -> str:
# This replaces all Greek words, regardless of whether they
# represent plain text. Greek text that occurs inside a tag (for example
# as a tag property) would still acquire this tag.
@type_enforced.Enforcer(enabled=enforcer.ENABLED)
def greek(text: str) -> str:
return GREEK_WORD_RE.sub(
r'<span class="greek">\1</span>',
text,
)

@type_enforced.Enforcer(enabled=enforcer.ENABLED)
def create_front() -> field.Field:
def create_front() -> field.field:
if len(dialect_cols) == 1:
return roots_col(dialect_cols[0], line_br=True, force=False)

def dialect(col: str) -> field.Field:
def dialect(col: str) -> field.field:
return field.aon(
'<span class="left">',
"(",
Expand Down Expand Up @@ -392,9 +386,7 @@ def _explanatory_alt(path: str) -> str:
)


@type_enforced.Enforcer(enabled=enforcer.ENABLED)
def copticsite_com(deck_name: str, deck_id: int) -> deck.deck:
@type_enforced.Enforcer(enabled=enforcer.ENABLED)
def col(
col_name: str,
line_br: bool = False,
Expand Down Expand Up @@ -442,9 +434,7 @@ def col(
)


@type_enforced.Enforcer(enabled=enforcer.ENABLED)
def kellia(deck_name: str, deck_id: int, basename: str) -> deck.deck:
@type_enforced.Enforcer(enabled=enforcer.ENABLED)
def col(
col_name: str,
line_br: bool = False,
Expand Down Expand Up @@ -489,7 +479,6 @@ def col(
)


@type_enforced.Enforcer(enabled=enforcer.ENABLED)
def _dedup(arr: list[int], at_most_once: bool = False) -> list[int]:
"""
Args:
Expand All @@ -503,23 +492,21 @@ def _dedup(arr: list[int], at_most_once: bool = False) -> list[int]:
"""
if at_most_once:
return list(dict.fromkeys(arr))
out = []
out: list[int] = []
for x in arr:
if out and out[-1] == x:
continue
out.append(x)
return out


@type_enforced.Enforcer(enabled=enforcer.ENABLED)
def _page_numbers(page_ranges: str) -> list[int]:
"""page_ranges is a comma-separated list of integers or integer ranges,
just like what you type when you're using your printer.
For example, "1,3-5,8-9" means [1, 3, 4, 5, 8, 9].
"""

@type_enforced.Enforcer(enabled=enforcer.ENABLED)
def parse(page_number: str) -> int:
page_number = page_number.strip()
if page_number[-1] in ["a", "b"]:
Expand All @@ -543,10 +530,9 @@ def parse(page_number: str) -> int:
return out


@type_enforced.Enforcer(enabled=enforcer.ENABLED)
class _dir_lister:
def __init__(self, dir: str, get_key: enforcer.Callable) -> None:
self.cache = {}
def __init__(self, dir: str, get_key: typing.Callable) -> None:
self.cache: dict[str, list[str]] = {}
if not os.path.exists(dir):
return
for file in os.listdir(dir):
Expand All @@ -560,7 +546,6 @@ def get(self, key: str) -> list[str]:
return utils.sort_semver(self.cache.get(key, []))


@type_enforced.Enforcer(enabled=enforcer.ENABLED)
class _sensor:
def __init__(self, keys: list[str], sense_jsons: list[str]) -> None:
self.decoder = json.JSONDecoder(
Expand Down Expand Up @@ -634,7 +619,6 @@ def get_caption(self, path: str) -> str:
KELLIA_GREEK = "KELLIA::Greek"


@type_enforced.Enforcer(enabled=enforcer.ENABLED)
def file_name(deck_name: str) -> str:
"""Given a deck name, return a string that is valid as a file name.
Expand All @@ -645,7 +629,7 @@ def file_name(deck_name: str) -> str:
)


LAMBDAS: dict[str, enforcer.Callable] = {
LAMBDAS: dict[str, typing.Callable] = {
CRUM_ALL: lambda deck_name: crum(
deck_name,
1284010387,
Expand Down
23 changes: 11 additions & 12 deletions flashcards/deck.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
import pathlib
import shutil

import enforcer
import field
import genanki
import type_enforced
import genanki # type: ignore[import-untyped]

import utils

Expand Down Expand Up @@ -36,7 +34,6 @@
ANKI_JS_FMT = """(() => {{ {javascript} }})();"""


@type_enforced.Enforcer(enabled=enforcer.ENABLED)
class stats:
def __init__(self) -> None:
self._exported_notes = 0
Expand All @@ -61,7 +58,6 @@ def print(self) -> None:
self.problematic(self._duplicate_key, "notes have duplicate keys.")


@type_enforced.Enforcer(enabled=enforcer.ENABLED)
class Note(genanki.Note):
@property
def guid(self):
Expand All @@ -70,7 +66,6 @@ def guid(self):
return genanki.guid_for(self.fields[2])


@type_enforced.Enforcer(enabled=enforcer.ENABLED)
class deck:
def __init__(
self,
Expand All @@ -79,10 +74,10 @@ def __init__(
deck_description: str,
css: str,
javascript: str,
key: field.Field,
front: field.Field,
back: field.Field,
title: field.Field,
key: field.field,
front: field.field,
back: field.field,
title: field.field,
force_key: bool = True,
force_no_duplicate_keys: bool = True,
force_front: bool = True,
Expand Down Expand Up @@ -185,6 +180,10 @@ def __init__(
else:
continue

assert isinstance(k, str)
assert isinstance(f, str)
assert isinstance(b, str)
assert isinstance(t, str)
self.keys.append(f"{deck_name} - {k}")
self.fronts.append(f)
self.backs.append(b)
Expand Down Expand Up @@ -229,8 +228,8 @@ def write_web(self, dir: str) -> None:
)
with open(os.path.join(dir, CSS_BASENAME), "w") as f:
f.write(self.css)
for f in self.media:
shutil.copy(f, dir)
for path in self.media:
shutil.copy(path, dir)
utils.wrote(dir)

def html_to_anki(self, html: str) -> str:
Expand Down
8 changes: 0 additions & 8 deletions flashcards/enforcer.py

This file was deleted.

Loading

0 comments on commit 5aeb7f5

Please sign in to comment.