Skip to content

Commit

Permalink
refactor: add Final and TypeAlias annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
linuxdaemon committed Apr 15, 2024
1 parent c4034c8 commit 612ba7f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 21 deletions.
35 changes: 18 additions & 17 deletions irclib/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from abc import ABCMeta, abstractmethod
from typing import (
Dict,
Final,
Iterable,
Iterator,
List,
Expand All @@ -19,7 +20,7 @@
cast,
)

from typing_extensions import Self
from typing_extensions import Self, TypeAlias

from irclib.errors import ParseError

Expand All @@ -32,37 +33,37 @@
"ParamList",
"Message",
)
MsgTagList = Optional["TagList"]
MsgPrefix = Optional["Prefix"]
MessageTuple = Tuple[MsgTagList, MsgPrefix, str, "ParamList"]
MsgTagList: TypeAlias = Optional["TagList"]
MsgPrefix: TypeAlias = Optional["Prefix"]
MessageTuple: TypeAlias = Tuple[MsgTagList, MsgPrefix, str, "ParamList"]

TAGS_SENTINEL = "@"
TAGS_SEP = ";"
TAG_VALUE_SEP = "="
TAGS_SENTINEL: Final = "@"
TAGS_SEP: Final = ";"
TAG_VALUE_SEP: Final = "="

PREFIX_SENTINEL = ":"
PREFIX_USER_SEP = "!"
PREFIX_HOST_SEP = "@"
PREFIX_SENTINEL: Final = ":"
PREFIX_USER_SEP: Final = "!"
PREFIX_HOST_SEP: Final = "@"

PARAM_SEP = " "
TRAIL_SENTINEL = ":"
PARAM_SEP: Final = " "
TRAIL_SENTINEL: Final = ":"

CAP_SEP = " "
CAP_VALUE_SEP = "="
CAP_SEP: Final = " "
CAP_VALUE_SEP: Final = "="

PREFIX_RE = re.compile(
PREFIX_RE: Final = re.compile(
r"^:?(?P<nick>.*?)(?:!(?P<user>.*?))?(?:@(?P<host>.*?))?$"
)

TAG_VALUE_ESCAPES = {
TAG_VALUE_ESCAPES: Final = {
"\\s": " ",
"\\:": ";",
"\\r": "\r",
"\\n": "\n",
"\\\\": "\\",
}

TAG_VALUE_UNESCAPES = {
TAG_VALUE_UNESCAPES: Final = {
unescaped: escaped for escaped, unescaped in TAG_VALUE_ESCAPES.items()
}

Expand Down
3 changes: 2 additions & 1 deletion irclib/util/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
"""

import re
from typing import Final

__all__ = ("match_mask",)

GLOB_MAP = {"?": ".", "*": ".*"}
GLOB_MAP: Final = {"?": ".", "*": ".*"}


def match_mask(mask: str, pattern: str) -> bool:
Expand Down
7 changes: 4 additions & 3 deletions irclib/util/string.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from typing import (
Callable,
Dict,
Final,
List,
NamedTuple,
Optional,
Expand Down Expand Up @@ -39,13 +40,13 @@ def upper_table(self) -> Dict[int, int]:
return str.maketrans(self.upper, self.lower)


RFC1459 = Casemap(
RFC1459: Final = Casemap(
"".join(map(chr, range(65, 95))), "".join(map(chr, range(97, 127)))
)
STRICT_RFC1459 = Casemap(
STRICT_RFC1459: Final = Casemap(
"".join(map(chr, range(65, 94))), "".join(map(chr, range(97, 126)))
)
ASCII = Casemap(
ASCII: Final = Casemap(
"".join(map(chr, range(65, 91))), "".join(map(chr, range(97, 123)))
)

Expand Down

0 comments on commit 612ba7f

Please sign in to comment.