Skip to content

Commit

Permalink
Use object.__hash__ for Node.__hash
Browse files Browse the repository at this point in the history
This fixes a regression in commit 6029341 that
changed the `__hash__` implementation of Node from the default pointer
hash, to a hash based on the node fields.

Since these fields contains list objects, they are not hashable, making
every call to `Node.__hash__` fail.

This breaks some third-party usage such as in `django-compressor`
(See: django-compressor/django-compressor#1060)

This changed reverts the hash method back to using `object.__hash__` as
the hash implementation.
  • Loading branch information
klette committed Oct 27, 2021
1 parent f20a9c9 commit 6fa2e6c
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES.rst
Expand Up @@ -5,6 +5,7 @@ Version 3.1.0

Unreleased

- Change Node.__hash__ behavior to use object.__hash__. :issue:`1521`

Version 3.0.2
-------------
Expand Down
2 changes: 1 addition & 1 deletion src/jinja2/nodes.py
Expand Up @@ -242,7 +242,7 @@ def __eq__(self, other: t.Any) -> bool:
return tuple(self.iter_fields()) == tuple(other.iter_fields())

def __hash__(self) -> int:
return hash(tuple(self.iter_fields()))
return object.__hash__(self)

def __repr__(self) -> str:
args_str = ", ".join(f"{a}={getattr(self, a, None)!r}" for a in self.fields)
Expand Down
4 changes: 4 additions & 0 deletions tests/test_node_hash.py
@@ -0,0 +1,4 @@
class TestHashing:
def test_template_hash(self, env):
template = env.parse("hash test")
assert hash(template)

0 comments on commit 6fa2e6c

Please sign in to comment.