Skip to content
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pytools/test/test_persistent_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,16 @@ def update_persistent_hash(self, key_hash, key_builder):
def test_class_hashing() -> None:
keyb = KeyBuilder()

class WithoutUpdateMethod:
pass

assert keyb(WithoutUpdateMethod) == keyb(WithoutUpdateMethod)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arguably, this should not work. This is a different class every time the function gets called. It might be useful to see if the class type has the same id() as the one available at the advertised name, before using that name for hashing.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I'm not sure I understand. Do you mean, every time test_class_hashing is called, the class is different? But this isn't really tested in this line, perhaps more like in the next line assert keyb(WithoutUpdateMethod) == "da060d601d180d4c"?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean, every time test_class_hashing is called, the class is different?

Yes.

My point is that a function-local class does not have a globally valid name and thus should not be hashable. This can be tested by attempting to import it by its __qualname__ (won't work for local classes) and seeing if it comes back with the same id (just to be safe).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL, thanks! What do you think of e35c58a? I'm worried that this may cause extra cache misses since some tags that we use might be local classes?

assert keyb(WithoutUpdateMethod) == "da060d601d180d4c"

with pytest.raises(TypeError):
# does not have update_persistent_hash() = > will raise
keyb(WithoutUpdateMethod())

class WithUpdateMethod:
def update_persistent_hash(self, key_hash, key_builder):
# Only called for instances of this class, not for the class itself
Expand Down
Loading