diff --git a/changes/4455-czaki.md b/changes/4455-czaki.md new file mode 100644 index 0000000000..a443079828 --- /dev/null +++ b/changes/4455-czaki.md @@ -0,0 +1 @@ +bugfix: Add `__hash__` method to `pydancic.color.Color` class. \ No newline at end of file diff --git a/pydantic/color.py b/pydantic/color.py index 6ea2c53f80..6fdc9fb144 100644 --- a/pydantic/color.py +++ b/pydantic/color.py @@ -201,6 +201,9 @@ def __repr_args__(self) -> 'ReprArgs': def __eq__(self, other: Any) -> bool: return isinstance(other, Color) and self.as_rgb_tuple() == other.as_rgb_tuple() + def __hash__(self) -> int: + return hash(self.as_rgb_tuple()) + def parse_tuple(value: Tuple[Any, ...]) -> RGBA: """ diff --git a/tests/test_color.py b/tests/test_color.py index db4ce97254..2f3af623be 100644 --- a/tests/test_color.py +++ b/tests/test_color.py @@ -193,3 +193,9 @@ def test_eq(): assert Color('red') == Color((255, 0, 0)) assert Color('red') != Color((0, 0, 255)) + + +def test_color_hashable(): + assert hash(Color('red')) != hash(Color('blue')) + assert hash(Color('red')) == hash(Color((255, 0, 0))) + assert hash(Color('red')) != hash(Color((255, 0, 0, 0.5)))