smarie/python-autoclass

Exclude argument does not work properly with autohash

smarie opened this issue · 0 comments

Suppose I want to exclude a couple fields from the hash because for example they are not hashable.

@autoclass(autohash=False)
@autohash(exclude='bar')
class Foo:
    def __init__(self, foo: str, bar: Dict[str, str]):
        pass

The above does not work:

> a = Foo('hello', dict())
> hash(a)  # TypeError: unhashable type: 'dict'

The only workaround as of today is to use the private name associated with the attribute:

@autoclass(autohash=False)
@autohash(exclude='_bar')  # <- here use the private name
class Foo:
    def __init__(self, foo: str, bar: Dict[str, str]):
        pass

But it is not satisfying.

Note that there is probably a similar behaviour in @autodict with non-None exclude/include.