Skip to content
Draft
Show file tree
Hide file tree
Changes from 4 commits
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
18 changes: 16 additions & 2 deletions pytools/persistent_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,22 @@ def __call__(self, key: Any) -> str:
# understandably don't like it.

def update_for_type(self, key_hash: Hash, key: type) -> None:
key_hash.update(
f"{key.__module__}.{key.__qualname__}.{key.__name__}".encode())
try:
module = sys.modules[key.__module__]
resolved = module
for attr in key.__qualname__.split("."):
resolved = getattr(resolved, attr) # pyright: ignore[reportAny]
if resolved is key:
# Globally accessible: hash based on name
self.rec(key_hash,
f"{key.__module__}.{key.__qualname__}".encode()
)
else:
# Name collision or local class; fall back
self.rec(key_hash, f"{key.__module__}.{key.__qualname__}_{id(key)}")

@inducer inducer Jun 18, 2025

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.

I think local classes should be unhashable, i.e. this should be an error.

except (KeyError, AttributeError):
# Can't resolve the class name, must be local
self.rec(key_hash, f"{key.__module__}.{key.__qualname__}_{id(key)}")

update_for_ABCMeta = update_for_type

Expand Down
44 changes: 43 additions & 1 deletion pytools/test/test_persistent_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,9 +525,52 @@ def update_persistent_hash(self, key_hash, key_builder):
assert keyb(MyABC3) != keyb(MyABC) != keyb(MyABC3())


class WithoutUpdateMethodGlobal:
pass


class CollidingNameClass:
pass


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(WithoutUpdateMethodGlobal) == keyb(WithoutUpdateMethodGlobal)

# This doesn't work with the function-local class "WithoutUpdateMethod", because
# local classes are instantiated at each function call, and thus their hash
# includes the id() of the class:
# assert keyb(WithoutUpdateMethod) == "N/A"
assert keyb(WithoutUpdateMethodGlobal) == "49c4673089d30507"

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

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

# {{{ test for name collisions between top-level and function-local classes

def make_colliding_name_class():
class CollidingNameClass:
pass

return CollidingNameClass

top_level_cls = CollidingNameClass
shadowed_cls = make_colliding_name_class()

assert keyb(top_level_cls) != keyb(shadowed_cls)

# }}}

class WithUpdateMethod:
def update_persistent_hash(self, key_hash, key_builder):
# Only called for instances of this class, not for the class itself
Expand All @@ -548,7 +591,6 @@ class TagClass2(Tag):
assert keyb(TagClass()) != keyb(TagClass2())

assert keyb(TagClass()) == "7b3e4e66503438f6"
assert keyb(TagClass2) == "690b86bbf51aad83"

@tag_dataclass
class TagClass3(Tag):
Expand Down
Loading