Skip to content

Commit

Permalink
Refactor type hints to use Final for constants across multiple files
Browse files Browse the repository at this point in the history
Signed-off-by: Tsuyoshi Hombashi <[email protected]>
  • Loading branch information
thombashi committed Jan 1, 2025
1 parent 25b7124 commit f8d7f04
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 44 deletions.
13 changes: 8 additions & 5 deletions pathvalidate/__version__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
__author__ = "Tsuyoshi Hombashi"
__copyright__ = f"Copyright 2016-2024, {__author__}"
__license__ = "MIT License"
from typing import Final


__author__: Final = "Tsuyoshi Hombashi"
__copyright__: Final = f"Copyright 2016-2024, {__author__}"
__license__: Final = "MIT License"
__version__ = "3.2.1"
__maintainer__ = __author__
__email__ = "[email protected]"
__maintainer__: Final = __author__
__email__: Final = "[email protected]"
10 changes: 5 additions & 5 deletions pathvalidate/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import re
import sys
from collections.abc import Sequence
from typing import ClassVar, Final, Optional
from typing import Final, Optional

from ._common import normalize_platform, unprintable_ascii_chars
from ._const import DEFAULT_MIN_LEN, Platform
Expand All @@ -17,10 +17,10 @@


class BaseFile:
_INVALID_PATH_CHARS: ClassVar[str] = "".join(unprintable_ascii_chars)
_INVALID_FILENAME_CHARS: ClassVar[str] = _INVALID_PATH_CHARS + "/"
_INVALID_WIN_PATH_CHARS: ClassVar[str] = _INVALID_PATH_CHARS + ':*?"<>|\t\n\r\x0b\x0c'
_INVALID_WIN_FILENAME_CHARS: ClassVar[str] = (
_INVALID_PATH_CHARS: Final[str] = "".join(unprintable_ascii_chars)
_INVALID_FILENAME_CHARS: Final[str] = _INVALID_PATH_CHARS + "/"
_INVALID_WIN_PATH_CHARS: Final[str] = _INVALID_PATH_CHARS + ':*?"<>|\t\n\r\x0b\x0c'
_INVALID_WIN_FILENAME_CHARS: Final[str] = (
_INVALID_FILENAME_CHARS + _INVALID_WIN_PATH_CHARS + "\\"
)

Expand Down
12 changes: 6 additions & 6 deletions pathvalidate/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
import string
import sys
from pathlib import PurePath
from typing import Any, Optional
from typing import Any, Final, Optional

from ._const import Platform
from ._types import PathType, PlatformType


_re_whitespaces = re.compile(r"^[\s]+$")
_re_whitespaces: Final = re.compile(r"^[\s]+$")


def validate_pathtype(
Expand Down Expand Up @@ -75,7 +75,7 @@ def _get_unprintable_ascii_chars() -> list[str]:
return [chr(c) for c in range(128) if chr(c) not in string.printable]


unprintable_ascii_chars = tuple(_get_unprintable_ascii_chars())
unprintable_ascii_chars: Final = tuple(_get_unprintable_ascii_chars())


def _get_ascii_symbols() -> list[str]:
Expand All @@ -92,12 +92,12 @@ def _get_ascii_symbols() -> list[str]:
return symbol_list


ascii_symbols = tuple(_get_ascii_symbols())
ascii_symbols: Final = tuple(_get_ascii_symbols())

__RE_UNPRINTABLE_CHARS = re.compile(
__RE_UNPRINTABLE_CHARS: Final = re.compile(
"[{}]".format(re.escape("".join(unprintable_ascii_chars))), re.UNICODE
)
__RE_ANSI_ESCAPE = re.compile(
__RE_ANSI_ESCAPE: Final = re.compile(
r"(?:\x1B[@-Z\\-_]|[\x80-\x9A\x9C-\x9F]|(?:\x1B\[|\x9B)[0-?]*[ -/]*[@-~])"
)

Expand Down
7 changes: 4 additions & 3 deletions pathvalidate/_const.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import enum
from typing import Final


DEFAULT_MIN_LEN = 1
INVALID_CHAR_ERR_MSG_TMPL = "invalids=({invalid}), value={value}"
DEFAULT_MIN_LEN: Final = 1
INVALID_CHAR_ERR_MSG_TMPL: Final = "invalids=({invalid}), value={value}"


_NTFS_RESERVED_FILE_NAMES = (
_NTFS_RESERVED_FILE_NAMES: Final = (
"$Mft",
"$MftMirr",
"$LogFile",
Expand Down
14 changes: 8 additions & 6 deletions pathvalidate/_filename.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from collections.abc import Sequence
from pathlib import Path, PurePath
from re import Pattern
from typing import Optional
from typing import Final, Optional

from ._base import AbstractSanitizer, AbstractValidator, BaseFile, BaseValidator
from ._common import findall_to_str, is_nt_abspath, to_str, truncate_str, validate_pathtype
Expand All @@ -19,9 +19,11 @@
from .handler import ReservedNameHandler, ValidationErrorHandler


_DEFAULT_MAX_FILENAME_LEN = 255
_RE_INVALID_FILENAME = re.compile(f"[{re.escape(BaseFile._INVALID_FILENAME_CHARS):s}]", re.UNICODE)
_RE_INVALID_WIN_FILENAME = re.compile(
_DEFAULT_MAX_FILENAME_LEN: Final = 255
_RE_INVALID_FILENAME: Final = re.compile(
f"[{re.escape(BaseFile._INVALID_FILENAME_CHARS):s}]", re.UNICODE
)
_RE_INVALID_WIN_FILENAME: Final = re.compile(
f"[{re.escape(BaseFile._INVALID_WIN_FILENAME_CHARS):s}]", re.UNICODE
)

Expand Down Expand Up @@ -122,7 +124,7 @@ def _get_sanitize_regexp(self) -> Pattern[str]:


class FileNameValidator(BaseValidator):
_WINDOWS_RESERVED_FILE_NAMES = (
_WINDOWS_RESERVED_FILE_NAMES: Final = (
("CON", "PRN", "AUX", "CLOCK$", "NUL")
+ tuple(f"{name:s}{num:d}" for name, num in itertools.product(("COM", "LPT"), range(0, 10)))
+ tuple(
Expand All @@ -133,7 +135,7 @@ class FileNameValidator(BaseValidator):
)
)
)
_MACOS_RESERVED_FILE_NAMES = (":",)
_MACOS_RESERVED_FILE_NAMES: Final = (":",)

@property
def reserved_keywords(self) -> tuple[str, ...]:
Expand Down
12 changes: 7 additions & 5 deletions pathvalidate/_filepath.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from collections.abc import Sequence
from pathlib import Path, PurePath
from re import Pattern
from typing import Optional
from typing import Final, Optional

from ._base import AbstractSanitizer, AbstractValidator, BaseFile, BaseValidator
from ._common import findall_to_str, is_nt_abspath, to_str, validate_pathtype
Expand All @@ -21,8 +21,10 @@
from .handler import ReservedNameHandler, ValidationErrorHandler


_RE_INVALID_PATH = re.compile(f"[{re.escape(BaseFile._INVALID_PATH_CHARS):s}]", re.UNICODE)
_RE_INVALID_WIN_PATH = re.compile(f"[{re.escape(BaseFile._INVALID_WIN_PATH_CHARS):s}]", re.UNICODE)
_RE_INVALID_PATH: Final = re.compile(f"[{re.escape(BaseFile._INVALID_PATH_CHARS):s}]", re.UNICODE)
_RE_INVALID_WIN_PATH: Final = re.compile(
f"[{re.escape(BaseFile._INVALID_WIN_PATH_CHARS):s}]", re.UNICODE
)


class FilePathSanitizer(AbstractSanitizer):
Expand Down Expand Up @@ -142,11 +144,11 @@ def __get_path_separator(self) -> str:


class FilePathValidator(BaseValidator):
_RE_NTFS_RESERVED = re.compile(
_RE_NTFS_RESERVED: Final = re.compile(
"|".join(f"^/{re.escape(pattern)}$" for pattern in _NTFS_RESERVED_FILE_NAMES),
re.IGNORECASE,
)
_MACOS_RESERVED_FILE_PATHS = ("/", ":")
_MACOS_RESERVED_FILE_PATHS: Final = ("/", ":")

@property
def reserved_keywords(self) -> tuple[str, ...]:
Expand Down
3 changes: 2 additions & 1 deletion pathvalidate/_ltsv.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
"""

import re
from typing import Final

from ._common import to_str, validate_pathtype
from .error import InvalidCharError


__RE_INVALID_LTSV_LABEL = re.compile("[^0-9A-Za-z_.-]", re.UNICODE)
__RE_INVALID_LTSV_LABEL: Final = re.compile("[^0-9A-Za-z_.-]", re.UNICODE)


def validate_ltsv_label(label: str) -> None:
Expand Down
3 changes: 2 additions & 1 deletion pathvalidate/_symbol.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

import re
from collections.abc import Sequence
from typing import Final

from ._common import ascii_symbols, to_str, unprintable_ascii_chars
from .error import InvalidCharError


__RE_SYMBOL = re.compile(
__RE_SYMBOL: Final = re.compile(
"[{}]".format(re.escape("".join(ascii_symbols + unprintable_ascii_chars))), re.UNICODE
)

Expand Down
16 changes: 8 additions & 8 deletions pathvalidate/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""

import enum
from typing import Optional
from typing import Final, Optional

from ._const import Platform

Expand All @@ -13,13 +13,13 @@ def _to_error_code(code: int) -> str:


class ErrorAttrKey:
BYTE_COUNT = "byte_count"
DESCRIPTION = "description"
FS_ENCODING = "fs_encoding"
PLATFORM = "platform"
REASON = "reason"
RESERVED_NAME = "reserved_name"
REUSABLE_NAME = "reusable_name"
BYTE_COUNT: Final = "byte_count"
DESCRIPTION: Final = "description"
FS_ENCODING: Final = "fs_encoding"
PLATFORM: Final = "platform"
REASON: Final = "reason"
RESERVED_NAME: Final = "reserved_name"
REUSABLE_NAME: Final = "reusable_name"


@enum.unique
Expand Down
9 changes: 5 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
"""

import os.path
from typing import Final

import setuptools


MODULE_NAME = "pathvalidate"
REPOSITORY_URL = f"https://github.com/thombashi/{MODULE_NAME:s}"
REQUIREMENT_DIR = "requirements"
ENCODING = "utf8"
MODULE_NAME: Final = "pathvalidate"
REPOSITORY_URL: Final = f"https://github.com/thombashi/{MODULE_NAME:s}"
REQUIREMENT_DIR: Final = "requirements"
ENCODING: Final = "utf8"

pkg_info: dict[str, str] = {}

Expand Down

0 comments on commit f8d7f04

Please sign in to comment.