From b73d9af1cf48634c1a12c5991caf90b43213cec5 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 21 Apr 2025 11:20:14 -0400 Subject: [PATCH] Correctly handle None, add tests for other invalid types --- email_validator/validate_email.py | 2 +- tests/test_main.py | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/email_validator/validate_email.py b/email_validator/validate_email.py index 0e8f6e0..6545c7e 100644 --- a/email_validator/validate_email.py +++ b/email_validator/validate_email.py @@ -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 diff --git a/tests/test_main.py b/tests/test_main.py index ab8eecd..a450f89 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -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)