Uploaded image for project: 'Thrift'
  1. Thrift
  2. THRIFT-162

Thrift structures are unhashable, preventing them from being used as set elements

    XMLWordPrintableJSON

Details

    • Patch Available

    Description

      Let Foo be a Thrift structure:

      struct Foo {
      1: i32 bar
      }

      If you want to use it properly as a set element or a as a dictionary key, the autoegenerated Python code will complain about not being hashable:

      >>> f1 = Foo()
      >>> f1.bar = 1
      >>> f2 = Foo()
      >>> f2.bar = 1
      >>> f1 == f2
      True
      >>> set([f1]) & set([f2])
      set([])
      >>> d = {}
      >>> d[f1] = 2
      Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      TypeError: unhashable instance

      Since Thrift structures already implement _eq_ and _ne, they should implement __hash_ as well. The attached patch tries to mimic the behaviour of the Java compiler, including a HashCodeBuilder class written in Python.

      Attachments

        1. thrift-162_v2_annotiations.patch
          5 kB
          Esteve Fernandez
        2. thrift-162_annotiations.patch
          2 kB
          Esteve Fernandez
        3. thrift_py_hash.patch
          4 kB
          Esteve Fernandez
        4. reading-containers.diff
          3 kB
          David Reiss
        5. immut-v4.diff
          7 kB
          David Reiss
        6. immut-v3.diff
          9 kB
          David Reiss
        7. immut-v2.diff
          7 kB
          David Reiss
        8. immut-no-slots-v1.diff
          2 kB
          David Reiss

        Issue Links

          Activity

            People

              nsuke Nobuaki Sukegawa
              esteve Esteve Fernandez
              Votes:
              1 Vote for this issue
              Watchers:
              8 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: