Skip to content

Commit f8d7f04

Browse files
committed
Refactor type hints to use Final for constants across multiple files
Signed-off-by: Tsuyoshi Hombashi <tsuyoshi.hombashi@gmail.com>
1 parent 25b7124 commit f8d7f04

File tree

10 files changed

+55
-44
lines changed

10 files changed

+55
-44
lines changed

pathvalidate/__version__.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
__author__ = "Tsuyoshi Hombashi"
2-
__copyright__ = f"Copyright 2016-2024, {__author__}"
3-
__license__ = "MIT License"
1+
from typing import Final
2+
3+
4+
__author__: Final = "Tsuyoshi Hombashi"
5+
__copyright__: Final = f"Copyright 2016-2024, {__author__}"
6+
__license__: Final = "MIT License"
47
__version__ = "3.2.1"
5-
__maintainer__ = __author__
6-
__email__ = "tsuyoshi.hombashi@gmail.com"
8+
__maintainer__: Final = __author__
9+
__email__: Final = "tsuyoshi.hombashi@gmail.com"

pathvalidate/_base.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import re
88
import sys
99
from collections.abc import Sequence
10-
from typing import ClassVar, Final, Optional
10+
from typing import Final, Optional
1111

1212
from ._common import normalize_platform, unprintable_ascii_chars
1313
from ._const import DEFAULT_MIN_LEN, Platform
@@ -17,10 +17,10 @@
1717

1818

