graphql-python/graphql-core-legacy

AST nodes implements __hash__ by id(self) while __eq__ is based on value

qqi0O0 opened this issue · 2 comments

All classes in language/ast.py implement __eq__ by comparing values, and implement __hash__ by id. This violates basic properties such as

a==b => hash(a)==hash(b)

This leads to issues like:

a = Document(definitions=[])
b = Document(definitions=[])
s = {a, b}
l = list(s)
l[0] == l[1]    =>    True

AST nodes should probably just not implement __hash__, since nodes are mutable.

Cito commented

Thanks for pointing this out. You're right that equal nodes should have equal hashes.

There are a some places (particularly in validation) where nodes are used as keys of dicts, so before removing the __hash__ methods we need to check if we can change the code in these places so that the nodes do not need to be hashable.

Feel free to work on this and send a PR. Otherwise I will look into this when I find more time.

Cito commented

This has been fixed in graphql-core-next (graphql-core version 3).

I think we should leave it as it is in graphql-core 2 for compatibility reasons.