Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix serialization of NameEmail if name includes an email address #8860

Merged

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

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
Member

@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
Member

@NeevCohen,

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

Copy link
Member

@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!

@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
Member

@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
53 checks passed
@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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants