@@ -525,19 +525,52 @@ def update_persistent_hash(self, key_hash, key_builder):
525525 assert keyb (MyABC3 ) != keyb (MyABC ) != keyb (MyABC3 ())
526526
527527
528+ class WithoutUpdateMethodGlobal :
529+ pass
530+
531+
532+ class CollidingNameClass :
533+ pass
534+
535+
528536def test_class_hashing () -> None :
529537 keyb = KeyBuilder ()
530538
531539 class WithoutUpdateMethod :
532540 pass
533541
534542 assert keyb (WithoutUpdateMethod ) == keyb (WithoutUpdateMethod )
535- assert keyb (WithoutUpdateMethod ) == "da060d601d180d4c"
543+ assert keyb (WithoutUpdateMethodGlobal ) == keyb (WithoutUpdateMethodGlobal )
544+
545+ # This doesn't work with the function-local class "WithoutUpdateMethod", because
546+ # local classes are instantiated at each function call, and thus their hash
547+ # includes the id() of the class:
548+ # assert keyb(WithoutUpdateMethod) == "N/A"
549+ assert keyb (WithoutUpdateMethodGlobal ) == "49c4673089d30507"
536550
537551 with pytest .raises (TypeError ):
538552 # does not have update_persistent_hash() = > will raise
539553 keyb (WithoutUpdateMethod ())
540554
555+ with pytest .raises (TypeError ):
556+ # does not have update_persistent_hash() = > will raise
557+ keyb (WithoutUpdateMethodGlobal ())
558+
559+ # {{{ test for name collisions between top-level and function-local classes
560+
561+ def make_colliding_name_class ():
562+ class CollidingNameClass :
563+ pass
564+
565+ return CollidingNameClass
566+
567+ top_level_cls = CollidingNameClass
568+ shadowed_cls = make_colliding_name_class ()
569+
570+ assert keyb (top_level_cls ) != keyb (shadowed_cls )
571+
572+ # }}}
573+
541574 class WithUpdateMethod :
542575 def update_persistent_hash (self , key_hash , key_builder ):
543576 # Only called for instances of this class, not for the class itself
@@ -558,7 +591,6 @@ class TagClass2(Tag):
558591 assert keyb (TagClass ()) != keyb (TagClass2 ())
559592
560593 assert keyb (TagClass ()) == "7b3e4e66503438f6"
561- assert keyb (TagClass2 ) == "690b86bbf51aad83"
562594
563595 @tag_dataclass
564596 class TagClass3 (Tag ):
0 commit comments