1919
class BaseFile:
20-
_INVALID_PATH_CHARS: ClassVar[str] = "".join(unprintable_ascii_chars)
21-
_INVALID_FILENAME_CHARS: ClassVar[str] = _INVALID_PATH_CHARS + "/"
22-
_INVALID_WIN_PATH_CHARS: ClassVar[str] = _INVALID_PATH_CHARS + ':*?"<>|\t\n\r\x0b\x0c'
23-
_INVALID_WIN_FILENAME_CHARS: ClassVar[str] = (
20+
_INVALID_PATH_CHARS: Final[str] = "".join(unprintable_ascii_chars)
21+
_INVALID_FILENAME_CHARS: Final[str] = _INVALID_PATH_CHARS + "/"
22+
_INVALID_WIN_PATH_CHARS: Final[str] = _INVALID_PATH_CHARS + ':*?"<>|\t\n\r\x0b\x0c'
23+
_INVALID_WIN_FILENAME_CHARS: Final[str] = (
2424
_INVALID_FILENAME_CHARS + _INVALID_WIN_PATH_CHARS + "\\"
2525
)
2626

pathvalidate/_common.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
import string
99
import sys
1010
from pathlib import PurePath
11-
from typing import Any, Optional
11+
from typing import Any, Final, Optional
1212

1313
from ._const import Platform
1414
from ._types import PathType, PlatformType
1515

1616

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

1919

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

7777

78-
unprintable_ascii_chars = tuple(_get_unprintable_ascii_chars())
78+
unprintable_ascii_chars: Final = tuple(_get_unprintable_ascii_chars())
7979

8080

8181
def _get_ascii_symbols() -> list[str]:
@@ -92,12 +92,12 @@ def _get_ascii_symbols() -> list[str]:
9292
return symbol_list
9393

9494

95-
ascii_symbols = tuple(_get_ascii_symbols())
95+
ascii_symbols: Final = tuple(_get_ascii_symbols())
9696

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

pathvalidate/_const.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import enum
2+
from typing import Final
23

34

4-
DEFAULT_MIN_LEN = 1
5-
INVALID_CHAR_ERR_MSG_TMPL = "invalids=({invalid}), value={value}"
5+
DEFAULT_MIN_LEN: Final = 1
6+
INVALID_CHAR_ERR_MSG_TMPL: Final = "invalids=({invalid}), value={value}"
67

78

8-
_NTFS_RESERVED_FILE_NAMES = (
9+
_NTFS_RESERVED_FILE_NAMES: Final = (
910
"$Mft",
1011
"$MftMirr",
1112
"$LogFile",

pathvalidate/_filename.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from collections.abc import Sequence
1010
from pathlib import Path, PurePath
1111
from re import Pattern
12-
from typing import Optional
12+
from typing import Final, Optional
1313

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

2121

22-
_DEFAULT_MAX_FILENAME_LEN = 255
23-
_RE_INVALID_FILENAME = re.compile(f"[{re.escape(BaseFile._INVALID_FILENAME_CHARS):s}]", re.UNICODE)
24-
_RE_INVALID_WIN_FILENAME = re.compile(
22+
_DEFAULT_MAX_FILENAME_LEN: Final = 255
23+
_RE_INVALID_FILENAME: Final = re.compile(
24+
f"[{re.escape(BaseFile._INVALID_FILENAME_CHARS):s}]", re.UNICODE
25+
)
26+
_RE_INVALID_WIN_FILENAME: Final = re.compile(
2527
f"[{re.escape(BaseFile._INVALID_WIN_FILENAME_CHARS):s}]", re.UNICODE
2628
)
2729

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

123125

124126
class FileNameValidator(BaseValidator):
125-
_WINDOWS_RESERVED_FILE_NAMES = (
127+
_WINDOWS_RESERVED_FILE_NAMES: Final = (
126128
("CON", "PRN", "AUX", "CLOCK$", "NUL")
127129
+ tuple(f"{name:s}{num:d}" for name, num in itertools.product(("COM", "LPT"), range(0, 10)))
128130
+ tuple(
@@ -133,7 +135,7 @@ class FileNameValidator(BaseValidator):
133135
)
134136
)
135137
)
136-
_MACOS_RESERVED_FILE_NAMES = (":",)
138+
_MACOS_RESERVED_FILE_NAMES: Final = (":",)
137139

138140
@property
139141
def reserved_keywords(self) -> tuple[str, ...]:

pathvalidate/_filepath.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from collections.abc import Sequence
1111
from pathlib import Path, PurePath
1212
from re import Pattern
13-
from typing import Optional
13+
from typing import Final, Optional
1414

1515
from ._base import AbstractSanitizer, AbstractValidator, BaseFile, BaseValidator
1616
from ._common import findall_to_str, is_nt_abspath, to_str, validate_pathtype
@@ -21,8 +21,10 @@
2121
from .handler import ReservedNameHandler, ValidationErrorHandler
2222

2323

24-
_RE_INVALID_PATH = re.compile(f"[{re.escape(BaseFile._INVALID_PATH_CHARS):s}]", re.UNICODE)
25-
_RE_INVALID_WIN_PATH = re.compile(f"[{re.escape(BaseFile._INVALID_WIN_PATH_CHARS):s}]", re.UNICODE)
24+
_RE_INVALID_PATH: Final = re.compile(f"[{re.escape(BaseFile._INVALID_PATH_CHARS):s}]", re.UNICODE)
25+
_RE_INVALID_WIN_PATH: Final = re.compile(
26+
f"[{re.escape(BaseFile._INVALID_WIN_PATH_CHARS):s}]", re.UNICODE
27+
)
2628

2729

2830
class FilePathSanitizer(AbstractSanitizer):
@@ -142,11 +144,11 @@ def __get_path_separator(self) -> str:
142144

143145

144146
class FilePathValidator(BaseValidator):
145-
_RE_NTFS_RESERVED = re.compile(
147+
_RE_NTFS_RESERVED: Final = re.compile(
146148
"|".join(f"^/{re.escape(pattern)}$" for pattern in _NTFS_RESERVED_FILE_NAMES),
147149
re.IGNORECASE,
148150
)
149-
_MACOS_RESERVED_FILE_PATHS = ("/", ":")
151+
_MACOS_RESERVED_FILE_PATHS: Final = ("/", ":")
150152

151153
@property
152154
def reserved_keywords(self) -> tuple[str, ...]:

pathvalidate/_ltsv.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
"""
44

55
import re
6+
from typing import Final
67

78
from ._common import to_str, validate_pathtype
89
from .error import InvalidCharError
910

1011

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

1314

1415
def validate_ltsv_label(label: str) -> None:

pathvalidate/_symbol.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44

55
import re
66
from collections.abc import Sequence
7+
from typing import Final
78

89
from ._common import ascii_symbols, to_str, unprintable_ascii_chars
910
from .error import InvalidCharError
1011

1112

12-
__RE_SYMBOL = re.compile(
13+
__RE_SYMBOL: Final = re.compile(
1314
"[{}]".format(re.escape("".join(ascii_symbols + unprintable_ascii_chars))), re.UNICODE
1415
)
1516

pathvalidate/error.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44

55
import enum
6-
from typing import Optional
6+
from typing import Final, Optional
77

88
from ._const import Platform
99

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

1414

1515
class ErrorAttrKey:
16-
BYTE_COUNT = "byte_count"
17-
DESCRIPTION = "description"
18-
FS_ENCODING = "fs_encoding"
19-
PLATFORM = "platform"
20-
REASON = "reason"
21-
RESERVED_NAME = "reserved_name"
22-
REUSABLE_NAME = "reusable_name"
16+
BYTE_COUNT: Final = "byte_count"
17+
DESCRIPTION: Final = "description"
18+
FS_ENCODING: Final = "fs_encoding"
19+
PLATFORM: Final = "platform"
20+
REASON: Final = "reason"
21+
RESERVED_NAME: Final = "reserved_name"
22+
REUSABLE_NAME: Final = "reusable_name"
2323

2424

2525
@enum.unique

setup.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
"""
44

55
import os.path
6+
from typing import Final
67

78
import setuptools
89

910

10-
MODULE_NAME = "pathvalidate"
11-
REPOSITORY_URL = f"https://github.com/thombashi/{MODULE_NAME:s}"
12-
REQUIREMENT_DIR = "requirements"
13-
ENCODING = "utf8"
11+
MODULE_NAME: Final = "pathvalidate"
12+
REPOSITORY_URL: Final = f"https://github.com/thombashi/{MODULE_NAME:s}"
13+
REQUIREMENT_DIR: Final = "requirements"
14+
ENCODING: Final = "utf8"
1415

1516
pkg_info: dict[str, str] = {}
1617

0 commit comments

Comments
 (0)