@@ -66,3 +66,39 @@ def test_rejects_newline_after_local_part(local_part: str):
6666 # 2026-10-11: A newline before the at-sign is not part of a valid local part.
6767 assert email (f"{ local_part } @example.com" ) is True
6868 assert isinstance (email (f"{ local_part } \n @example.com" ), ValidationError )
69+
70+
71+ @pytest .mark .parametrize (
72+ "address" ,
73+ ["2001:db8::1" , "2001:db8:0:0:0:0:0:1" , "::ffff:192.0.2.1" ],
74+ )
75+ def test_accepts_tagged_ipv6_literal (address : str ):
76+ """Accept RFC 5321 address literals when IPv6 addresses are enabled."""
77+ assert email (f"user@[IPv6:{ address } ]" , ipv6_address = True ) is True
78+ assert email (f"user@[ipv6:{ address } ]" , ipv6_address = True ) is True
79+ assert isinstance (email (f"user@[IPv6:{ address } ]" ), ValidationError )
80+ assert isinstance (email (f"user@[IPv6:{ address } ]" , ipv4_address = True ), ValidationError )
81+
82+
83+ @pytest .mark .parametrize (
84+ "address" , ["" , "example.com" , "192.0.2.1" , "2001:db8::xyz" , "fe80::1%eth0" ]
85+ )
86+ def test_rejects_invalid_tagged_ipv6_literal (address : str ):
87+ """An IPv6 tag must identify a valid IPv6 address, never a hostname or IPv4."""
88+ assert isinstance (email (f"user@[IPv6:{ address } ]" , ipv6_address = True ), ValidationError )
89+ assert isinstance (
90+ email (f"user@[IPv6:{ address } ]" , ipv6_address = True , ipv4_address = True ), ValidationError
91+ )
92+
93+
94+ def test_rejects_extra_brackets_around_tagged_ipv6_literal ():
95+ """Do not strip repeated brackets while interpreting an IPv6 tag."""
96+ assert isinstance (email ("user@[[IPv6:2001:db8::1]]" , ipv6_address = True ), ValidationError )
97+
98+
99+ def test_preserves_legacy_email_address_literals ():
100+ """Keep bare IPv6 and IPv4 literal support compatible."""
101+ assert email ("user@[2001:db8::1]" , ipv6_address = True ) is True
102+ assert email ("user@[192.0.2.1]" , ipv4_address = True ) is True
103+ assert email ("user@[IPv6:2001:db8::1]" , ipv6_address = True , ipv4_address = True ) is True
104+ assert isinstance (email ("invalid local@[IPv6:2001:db8::1]" , ipv6_address = True ), ValidationError )
0 commit comments