Skip to content

Commit cf47df0

Browse files
kinds(cache): use dataclass for CacheEntry
Reduces the code to be written, maintains the behavior. Note that I preserved the `slots`.
1 parent 3caf111 commit cf47df0

File tree

1 file changed

+8
-20
lines changed
  • cmake_file_api/kinds/cache

1 file changed

+8
-20
lines changed

cmake_file_api/kinds/cache/v2.py

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from enum import Enum
1+
import dataclasses
2+
from enum import StrEnum
23
import json
34
from pathlib import Path
45
from typing import Any
@@ -7,7 +8,7 @@
78
from cmake_file_api.kinds.kind import ObjectKind
89

910

10-
class CacheEntryType(Enum):
11+
class CacheEntryType(StrEnum):
1112
TYPE_BOOL = "BOOL"
1213
TYPE_FILEPATH = "FILEPATH"
1314
TYPE_PATH = "PATH"
@@ -37,15 +38,12 @@ def __repr__(self) -> str:
3738
self.value,
3839
)
3940

40-
41+
@dataclasses.dataclass(frozen = True, slots = True)
4142
class CacheEntry:
42-
__slots__ = ("name", "value", "type", "properties")
43-
44-
def __init__(self, name: str, value: str, type: CacheEntryType, properties: list[CacheEntryProperty]):
45-
self.name = name
46-
self.value = value
47-
self.type = type
48-
self.properties = properties
43+
name : str
44+
value : str
45+
type : CacheEntryType
46+
properties : list[CacheEntryProperty]
4947

5048
@classmethod
5149
def from_dict(cls, dikt: dict[str, Any]) -> "CacheEntry":
@@ -55,16 +53,6 @@ def from_dict(cls, dikt: dict[str, Any]) -> "CacheEntry":
5553
properties = list(CacheEntryProperty.from_dict(cep) for cep in dikt["properties"])
5654
return cls(name, value, type, properties)
5755

58-
def __repr__(self) -> str:
59-
return "{}(name='{}', value='{}', type={}, properties={})".format(
60-
type(self).__name__,
61-
self.name,
62-
self.value,
63-
self.type.name,
64-
repr(self.properties),
65-
)
66-
67-
6856
class CacheV2:
6957
KIND = ObjectKind.CACHE
7058

0 commit comments

Comments
 (0)