Skip to content

Hash function missing from subclass #483

@vitalbmcdonald

Description

@vitalbmcdonald

When creating a subclass of an attr object, I would expect setting cmp=False to cause the subclass to be hashable. Right now it inherits the hash settings from its parent and ignores the cmp settings for hashing.

import attr

@attr.s
class Foo:
    pass

@attr.s(cmp=False)
class Bar(Foo):
    pass

assert Foo.__hash__ is None, "Foo incorrectly has a hash method"
## passes

assert Bar.__hash__ is object.__hash__, "Bar is missing the identity hash method"
## fails

The issue I believe is this elif block assumes that the class is inheriting from object and thereby getting its __hash__ method. Adding a line to explicitly add the __hash__ method would fix the problem.

The workaround I am using is just adding the __hash__ method after the class is defined, so

@attr.s(cmp=False)
class Bar(Foo):
    pass

Bar.__hash__ = object.__hash__

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions