File tree 2 files changed +26
-7
lines changed
2 files changed +26
-7
lines changed Original file line number Diff line number Diff line change @@ -255,12 +255,18 @@ def __eq__(self, other: Any) -> bool:
255
255
)
256
256
257
257
def __hash__ (self ) -> int :
258
- if getattr (self , "_hash" , None ) is None :
259
- self ._hash = hash (tuple (getattr (self , key ) for key in self .keys ))
260
- return self ._hash
261
-
262
- def __setattr__ (self , key , value ):
263
- object .__setattr__ (self , "_hash" , None )
258
+ """Get a cached hash value for the node."""
259
+ # Caching the hash values improves the performance of AST validators
260
+ hashed = getattr (self , "_hash" , None )
261
+ if hashed is None :
262
+ hashed = hash (tuple (getattr (self , key ) for key in self .keys ))
263
+ self ._hash = hashed
264
+ return hashed
265
+
266
+ def __setattr__ (self , key : str , value : Any ) -> None :
267
+ # reset cashed hash value if attributes are changed
268
+ if hasattr (self , "_hash" ) and key in self .keys :
269
+ del self ._hash
264
270
super ().__setattr__ (key , value )
265
271
266
272
def __copy__ (self ) -> "Node" :
Original file line number Diff line number Diff line change @@ -180,14 +180,27 @@ def can_hash():
180
180
assert node3 != node
181
181
assert hash (node3 ) != hash (node )
182
182
183
+ def caches_are_hashed ():
184
+ node = SampleTestNode (alpha = 1 )
185
+ assert not hasattr (node , "_hash" )
186
+ hash1 = hash (node )
187
+ assert hasattr (node , "_hash" )
188
+ assert hash1 == getattr (node , "_hash" )
189
+ node .alpha = 2
190
+ assert not hasattr (node , "_hash" )
191
+ hash2 = hash (node )
192
+ assert hash2 != hash1
193
+ assert hasattr (node , "_hash" )
194
+ assert hash2 == getattr (node , "_hash" )
195
+
183
196
def can_create_weak_reference ():
184
197
node = SampleTestNode (alpha = 1 , beta = 2 )
185
198
ref = weakref .ref (node )
186
199
assert ref () is node
187
200
188
201
def can_create_custom_attribute ():
189
202
node = SampleTestNode (alpha = 1 , beta = 2 )
190
- node .gamma = 3 # type: ignore
203
+ node .gamma = 3
191
204
assert node .gamma == 3 # type: ignore
192
205
193
206
def can_create_shallow_copy ():
You can’t perform that action at this time.
0 commit comments