Skip to content

Commit

Permalink
use from __future__ import annotations and get rid of quotation aro…
Browse files Browse the repository at this point in the history
…und types
  • Loading branch information
ilius committed Oct 23, 2024
1 parent a934ff7 commit e9a9571
Show file tree
Hide file tree
Showing 113 changed files with 826 additions and 697 deletions.
6 changes: 4 additions & 2 deletions pyglossary/apple_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
# also see:
# https://github.com/servo/servo/blob/master/components/style/properties/counted_unknown_properties.py

from __future__ import annotations

import re

from .core import log
Expand All @@ -35,7 +37,7 @@
rb"[ \t]*(" + b"|".join(cssKeyRemove) + rb")\s*:[^;}]*;\s*",
)

cssMapping: "dict[str, str]" = {
cssMapping: dict[str, str] = {
# I didn't actually find these font values:
"-apple-system-body": '"Helvetica Neue"',
"-apple-system": '"Helvetica Neue"',
Expand Down Expand Up @@ -98,7 +100,7 @@
)


def _subCSS(m: "re.Match") -> bytes:
def _subCSS(m: re.Match) -> bytes:
b_key = m.group(0)
value = cssMapping.get(b_key.decode("ascii"))
if value is None:
Expand Down
5 changes: 3 additions & 2 deletions pyglossary/compression.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
from __future__ import annotations

import logging
import os
Expand Down Expand Up @@ -85,7 +86,7 @@ def zipFileOrDir(filename: str) -> None:

from .os_utils import indir

def _zipFileAdd(zf: "zipfile.ZipFile", filename: str) -> None:
def _zipFileAdd(zf: zipfile.ZipFile, filename: str) -> None:
if isfile(filename):
zf.write(filename)
return
Expand Down Expand Up @@ -115,7 +116,7 @@ def _zipFileAdd(zf: "zipfile.ZipFile", filename: str) -> None:
_zipFileAdd(zf, fname)


def compress(_glos: "GlossaryType", filename: str, compression: str) -> str:
def compress(_glos: GlossaryType, filename: str, compression: str) -> str:
"""
Filename is the existing file path.
Expand Down
22 changes: 12 additions & 10 deletions pyglossary/core.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import inspect
import logging
import os
Expand Down Expand Up @@ -78,11 +80,11 @@ def trace(log: logging.Logger, msg: str) -> None:
class _Formatter(logging.Formatter):
def __init__(self, *args, **kwargs) -> None: # noqa: ANN101
logging.Formatter.__init__(self, *args, **kwargs)
self.fill: "Callable[[str], str] | None" = None
self.fill: Callable[[str], str] | None = None

def formatMessage(
self,
record: "logging.LogRecord",
record: logging.LogRecord,
) -> str:
msg = logging.Formatter.formatMessage(self, record)
if self.fill is not None:
Expand Down Expand Up @@ -125,7 +127,7 @@ def getVerbosity(self) -> int:
def trace(self, msg: str) -> None:
self.log(TRACE, msg)

def pretty(self, data: "Any", header: str = "") -> None:
def pretty(self, data: Any, header: str = "") -> None:
from pprint import pformat

self.debug(header + pformat(data))
Expand All @@ -144,7 +146,7 @@ def setTimeEnable(self, timeEnable: bool) -> None:
for handler in self.handlers:
handler.setFormatter(formatter)

def addHandler(self, hdlr: "logging.Handler") -> None:
def addHandler(self, hdlr: logging.Handler) -> None:
# if want to add separate format (new config keys and flags) for ui_gtk
# and ui_tk, you need to remove this function and run handler.setFormatter
# in ui_gtk and ui_tk
Expand All @@ -153,7 +155,7 @@ def addHandler(self, hdlr: "logging.Handler") -> None:


def _formatVarDict(
dct: "dict[str, Any]",
dct: dict[str, Any],
indent: int = 4,
max_width: int = 80,
) -> str:
Expand All @@ -174,7 +176,7 @@ def _formatVarDict(


def format_exception(
exc_info: "ExcInfoType | None" = None,
exc_info: ExcInfoType | None = None,
add_locals: bool = False,
add_globals: bool = False,
) -> str:
Expand Down Expand Up @@ -341,8 +343,8 @@ def isDebug() -> bool:
if os.sep == "\\":

def _windows_show_exception(
_type: "type[BaseException]",
exc: "BaseException",
_type: type[BaseException],
exc: BaseException,
tback: "TracebackType | None",
) -> None:
if not (_type and exc and tback):
Expand All @@ -362,8 +364,8 @@ def _windows_show_exception(
else:

def _unix_show_exception(
_type: "type[BaseException]",
exc: "BaseException",
_type: type[BaseException],
exc: BaseException,
tback: "TracebackType | None",
) -> None:
if not (_type and exc and tback):
Expand Down
32 changes: 17 additions & 15 deletions pyglossary/ebook_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

from __future__ import annotations

import logging
import os
import shutil
Expand All @@ -46,7 +48,7 @@


class GroupState:
def __init__(self, writer: "EbookWriter") -> None:
def __init__(self, writer: EbookWriter) -> None:
self.writer = writer
self.last_prefix = ""
self.group_index = -1
Expand All @@ -55,12 +57,12 @@ def __init__(self, writer: "EbookWriter") -> None:
def reset(self) -> None:
self.first_word = ""
self.last_word = ""
self.group_contents: "list[str]" = []
self.group_contents: list[str] = []

def is_new(self, prefix: str) -> bool:
return bool(self.last_prefix) and prefix != self.last_prefix

def add(self, entry: "EntryType", prefix: str) -> None:
def add(self, entry: EntryType, prefix: str) -> None:
word = entry.s_word
defi = entry.defi
if not self.first_word:
Expand Down Expand Up @@ -137,7 +139,7 @@ class EbookWriter:

def __init__(
self,
glos: "GlossaryType",
glos: GlossaryType,
escape_strings: bool = False,
# ignore_synonyms=False,
# flatten_synonyms=False,
Expand All @@ -157,9 +159,9 @@ def __init__(

self._tmpDir = tempfile.mkdtemp()
self.cover = ""
self.files: "list[dict[str, Any]]" = []
self.manifest_files: "list[dict[str, str]]" = []
self._group_labels: "list[str]" = []
self.files: list[dict[str, Any]] = []
self.manifest_files: list[dict[str, str]] = []
self._group_labels: list[str] = []

def finish(self) -> None:
self._filename = ""
Expand Down Expand Up @@ -245,13 +247,13 @@ def get_group_xhtml_file_name_from_index(self, index: int) -> str:
def get_prefix(self, word: str) -> str:
raise NotImplementedError

def sortKey(self, words: "list[str]") -> "Any":
def sortKey(self, words: list[str]) -> Any:
raise NotImplementedError

def _add_group(
self,
group_labels: "list[str]",
state: "GroupState",
group_labels: list[str],
state: GroupState,
) -> None:
if not state.last_prefix:
return
Expand Down Expand Up @@ -284,7 +286,7 @@ def _add_group(
"application/xhtml+xml",
)

def write_data_entry(self, entry: "EntryType") -> None:
def write_data_entry(self, entry: EntryType) -> None:
if entry.getFileName() == "style.css":
self.add_file_manifest(
"OEBPS/style.css",
Expand All @@ -297,7 +299,7 @@ def write_groups(self) -> "Generator[None, EntryType, None]":
# TODO: rtl=False option
# TODO: handle alternates better (now shows word1|word2... in title)

group_labels: "list[str]" = []
group_labels: list[str] = []

state = GroupState(self)
while True:
Expand All @@ -323,7 +325,7 @@ def format_group_content(
self,
word: str,
defi: str,
variants: "list[str] | None" = None, # noqa: ARG002
variants: list[str] | None = None, # noqa: ARG002
) -> str:
return self.GROUP_XHTML_WORD_DEFINITION_TEMPLATE.format(
headword=self.escape_if_needed(word),
Expand All @@ -342,7 +344,7 @@ def escape_if_needed(self, string: str) -> str:
.replace("<", "&lt;")
)

def write_index(self, group_labels: "list[str]") -> None:
def write_index(self, group_labels: list[str]) -> None:
"""group_labels: a list of labels."""
links = []
for label_i, label in enumerate(group_labels):
Expand Down Expand Up @@ -420,7 +422,7 @@ def write_opf(self) -> None:

self.add_file("OEBPS/content.opf", opf_contents)

def write_ncx(self, group_labels: "list[str]") -> None:
def write_ncx(self, group_labels: list[str]) -> None:
"""
write_ncx.
Expand Down
4 changes: 2 additions & 2 deletions pyglossary/entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def s_word(self) -> str:
return self._fname

@property
def l_word(self) -> "list[str]":
def l_word(self) -> list[str]:
return [self._fname]

@property
Expand Down Expand Up @@ -259,7 +259,7 @@ def s_word(self) -> str:
return joinByBar(self._word)

@property
def l_word(self) -> "list[str]":
def l_word(self) -> list[str]:
"""Returns list of the word and all the alternate words."""
if isinstance(self._word, str):
return [self._word]
Expand Down
2 changes: 1 addition & 1 deletion pyglossary/entry_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


class BaseEntry:
__slots__: "list[str]" = [
__slots__: list[str] = [
"_word",
]

Expand Down
Loading

0 comments on commit e9a9571

Please sign in to comment.