KEMBAR78
Fix serialization of NameEmail if name includes an email address by NeevCohen · Pull Request #8860 · pydantic/pydantic · GitHub
Skip to content

Conversation

@NeevCohen
Copy link
Contributor

@NeevCohen NeevCohen commented Feb 20, 2024

Change Summary

Fixing a serialization issue where an email would include an email address in the name i.e. "name@gmail.com" <name@gmail.com> and NameEmail would validate it correctly but remove the quotes when serializing back into a string

import json
from pydantic import BaseModel, NameEmail

class Email(BaseModel):
  From: NameEmail

email = Email(From='"name@mailbox.com" <name@mailbox.com>')
print(email)
# output
# From=NameEmail(name='name@mailbox.com', email='name@mailbox.com')

obj = json.loads(email.model_dump_json())
print(obj)
# output
# {'From': 'name@mailbox.com <name@mailbox.com>'}

email = Email(From=obj['From'])
# Traceback (most recent call last):
#  ...
#    email = Email(From=obj['From'])
#            ^^^^^^^^^^^^^^^^^^^^^^^
#  File "/<some_path>/lib/python3.11/site-packages/pydantic/main.py", line 171, in __init__
#    self.__pydantic_validator__.validate_python(data, self_instance=self) pydantic_core._pydantic_core.ValidationError: 1 validation error for Email From
#  value is not a valid email address: The email address is not valid. It must have exactly one @-sign. [type=value_error, input_value='name@mailbox.com <name@mailbox.com>', input_type=str]

Related issue number

Fixing #8811

Checklist

  • The pull request title is a good summary of the changes - it will be used in the changelog
  • Unit tests for the changes exist
  • Tests pass on CI
  • Documentation reflects the changes where applicable
  • My PR is ready to review, please add a comment including the phrase "please review" to assign reviewers

Selected Reviewer: @adriangb

@codspeed-hq
Copy link

codspeed-hq bot commented Feb 20, 2024

CodSpeed Performance Report

Merging #8860 will not alter performance

Comparing NeevCohen:fix/email-address-in-nameemail-name (790f7f0) with main (812516d)

Summary

✅ 10 untouched benchmarks

@NeevCohen
Copy link
Contributor Author

Please review

@sydney-runkle
Copy link
Contributor

@NeevCohen,

Thanks for the PR. I will confirm with the team tomorrow if this is the approach we want to take for the fix, and I'll get back to you early tomorrow :).

@sydney-runkle
Copy link
Contributor

@NeevCohen,

Let's go with a more simple approach of simply checking for an @ symbol in the name :).

Copy link
Contributor

@sydney-runkle sydney-runkle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned in my above comment, let's go with a simple check for @ instead of the regex matching. Thanks!

@pydantic-hooky pydantic-hooky bot added awaiting author revision awaiting changes from the PR author and removed ready for review labels Feb 22, 2024
@pydantic-hooky pydantic-hooky bot assigned NeevCohen and unassigned adriangb Feb 22, 2024
@NeevCohen
Copy link
Contributor Author

@sydney-runkle sure, once I'll get home I'll change it

@NeevCohen
Copy link
Contributor Author

@sydney-runkle Changed it as per your comment

Copy link
Contributor

@sydney-runkle sydney-runkle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thanks!!

@sydney-runkle sydney-runkle merged commit deeb110 into pydantic:main Feb 23, 2024
@NeevCohen NeevCohen deleted the fix/email-address-in-nameemail-name branch March 12, 2024 18:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting author revision awaiting changes from the PR author relnotes-fix Used for bugfixes.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants