Opened 4 years ago

Closed 4 years ago

#26145 closed defect (fixed)

py3: hashing of cartesian product elements

Reported by: Daniel Krenn Owned by:
Priority: major Milestone: sage-8.4
Component: python3 Keywords:
Cc: Merged in:
Authors: Frédéric Chapoton Reviewers: Travis Scrimshaw
Report Upstream: N/A Work issues:
Branch: bc571fd (Commits, GitHub, GitLab) Commit: bc571fdd3a6f7742b6e6e6e151bfece1e46e624f
Dependencies: Stopgaps:

Status badges

Description (last modified by Frédéric Chapoton)

part of #24551

sage: C = cartesian_product([ZZ,ZZ])
sage: b = C((1,1))
sage: hash(b)
---------------------------
TypeError      Traceback (most recent call last)
<ipython-input-1-b09cf2f9caf6> in <module>()
      1 C = cartesian_product([ZZ,ZZ])
      2 b = C((Integer(1),Integer(1)))
----> 3 hash(b)

TypeError: unhashable type: 'CartesianProduct_with_category.element_class'

Change History (12)

comment:1 Changed 4 years ago by Frédéric Chapoton

Description: modified (diff)

comment:2 Changed 4 years ago by Frédéric Chapoton

indeed, hash does not exist, even if it appears in the tab completion:

sage: A = cartesian_product([ZZ, ZZ])
sage: elt = A((1,1))
sage: elt.__eq__
<method-wrapper '__eq__' of CartesianProduct_with_category.element_class object at 0x7f289e0953b8>
sage: elt.__hash__

comment:3 Changed 4 years ago by Frédéric Chapoton

Description: modified (diff)

comment:4 Changed 4 years ago by Frédéric Chapoton

Cc: Jeroen Demeyer added

Looks similar to the problem here:

sage:  w = Word([0,1,1,0])
sage: type(w)
<class 'sage.combinat.words.word.FiniteWord_list'>
sage: w.__eq__
<method-wrapper '__eq__' of FiniteWord_list object at 0x7f840419ec78>
sage: w.__hash__

Maybe there is something to change in the way cython handles the __richmp__ methods ?

Version 0, edited 4 years ago by Frédéric Chapoton (next)

comment:5 Changed 4 years ago by Frédéric Chapoton

Authors: Frédéric Chapoton
Branch: u/chapoton/hash_cartesian
Cc: Jeroen Demeyer removed
Commit: dc1d70472f1637aacc6e080da16c02c9096862c8
Status: newneeds_review

New commits:

dc1d704py3: hash for Cartesian product elements

comment:6 Changed 4 years ago by Frédéric Chapoton

Status: needs_reviewneeds_work

comment:7 Changed 4 years ago by git

Commit: dc1d70472f1637aacc6e080da16c02c9096862c8bc571fdd3a6f7742b6e6e6e151bfece1e46e624f

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

bc571fdpy3: hash for Cartesian product elements

comment:8 Changed 4 years ago by Frédéric Chapoton

Status: needs_workneeds_review

comment:9 Changed 4 years ago by Travis Scrimshaw

Reviewers: Travis Scrimshaw
Status: needs_reviewpositive_review

LGTM.

comment:10 in reply to:  2 Changed 4 years ago by Daniel Krenn

Replying to chapoton:

indeed, hash does not exist, even if it appears in the tab completion: [...]

Fix LGTM, but can someone explain what is going on: The class ElementWrapper has a __hash__ defined; the class ElementWrapperCheckWrappedClass is derived from ElementWrapper, so there should be the same __hash__ used. Why does this not work (with Py3)?

comment:11 Changed 4 years ago by Frédéric Chapoton

Probably because it redefines comparison __richcmp__, which itself is used by Cython to redefine __eq__.. just a guess..

comment:12 Changed 4 years ago by Volker Braun

Branch: u/chapoton/hash_cartesianbc571fdd3a6f7742b6e6e6e151bfece1e46e624f
Resolution: fixed
Status: positive_reviewclosed
Note: See TracTickets for help on using tickets.