[ 
https://issues.apache.org/jira/browse/AVRO-1737?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Zoltan Fedor updated AVRO-1737:
-------------------------------
    Description: 
Following the standard example from 
http://avro.apache.org/docs/current/gettingstartedpython.html but adding a 
dict.get() lookup using an avro.schema.RecordSchema object fails due to 
"unhashable type: 'RecordSchema'"

This likely happens because the RecordSchema class overwrites the __eq__ method 
but does NOT define a __hash__ method.

Check the Python 3 manual on object.__hash__ 
(https://docs.python.org/3.1/reference/datamodel.html?highlight=hash#object.%5F%5Fhash%5F%5F):
"If a class does not define an __eq__() method it should not define a 
__hash__() operation either; if it defines __eq__() but not __hash__(), its 
instances will not be usable as items in hashable collections."


TO REPRODUCE the issue run the following script in Python 3 (tested in Python 
3.4.3):
import avro.schema
from avro.datafile import DataFileReader, DataFileWriter
from avro.io import DatumReader, DatumWriter

schema = avro.schema.Parse(open("users.avsc").read())

writer = DataFileWriter(open("users.avro", "wb"), DatumWriter(), schema)
writer.append({"name": "Alyssa", "favorite_number": 256})
writer.append({"name": "Ben", "favorite_number": 7, "favorite_color": "red"})
writer.close()

reader = DataFileReader(open("users.avro", "rb"), DatumReader())
for user in reader:
    print(user)
reader.close()

# added to generate the error
test = {}
test.get(schema, -1)


  was:
Following the standard example from 
http://avro.apache.org/docs/current/gettingstartedpython.html but adding a 
dict.get() lookup using an avro.schema.RecordSchema object fails due to 
"unhashable type: 'RecordSchema'"

This likely happens because the RecordSchema class overwrite the __eq__ method 
but does NOT define a __hash__ method.

To reproduce run the following script in Python 3 (tested in Python 3.4.3):
import avro.schema
from avro.datafile import DataFileReader, DataFileWriter
from avro.io import DatumReader, DatumWriter

schema = avro.schema.Parse(open("users.avsc").read())

writer = DataFileWriter(open("users.avro", "wb"), DatumWriter(), schema)
writer.append({"name": "Alyssa", "favorite_number": 256})
writer.append({"name": "Ben", "favorite_number": 7, "favorite_color": "red"})
writer.close()

reader = DataFileReader(open("users.avro", "rb"), DatumReader())
for user in reader:
    print(user)
reader.close()

# added to generate the error
test = {}
test.get(schema, -1)



> Python 3.4 - unhashable type: 'RecordSchema'
> --------------------------------------------
>
>                 Key: AVRO-1737
>                 URL: https://issues.apache.org/jira/browse/AVRO-1737
>             Project: Avro
>          Issue Type: Bug
>          Components: python
>    Affects Versions: 1.7.7
>         Environment: Python 3.4, avro 1.7.7
>            Reporter: Zoltan Fedor
>
> Following the standard example from 
> http://avro.apache.org/docs/current/gettingstartedpython.html but adding a 
> dict.get() lookup using an avro.schema.RecordSchema object fails due to 
> "unhashable type: 'RecordSchema'"
> This likely happens because the RecordSchema class overwrites the __eq__ 
> method but does NOT define a __hash__ method.
> Check the Python 3 manual on object.__hash__ 
> (https://docs.python.org/3.1/reference/datamodel.html?highlight=hash#object.%5F%5Fhash%5F%5F):
> "If a class does not define an __eq__() method it should not define a 
> __hash__() operation either; if it defines __eq__() but not __hash__(), its 
> instances will not be usable as items in hashable collections."
> TO REPRODUCE the issue run the following script in Python 3 (tested in Python 
> 3.4.3):
> import avro.schema
> from avro.datafile import DataFileReader, DataFileWriter
> from avro.io import DatumReader, DatumWriter
> schema = avro.schema.Parse(open("users.avsc").read())
> writer = DataFileWriter(open("users.avro", "wb"), DatumWriter(), schema)
> writer.append({"name": "Alyssa", "favorite_number": 256})
> writer.append({"name": "Ben", "favorite_number": 7, "favorite_color": "red"})
> writer.close()
> reader = DataFileReader(open("users.avro", "rb"), DatumReader())
> for user in reader:
>     print(user)
> reader.close()
> # added to generate the error
> test = {}
> test.get(schema, -1)



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to