-
Notifications
You must be signed in to change notification settings - Fork 27
add a small test for hashability of bare classes #296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
26328fb to
4d5a7eb
Compare
pytools/test/test_persistent_dict.py
Outdated
| class WithoutUpdateMethod: | ||
| pass | ||
|
|
||
| assert keyb(WithoutUpdateMethod) == keyb(WithoutUpdateMethod) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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"?
There was a problem hiding this comment.
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_hashingis 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).
There was a problem hiding this comment.
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?
pytools/persistent_dict.py
Outdated
| ) | ||
| else: | ||
| # Name collision or local class; fall back | ||
| self.rec(key_hash, f"{key.__module__}.{key.__qualname__}_{id(key)}") |
There was a problem hiding this comment.
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.
ae0a065 to
b30b29e
Compare
No description provided.