Skip to content

Commit 6f495ff

Browse files
committed
annotating Enum.value
see also: python/typing#535
1 parent 9da909b commit 6f495ff

7 files changed

Lines changed: 34 additions & 11 deletions

File tree

prime_backup/cli/return_codes.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import enum
22
import sys
3+
from typing import TYPE_CHECKING
34

45
from typing_extensions import NoReturn
56

@@ -12,5 +13,8 @@ class ErrorReturnCodes(enum.Enum):
1213
backup_file_not_found = 5
1314
missing_dependency = 6
1415

16+
if TYPE_CHECKING:
17+
value: int
18+
1519
def sys_exit(self) -> NoReturn:
1620
sys.exit(self.value)

prime_backup/compressors.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import dataclasses
33
import enum
44
from abc import abstractmethod, ABC
5-
from typing import BinaryIO, Union, ContextManager, Tuple, Callable, Literal, Generator
5+
from typing import BinaryIO, Union, ContextManager, Tuple, Callable, Literal, Generator, TYPE_CHECKING, Type
66

77
from typing_extensions import Protocol, override
88

@@ -222,5 +222,8 @@ class CompressMethod(enum.Enum):
222222
zstd = ZstdCompressor
223223
lz4 = Lz4Compressor
224224

225+
if TYPE_CHECKING:
226+
value: Type[Compressor]
227+
225228
def __repr__(self) -> str:
226229
return '{}({!r})'.format(self.__class__.__name__, self.name)

prime_backup/types/backup_tags.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import enum
2-
from typing import Optional, Any, Type, List
2+
from typing import Optional, Any, Type, List, TYPE_CHECKING
33

44
from mcdreforged.api.all import RText, RTextBase, RColor
55

@@ -31,6 +31,9 @@ class BackupTagName(enum.Enum):
3131
protected = BackupTagValue(bool, 'P', RColor.dark_green)
3232
scheduled = BackupTagValue(bool, 'S', RColor.dark_blue)
3333

34+
if TYPE_CHECKING:
35+
value: BackupTagValue
36+
3437
@classmethod
3538
def bool_tags(cls) -> List['BackupTagName']:
3639
return [

prime_backup/types/chunk_method.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import enum
22
from pathlib import Path
3-
from typing import Optional, IO
3+
from typing import Optional, IO, TYPE_CHECKING
44

5-
from prime_backup.types.chunker_factory import CDCChunkerFactory, FixedSizeChunkerFactory
5+
from prime_backup.types.chunker_factory import ChunkerFactory, CDCChunkerFactory, FixedSizeChunkerFactory
66
from prime_backup.utils.chunker import Chunker
77
from prime_backup.utils.path_like import PathLike
88

@@ -18,6 +18,9 @@ class ChunkMethod(enum.Enum):
1818
fixed_32k = FixedSizeChunkerFactory(32 * 1024)
1919
fixed_128k = FixedSizeChunkerFactory(128 * 1024)
2020

21+
if TYPE_CHECKING:
22+
value: ChunkerFactory
23+
2124
@classmethod
2225
def get_for_file(cls, file_path: PathLike, file_size: int) -> Optional['ChunkMethod']:
2326
"""

prime_backup/types/hash_method.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import dataclasses
22
import enum
33
import importlib
4-
from typing import Protocol, Union
4+
from typing import Protocol, Union, TYPE_CHECKING
55

66

77
class Hasher(Protocol):
@@ -30,6 +30,9 @@ class HashMethod(enum.Enum):
3030
sha256 = _HashMethodItem('hashlib.sha256', 64)
3131
blake3 = _HashMethodItem('blake3.blake3', 64)
3232

33+
if TYPE_CHECKING:
34+
value: _HashMethodItem
35+
3336

3437
def __verify_hex_length():
3538
for hash_method in HashMethod:

prime_backup/types/standalone_backup_format.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import dataclasses
22
import enum
33
import os
4-
from typing import Optional, List
4+
from typing import Optional, List, Union, TYPE_CHECKING
55

66
from prime_backup.types.tar_format import TarFormat
77
from prime_backup.utils.path_like import PathLike
@@ -24,12 +24,16 @@ class StandaloneBackupFormat(enum.Enum):
2424
tar_zst = TarFormat.zstd
2525
zip = ZipFormat('.zip')
2626

27+
if TYPE_CHECKING:
28+
value: Union[TarFormat, ZipFormat]
29+
2730
@property
2831
def __all_file_extensions(self) -> List[str]:
29-
if isinstance(self.value, TarFormat):
30-
return self.value.value.all_extensions
31-
elif isinstance(self.value, ZipFormat):
32-
return self.value.all_extensions
32+
format_value = self.value
33+
if isinstance(format_value, TarFormat):
34+
return format_value.value.all_extensions
35+
elif isinstance(format_value, ZipFormat):
36+
return format_value.all_extensions
3337
else:
3438
raise ValueError(self.value)
3539

prime_backup/types/tar_format.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import dataclasses
22
import enum
3-
from typing import Tuple, List, Literal, cast
3+
from typing import Tuple, List, Literal, cast, TYPE_CHECKING
44

55
from prime_backup.compressors import CompressMethod
66

@@ -35,6 +35,9 @@ class TarFormat(enum.Enum):
3535
lzma = _TarFormatItem('.tar.xz', ('.txz',), ':xz', CompressMethod.plain)
3636
zstd = _TarFormatItem('.tar.zst', ('.tar.zstd', '.tzst', '.tzstd'), ':', CompressMethod.zstd)
3737

38+
if TYPE_CHECKING:
39+
value: _TarFormatItem
40+
3841

3942
def __validate_tar_formats():
4043
for tf in TarFormat:

0 commit comments

Comments
 (0)