Skip to content

Commit

Permalink
Fix NameEmail equality comparison (#1527)
Browse files Browse the repository at this point in the history
* Adding appropriate __eq__ method for NameEmail

- Introducing new test assertions for NameEmail

Signed-off-by: Stephen Bunn <stephen@bunn.io>

* Adding change doc for NameEmail.__eq__

Signed-off-by: Stephen Bunn <stephen@bunn.io>

* Update pydantic/networks.py

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>

* Update tests/test_networks.py

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>

* Fixing indentation issue left over from suggestion

Signed-off-by: Stephen Bunn <stephen@bunn.io>

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
  • Loading branch information
stephen-bunn and samuelcolvin committed May 19, 2020
1 parent 28c2ac7 commit 1eeb225
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 0 deletions.
1 change: 1 addition & 0 deletions changes/1514-stephen-bunn.md
@@ -0,0 +1 @@
Add `NameEmail.__eq__` so duplicate `NameEmail` instances are evaluated as equal.
3 changes: 3 additions & 0 deletions pydantic/networks.py
Expand Up @@ -329,6 +329,9 @@ def __init__(self, name: str, email: str):
self.name = name
self.email = email

def __eq__(self, other: Any) -> bool:
return isinstance(other, NameEmail) and (self.name, self.email) == (other.name, other.email)

@classmethod
def __modify_schema__(cls, field_schema: Dict[str, Any]) -> None:
field_schema.update(type='string', format='name-email')
Expand Down
2 changes: 2 additions & 0 deletions tests/test_networks.py
Expand Up @@ -453,3 +453,5 @@ class Model(BaseModel):

assert str(Model(v=NameEmail('foo bar', 'foobaR@example.com')).v) == 'foo bar <foobaR@example.com>'
assert str(Model(v='foo bar <foobaR@example.com>').v) == 'foo bar <foobaR@example.com>'
assert NameEmail('foo bar', 'foobaR@example.com') == NameEmail('foo bar', 'foobaR@example.com')
assert NameEmail('foo bar', 'foobaR@example.com') != NameEmail('foo bar', 'different@example.com')

0 comments on commit 1eeb225

Please sign in to comment.