Opened 7 months ago

Closed 7 months ago

#26145 closed defect (fixed)

py3: hashing of cartesian product elements

Reported by: dkrenn 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) Commit: bc571fdd3a6f7742b6e6e6e151bfece1e46e624f
Dependencies: Stopgaps:

Description (last modified by 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 7 months ago by chapoton

  • Description modified (diff)

comment:2 follow-up: Changed 7 months ago by 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 7 months ago by chapoton

  • Description modified (diff)

comment:4 Changed 7 months ago by chapoton

  • Cc jdemeyer 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 __richcmp__ methods ?

Last edited 7 months ago by chapoton (previous) (diff)

comment:5 Changed 7 months ago by chapoton

  • Authors set to Frédéric Chapoton
  • Branch set to u/chapoton/hash_cartesian
  • Cc jdemeyer removed
  • Commit set to dc1d70472f1637aacc6e080da16c02c9096862c8
  • Status changed from new to needs_review

New commits:

dc1d704py3: hash for Cartesian product elements

comment:6 Changed 7 months ago by chapoton

  • Status changed from needs_review to needs_work

comment:7 Changed 7 months ago by git

  • Commit changed from dc1d70472f1637aacc6e080da16c02c9096862c8 to bc571fdd3a6f7742b6e6e6e151bfece1e46e624f

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 7 months ago by chapoton

  • Status changed from needs_work to needs_review

comment:9 Changed 7 months ago by tscrim

  • Reviewers set to Travis Scrimshaw
  • Status changed from needs_review to positive_review

LGTM.

comment:10 in reply to: ↑ 2 Changed 7 months ago by dkrenn

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 7 months ago by chapoton

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

comment:12 Changed 7 months ago by vbraun

  • Branch changed from u/chapoton/hash_cartesian to bc571fdd3a6f7742b6e6e6e151bfece1e46e624f
  • Resolution set to fixed
  • Status changed from positive_review to closed
Note: See TracTickets for help on using tickets.