|
1 | | -from inspect import isclass |
2 | 1 | from itertools import chain |
3 | 2 | from typing import Iterable, List, Tuple, Union |
4 | 3 |
|
5 | | -from .base import BaseType, ImportPathList, MetaData |
| 4 | +from .base import BaseType, ImportPathList, MetaData, get_hash_string |
6 | 5 | from .typing import metadata_to_typing |
7 | 6 |
|
8 | 7 |
|
9 | | -def get_hash_string(t: MetaData): |
10 | | - if isinstance(t, dict): |
11 | | - return str(hash(tuple((k, get_hash_string(v)) for k, v in t.items()))) |
12 | | - elif isclass(t): |
13 | | - return str(t) |
14 | | - elif isinstance(t, BaseType): |
15 | | - return t.to_hash_string() |
16 | | - |
17 | | - |
18 | 8 | class SingleType(BaseType): |
19 | | - __slots__ = ["_type"] |
| 9 | + __slots__ = ["_type", "_hash"] |
20 | 10 |
|
21 | 11 | def __init__(self, t: MetaData): |
22 | 12 | self._type = t |
@@ -47,14 +37,12 @@ def replace(self, t: 'MetaData', **kwargs) -> 'SingleType': |
47 | 37 | self.type = t |
48 | 38 | return self |
49 | 39 |
|
50 | | - def to_hash_string(self) -> str: |
51 | | - if not self._hash: |
52 | | - self._hash = f"{type(self).__name__}/{get_hash_string(self.type)}" |
53 | | - return self._hash |
| 40 | + def _to_hash_string(self) -> str: |
| 41 | + return f"{type(self).__name__}/{get_hash_string(self.type)}" |
54 | 42 |
|
55 | 43 |
|
56 | 44 | class ComplexType(BaseType): |
57 | | - __slots__ = ["_types"] |
| 45 | + __slots__ = ["_types", "_sorted", "_hash"] |
58 | 46 |
|
59 | 47 | def __init__(self, *types: MetaData): |
60 | 48 | self._types = list(types) |
@@ -125,10 +113,8 @@ def to_typing_code(self) -> Tuple[ImportPathList, str]: |
125 | 113 | f"[{nested}]" |
126 | 114 | ) |
127 | 115 |
|
128 | | - def to_hash_string(self) -> str: |
129 | | - if not self._hash: |
130 | | - self._hash = type(self).__name__ + "/" + ",".join(map(get_hash_string, self.types)) |
131 | | - return self._hash |
| 116 | + def _to_hash_string(self) -> str: |
| 117 | + return type(self).__name__ + "/" + ",".join(map(get_hash_string, self.types)) |
132 | 118 |
|
133 | 119 |
|
134 | 120 | class DOptional(SingleType): |
|
0 commit comments