Skip to content

Commit 07950d8

Browse files
committed
Provide proper deprecation of the "address" argument and field
* Raise deprecation warnings when address is used * Add a compatibility property to avoid breaking existing code (based on suggestion by Bas Stottelaar in PR #27) * Make addresses keyword-only, so that address can be eventually removed and replaced with it without breaking consumers * Raise TypeError instead of an assertion on conflicting address and addresses
1 parent 59dcf51 commit 07950d8

5 files changed

Lines changed: 38 additions & 23 deletions

File tree

examples/browser.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ def on_service_state_change(
1919
if state_change is ServiceStateChange.Added:
2020
info = zeroconf.get_service_info(service_type, name)
2121
if info:
22-
addresses = [
23-
"%s:%d" % (socket.inet_ntoa(addr), cast(int, info.port))
24-
for addr in info.addresses
25-
]
22+
addresses = ["%s:%d" % (socket.inet_ntoa(addr), cast(int, info.port)) for addr in info.addresses]
2623
print(" Addresses: %s" % ", ".join(addresses))
2724
print(" Weight: %d, priority: %d" % (info.weight, info.priority))
2825
print(" Server: %s" % (info.server,))

examples/registration.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,10 @@
2020
info = ServiceInfo(
2121
"_http._tcp.local.",
2222
"Paul's Test Web Site._http._tcp.local.",
23-
socket.inet_aton("127.0.0.1"),
24-
80,
25-
0,
26-
0,
27-
desc,
28-
"ash-2.local.",
23+
addresses=[socket.inet_aton("127.0.0.1")],
24+
port=80,
25+
properties=desc,
26+
server="ash-2.local.",
2927
)
3028

3129
zeroconf = Zeroconf()

examples/self_test.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,9 @@
2121
info = ServiceInfo(
2222
"_http._tcp.local.",
2323
"My Service Name._http._tcp.local.",
24-
socket.inet_aton("127.0.0.1"),
25-
1234,
26-
0,
27-
0,
28-
desc,
24+
addresses=[socket.inet_aton("127.0.0.1")],
25+
port=1234,
26+
properties=desc,
2927
)
3028
print(" Registering service...")
3129
r.register_service(info)

test_zeroconf.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -969,9 +969,15 @@ def test_multiple_addresses():
969969
# Old way
970970
info = ServiceInfo(type_, registration_name, address, 80, 0, 0, desc, "ash-2.local.")
971971

972-
assert not hasattr(info, "address")
972+
assert info.address == address
973973
assert info.addresses == [address]
974974

975+
# Updating works
976+
address2 = socket.inet_aton("10.0.1.3")
977+
info.address = address2
978+
979+
assert info.addresses == [address2]
980+
975981
# Compatibility way
976982
info = ServiceInfo(type_, registration_name, [address, address], 80, 0, 0, desc, "ash-2.local.")
977983

zeroconf.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import sys
3131
import threading
3232
import time
33+
import warnings
3334
from functools import reduce
3435
from typing import AnyStr, Dict, List, Optional, Union, cast
3536
from typing import Callable, Set, Tuple # noqa # used in type hints
@@ -1441,13 +1442,14 @@ def __init__(
14411442
server: Optional[str] = None,
14421443
host_ttl: int = _DNS_HOST_TTL,
14431444
other_ttl: int = _DNS_OTHER_TTL,
1444-
addresses: Optional[List[bytes]] = None,
1445+
*,
1446+
addresses: Optional[List[bytes]] = None
14451447
) -> None:
14461448
"""Create a service description.
14471449
14481450
type_: fully qualified service type name
14491451
name: fully qualified service name
1450-
address: IP address as unsigned short, network byte order (legacy)
1452+
address: IP address as unsigned short, network byte order (deprecated, use addresses)
14511453
port: port that the service runs on
14521454
weight: weight of the service
14531455
priority: priority of the service
@@ -1461,11 +1463,8 @@ def __init__(
14611463
"""
14621464

14631465
# Accept both none, or one, but not both.
1464-
assert (
1465-
(address is None and addresses is None)
1466-
or (address is None and addresses)
1467-
or (address and addresses is None)
1468-
)
1466+
if address is not None and addresses is not None:
1467+
raise TypeError("address and addresses cannot be provided together")
14691468

14701469
if not type_.endswith(service_type_name(name, allow_underscores=True)):
14711470
raise BadTypeInNameException
@@ -1474,6 +1473,7 @@ def __init__(
14741473
if addresses is not None:
14751474
self.addresses = addresses
14761475
elif address is not None:
1476+
warnings.warn("address is deprecated, use addresses instead", DeprecationWarning)
14771477
if isinstance(address, list):
14781478
self.addresses = address
14791479
else:
@@ -1492,6 +1492,22 @@ def __init__(
14921492
self.host_ttl = host_ttl
14931493
self.other_ttl = other_ttl
14941494

1495+
@property
1496+
def address(self):
1497+
warnings.warn("ServiceInfo.address is deprecated, use addresses instead", DeprecationWarning)
1498+
try:
1499+
return self.addresses[0]
1500+
except IndexError:
1501+
return None
1502+
1503+
@address.setter
1504+
def address(self, value):
1505+
warnings.warn("ServiceInfo.address is deprecated, use addresses instead", DeprecationWarning)
1506+
if value is None:
1507+
self.addresses = []
1508+
else:
1509+
self.addresses = [value]
1510+
14951511
@property
14961512
def properties(self) -> ServicePropertiesType:
14971513
return self._properties

0 commit comments

Comments
 (0)