Skip to content

Commit 3a845c3

Browse files
committed
Cache hash strings
1 parent 59ee7d5 commit 3a845c3

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

rest_client_gen/dynamic_typing/complex.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,20 @@ def get_hash_string(t: MetaData):
1616

1717

1818
class SingleType(BaseType):
19-
__slots__ = ["type"]
19+
__slots__ = ["_type"]
2020

2121
def __init__(self, t: MetaData):
22-
self.type = t
22+
self._type = t
23+
self._hash = None
24+
25+
@property
26+
def type(self):
27+
return self._type
28+
29+
@type.setter
30+
def type(self, t: MetaData):
31+
self._type = t
32+
self._hash = None
2333

2434
def __str__(self):
2535
return f"{type(self).__name__}[{self.type}]"
@@ -38,14 +48,18 @@ def replace(self, t: 'MetaData', **kwargs) -> 'SingleType':
3848
return self
3949

4050
def to_hash_string(self) -> str:
41-
return f"{type(self).__name__}/{get_hash_string(self.type)}"
51+
if not self._hash:
52+
self._hash = f"{type(self).__name__}/{get_hash_string(self.type)}"
53+
return self._hash
4254

4355

4456
class ComplexType(BaseType):
4557
__slots__ = ["_types"]
4658

4759
def __init__(self, *types: MetaData):
4860
self._types = list(types)
61+
self._sorted = None
62+
self._hash = None
4963

5064
@property
5165
def types(self):
@@ -55,6 +69,7 @@ def types(self):
5569
def types(self, value):
5670
self._types = value
5771
self._sorted = None
72+
self._hash = None
5873

5974
@property
6075
def sorted(self):
@@ -111,7 +126,9 @@ def to_typing_code(self) -> Tuple[ImportPathList, str]:
111126
)
112127

113128
def to_hash_string(self) -> str:
114-
return type(self).__name__ + "/" + ",".join(map(get_hash_string, self.types))
129+
if not self._hash:
130+
self._hash = type(self).__name__ + "/" + ",".join(map(get_hash_string, self.types))
131+
return self._hash
115132

116133

117134
class DOptional(SingleType):

0 commit comments

Comments
 (0)