Skip to content

Commit 128637f

Browse files
committed
Add support for bracketed IPv6 addresses
Accept IPv6 addresses enclosed in brackets and add test coverage for loopback, compressed, expanded, and IPv4-mapped forms.
1 parent 8f437fe commit 128637f

2 files changed

Lines changed: 8 additions & 3 deletions

File tree

‎src/validators/ip_address.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def ipv6(value: str, /, *, cidr: bool = True, strict: bool = False, host_bit: bo
118118
119119
Args:
120120
value:
121-
IP address string to validate.
121+
IP address string to validate. It may be enclosed in brackets.
122122
cidr:
123123
IP address string may contain CIDR annotation.
124124
strict:
@@ -134,6 +134,8 @@ def ipv6(value: str, /, *, cidr: bool = True, strict: bool = False, host_bit: bo
134134
"""
135135
if not value:
136136
return False
137+
if value.startswith("[") and value.endswith("]"):
138+
value = value[1:-1]
137139
try:
138140
if cidr:
139141
if strict and value.count("/") != 1:

‎tests/test_ipv6_brackets.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@
1212
@pytest.mark.parametrize(
1313
("ip",),
1414
[
15-
("[2001:db8::1]", ),
15+
("[::1]",),
16+
("[2001:db8::1]",),
17+
("[2001:0db8:0000:0000:0000:0000:0000:0001]",),
18+
("[::ffff:192.0.2.128]",),
1619
],
1720
)
1821
def test_ipv6_accepts_address_in_brackets(ip: str):
19-
"""Accept iv6 address in brackets."""
22+
"""Accept an IPv6 address enclosed in brackets."""
2023
result = validators.ipv6(ip)
2124
assert result is True, repr(result)

0 commit comments

Comments
 (0)