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
TypeError: unhashable type: 'instancemethod' #77576
Comments
in Python 3.5 it the pprint.PrettyPrinter mechanism got an overhaul, relying on PrettyPrinter._dispatch dict-lookup based on obj.__repr__ type. This breaks any Cythonized 3rd party libraries that used to be pretty-printable in Python3.4. type(object).__repr__ since instancemethod_hash function has been commented out: Line 569 in c30098c
oddly the behavior is different between Linux and Mac. The same object in Linux returns cyfunction, and is hashable, note that this isn't exactly something related directly to the implementation of Cython. the old logic in Python <3.4 pprint was not pretty (pun not intended), but relied solely on type checking, |
The non-hashable case should be handled by a default function, I would think. It should be fairly straightforward to come up with a reproducer that does not involve cython, which I think would be the first step. |
having a tough time trying to reproduce this issue in pure python. any clue as to how to create a __repr__ that's unhashable? class UnhashableRepr(dict):
__repr__ = _testcapi.instancemethod(dict.__repr__) doesn't work because that turns into a <slot wrapper '__repr__' of 'dict' objects> which becomes hashable. i'd love to provide a patch, seems trivial to fix, but wouldn't want to do so without some tests. |
Ah, in the absence of a traceback I think I misunderstood the problem (I failed to actually look at the code :) Given what you say about the slotwrapper, I'm not sure, but I'm guessing that that means cython isn't using the PyInstanceMethod_Type as intended. That doesn't mean it can't be fixed (though if true it makes it less likely that we'll fix it, unfortunately), but you'll have to figure out what the difference is between how it is used by cython and cpython. I've nosied Christian, maybe he'll remember why the hash is commented out. |
i just discovered cython v0.28 no longer creates instancemethod, so this bug should technically no longer show up after upgrading cython. so the question remains - is it a good idea to assume all type(obj).__repr__ is hashable? if so, we can close this bug. |
Functions/methods should be immutable, so yes, I think it is a safe assumption that they should be hashable, and a bug if they are not. I seem to vaguely recall that there is some other part of the cpython machinery that depend on being able to test function/method equality and assumes that they are immutable, which implies they should be hashable. I'll close this; Christian can reopen it if he thinks there is an actual bug lurking here. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: