[New-bugs-announce] [issue38335] simplify overlaps function in ipaddress.py

Sanjay report at bugs.python.org
Tue Oct 1 07:43:43 EDT 2019


New submission from Sanjay <sanjay537 at gmail.com>:

the current implementation of overlaps function tests either network or broadcast address is in other but
we can skip checking broadcast address is in other because we anyway check if other.network_address in self 

without loss of generality if we assume self has smaller prefixlen than other then when self.broadcast_address in other then other.network_address *SHOULD* be in self but the reverse is not true

so my first patch was to make the function logic simply do
`self.network_address in other or other.network_address in self`

but the current PR does a different change. We have introduced two new functions subnet_of and supernet_of

for two networks A, B there are only three possibilities 
1. they don't overlap
2. A is subnet of B
3. B is subnet of A

so we can reuse the existing function and just do
`return self.subnet_of(other) or self.supernet_of(other)`
the only thing is while overlaps() function returns false when we try to compare with a network or with diff version the other throws exception so I added a typecheck in the beginning.

P.S the docstring is slightly convoluted for newcomers, based on the three possibilities it should say "Tell if self is supernet or subnet of other" because "partly contained" can also mean two ranges intersect which can never happen to network prefixes. I have not made that change but can make it.

There are also some other issues I want to address but I want to do one at a time.

----------
components: Library (Lib)
files: skip_broadcast_in.patch
keywords: patch
messages: 353676
nosy: Sanjay
priority: normal
severity: normal
status: open
title: simplify overlaps function in ipaddress.py
versions: Python 3.5, Python 3.6, Python 3.7, Python 3.8, Python 3.9
Added file: https://bugs.python.org/file48634/skip_broadcast_in.patch

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue38335>
_______________________________________


More information about the New-bugs-announce mailing list