66from copy import copy , deepcopy
77from collections import defaultdict , deque
88from pathlib import Path
9- from typing import Mapping , Tuple , TypeVar
9+ from typing import Mapping , Optional , Tuple , TypeVar
1010import warnings
1111
1212from linkml_runtime .utils .namespaces import Namespaces
@@ -145,6 +145,11 @@ class SchemaView(object):
145145 modifications : int = 0
146146 uuid : str = None
147147
148+ ## private vars --------
149+ # cached hash
150+ _hash : Optional [int ] = None
151+
152+
148153 def __init__ (self , schema : Union [str , Path , SchemaDefinition ],
149154 importmap : Optional [Dict [str , str ]] = None , merge_imports : bool = False , base_dir : str = None ):
150155 if isinstance (schema , Path ):
@@ -166,8 +171,10 @@ def __eq__(self, other):
166171 return self .__key () == other .__key ()
167172 return NotImplemented
168173
169- def __hash__ (self ):
170- return hash (self .__key ())
174+ def __hash__ (self ) -> int :
175+ if self ._hash is None :
176+ self ._hash = hash (self .__key ())
177+ return self ._hash
171178
172179 @lru_cache (None )
173180 def namespaces (self ) -> Namespaces :
@@ -1850,6 +1857,7 @@ def copy_schema(self, new_name: str = None) -> SchemaDefinition:
18501857 return s2
18511858
18521859 def set_modified (self ) -> None :
1860+ self ._hash = None
18531861 self .modifications += 1
18541862
18551863 def materialize_patterns (self ) -> None :
0 commit comments