mirror of
https://github.com/kennethreitz/pydantic.git
synced 2026-06-05 23:00:18 +00:00
Add Color comparison method (#3646)
* Add Color comparison method * add changelog * switch to public attribute * compare rgb tuple Co-authored-by: Samuel Colvin <s@muelcolvin.com>
This commit is contained in:
@@ -0,0 +1 @@
|
||||
Add comparison method for `Color` class.
|
||||
@@ -198,6 +198,9 @@ class Color(Representation):
|
||||
def __repr_args__(self) -> 'ReprArgs':
|
||||
return [(None, self.as_named(fallback=True))] + [('rgb', self.as_rgb_tuple())] # type: ignore
|
||||
|
||||
def __eq__(self, other: Any) -> bool:
|
||||
return isinstance(other, Color) and self.as_rgb_tuple() == other.as_rgb_tuple()
|
||||
|
||||
|
||||
def parse_tuple(value: Tuple[Any, ...]) -> RGBA:
|
||||
"""
|
||||
|
||||
@@ -184,3 +184,12 @@ def test_str_repr():
|
||||
assert repr(Color('red')) == "Color('red', rgb=(255, 0, 0))"
|
||||
assert str(Color((1, 2, 3))) == '#010203'
|
||||
assert repr(Color((1, 2, 3))) == "Color('#010203', rgb=(1, 2, 3))"
|
||||
|
||||
|
||||
def test_eq():
|
||||
assert Color('red') == Color('red')
|
||||
assert Color('red') != Color('blue')
|
||||
assert Color('red') != 'red'
|
||||
|
||||
assert Color('red') == Color((255, 0, 0))
|
||||
assert Color('red') != Color((0, 0, 255))
|
||||
|
||||
Reference in New Issue
Block a user