Skip to content

Correctly handle None, add tests for other invalid types #155

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion email_validator/validate_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def validate_email(
if not isinstance(email, str):
try:
email = email.decode("ascii")
except ValueError as e:
except (AttributeError, ValueError) as e:
raise EmailSyntaxError("The email address is not valid ASCII.") from e

# Split the address into the display name (or None), the local part
Expand Down
14 changes: 14 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,17 @@ def test_deprecation() -> None:
valid_email = validate_email(input_email, check_deliverability=False)
with pytest.deprecated_call():
assert valid_email.email is not None


@pytest.mark.parametrize('invalid_email', [
None,
12345,
[],
{},
lambda x: x,
# I believe this is a valid email address, but it's not valid ASCII
b'\xd0\xba\xd0\xb2\xd1\x96\xd1\x82\xd0\xbe\xd1\x87\xd0\xba\xd0\xb0@\xd0\xbf\xd0\xbe\xd1\x88\xd1\x82\xd0\xb0.test'
])
def test_invalid_type(invalid_email) -> None:
with pytest.raises(EmailSyntaxError, match="The email address is not valid ASCII"):
validate_email(invalid_email, check_deliverability=False)