avro-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Zoltan Fedor (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (AVRO-1737) Python 3.4 - unhashable type: 'RecordSchema'
Date Tue, 15 Sep 2015 21:01:46 GMT

    [ https://issues.apache.org/jira/browse/AVRO-1737?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14746180#comment-14746180
] 

Zoltan Fedor commented on AVRO-1737:
------------------------------------

For now I have monkey patched this by doing something like this:

{code}
from avro import schema

def hash_func(self):
    return hash(str(self))

schema.RecordSchema.__hash__ = hash_func
{code}

> 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):
> {code}
> 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)
> {code}



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

Mime
View raw message