This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: ip_network does not clear/update the broadcast_address cache when network_address is changed.
Type: behavior Stage: resolved
Components: Documentation Versions: Python 3.8, Python 3.7
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: docs@python Nosy List: 992jo, docs@python, methane
Priority: normal Keywords:

Created on 2019-11-13 08:08 by 992jo, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (5)
msg356522 - (view) Author: Johannes Erwerle (992jo) Date: 2019-11-13 08:08
The ip_network class in the ipaddress module does cache the broadcast_address attribute.
But when the network address is changed the cache is not cleared/updated.

Example:

> from ipaddress import ip_network
> 
> print("------------------------------")
> 
> net = ip_network("10.0.0.0/8")
> print("net", net)
> print("broadcast_address", net.broadcast_address)
> print("increase")
> net.network_address += 2**24
> print("net", net)
> print("net.broadcast_address", net.broadcast_address)
>
> print("------------------------------")
>
> net2 = ip_network("10.0.0.0/8")
> print("net2", net2)
> print("no call to broadcast_address")
> print("increase")
> net2.network_address += 2**24
> print("net2", net2)
> print("net2.broadcast_address", net2.broadcast_address)
msg356529 - (view) Author: Inada Naoki (methane) * (Python committer) Date: 2019-11-13 11:00
All classes in the ipaddress module are designed as immutable.

While it is not documented, you can see __hash__ is overridden.  It means you must not change the object state.
msg356531 - (view) Author: Johannes Erwerle (992jo) Date: 2019-11-13 13:52
since it hasn't been documented that those classes are all designed to be immutable (and many things work when they are mutable) many people probably use it that way.
Declaring them immutable via the docs now would "break" existing code.
msg356532 - (view) Author: Inada Naoki (methane) * (Python committer) Date: 2019-11-13 14:16
It is documented, if you read it carefully.

"Network objects are hashable, so they can be used as keys in dictionaries."

https://docs.python.org/3/library/ipaddress.html#network-objects

"""
An object is hashable if it has a hash value which never changes during its lifetime (it needs a __hash__() method), and can be compared to other objects (it needs an __eq__() method). Hashable objects which compare equal must have the same hash value.
"""

https://docs.python.org/3/glossary.html#term-hashable

Make it mutable is not an option.
msg367080 - (view) Author: Inada Naoki (methane) * (Python committer) Date: 2020-04-23 05:39
I close this issue as rejected.
If you have opinion about this issue, please post to python-dev mailing list
or discuss.python.org.
History
Date User Action Args
2022-04-11 14:59:23adminsetgithub: 82965
2020-04-23 05:39:49methanesetstatus: open -> closed
resolution: rejected
messages: + msg367080

stage: resolved
2019-11-13 14:16:39methanesetmessages: + msg356532
2019-11-13 13:52:07992josetmessages: + msg356531
2019-11-13 11:00:46methanesetnosy: + docs@python, methane
messages: + msg356529

assignee: docs@python
components: + Documentation
2019-11-13 08:22:57992josettitle: ip_network does not clear/update the broadcast_address cache when the IP address is changed. -> ip_network does not clear/update the broadcast_address cache when network_address is changed.
2019-11-13 08:09:07992josetversions: + Python 3.8
2019-11-13 08:08:43992jocreate