Skip to content

Commit 481cbe8

Browse files
bpo-18802: Add more details to ipaddress documentation (GH-6083)
Original patch by Jon Foster and Berker Peksag. (cherry picked from commit 5609b78) Co-authored-by: Cheryl Sabella <[email protected]>
1 parent 47a0e64 commit 481cbe8

File tree

3 files changed

+51
-16
lines changed

3 files changed

+51
-16
lines changed

Doc/library/ipaddress.rst

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ Address objects
8989
The :class:`IPv4Address` and :class:`IPv6Address` objects share a lot of common
9090
attributes. Some attributes that are only meaningful for IPv6 addresses are
9191
also implemented by :class:`IPv4Address` objects, in order to make it easier to
92-
write code that handles both IP versions correctly.
92+
write code that handles both IP versions correctly. Address objects are
93+
:term:`hashable`, so they can be used as keys in dictionaries.
9394

9495
.. class:: IPv4Address(address)
9596

@@ -366,6 +367,8 @@ All attributes implemented by address objects are implemented by network
366367
objects as well. In addition, network objects implement additional attributes.
367368
All of these are common between :class:`IPv4Network` and :class:`IPv6Network`,
368369
so to avoid duplication they are only documented for :class:`IPv4Network`.
370+
Network objects are :term:`hashable`, so they can be used as keys in
371+
dictionaries.
369372

370373
.. class:: IPv4Network(address, strict=True)
371374

@@ -375,8 +378,9 @@ so to avoid duplication they are only documented for :class:`IPv4Network`.
375378
a slash (``/``). The IP address is the network address, and the mask
376379
can be either a single number, which means it's a *prefix*, or a string
377380
representation of an IPv4 address. If it's the latter, the mask is
378-
interpreted as a *net mask* if it starts with a non-zero field, or as
379-
a *host mask* if it starts with a zero field. If no mask is provided,
381+
interpreted as a *net mask* if it starts with a non-zero field, or as a
382+
*host mask* if it starts with a zero field, with the single exception of
383+
an all-zero mask which is treated as a *net mask*. If no mask is provided,
380384
it's considered to be ``/32``.
381385

382386
For example, the following *address* specifications are equivalent:
@@ -406,7 +410,7 @@ so to avoid duplication they are only documented for :class:`IPv4Network`.
406410

407411
Unless stated otherwise, all network methods accepting other network/address
408412
objects will raise :exc:`TypeError` if the argument's IP version is
409-
incompatible to ``self``
413+
incompatible to ``self``.
410414

411415
.. versionchanged:: 3.5
412416

@@ -416,7 +420,7 @@ so to avoid duplication they are only documented for :class:`IPv4Network`.
416420
.. attribute:: max_prefixlen
417421

418422
Refer to the corresponding attribute documentation in
419-
:class:`IPv4Address`
423+
:class:`IPv4Address`.
420424

421425
.. attribute:: is_multicast
422426
.. attribute:: is_private
@@ -426,7 +430,7 @@ so to avoid duplication they are only documented for :class:`IPv4Network`.
426430
.. attribute:: is_link_local
427431

428432
These attributes are true for the network as a whole if they are true
429-
for both the network address and the broadcast address
433+
for both the network address and the broadcast address.
430434

431435
.. attribute:: network_address
432436

@@ -563,10 +567,10 @@ so to avoid duplication they are only documented for :class:`IPv4Network`.
563567

564568
Construct an IPv6 network definition. *address* can be one of the following:
565569

566-
1. A string consisting of an IP address and an optional mask, separated by
567-
a slash (``/``). The IP address is the network address, and the mask
568-
is a single number, which represents a *prefix*. If no mask is provided,
569-
it's considered to be ``/128``.
570+
1. A string consisting of an IP address and an optional prefix length,
571+
separated by a slash (``/``). The IP address is the network address,
572+
and the prefix length must be a single number, the *prefix*. If no
573+
prefix length is provided, it's considered to be ``/128``.
570574

571575
Note that currently expanded netmasks are not supported. That means
572576
``2001:db00::0/24`` is a valid argument while ``2001:db00::0/ffff:ff00::``
@@ -623,12 +627,12 @@ so to avoid duplication they are only documented for :class:`IPv4Network`.
623627
.. method:: compare_networks(other)
624628

625629
Refer to the corresponding attribute documentation in
626-
:class:`IPv4Network`
630+
:class:`IPv4Network`.
627631

628632
.. attribute:: is_site_local
629633

630634
These attribute is true for the network as a whole if it is true
631-
for both the network address and the broadcast address
635+
for both the network address and the broadcast address.
632636

633637

634638
Operators
@@ -642,8 +646,8 @@ IPv6).
642646
Logical operators
643647
"""""""""""""""""
644648

645-
Network objects can be compared with the usual set of logical operators,
646-
similarly to address objects.
649+
Network objects can be compared with the usual set of logical operators.
650+
Network objects are ordered first by network address, then by net mask.
647651

648652

649653
Iteration
@@ -693,6 +697,9 @@ Network objects can act as containers of addresses. Some examples::
693697
Interface objects
694698
-----------------
695699

700+
Interface objects are :term:`hashable`, so they can be used as keys in
701+
dictionaries.
702+
696703
.. class:: IPv4Interface(address)
697704

698705
Construct an IPv4 interface. The meaning of *address* is as in the
@@ -764,6 +771,30 @@ Interface objects
764771
:class:`IPv4Interface`.
765772

766773

774+
Operators
775+
^^^^^^^^^
776+
777+
Interface objects support some operators. Unless stated otherwise, operators
778+
can only be applied between compatible objects (i.e. IPv4 with IPv4, IPv6 with
779+
IPv6).
780+
781+
782+
Logical operators
783+
"""""""""""""""""
784+
785+
Interface objects can be compared with the usual set of logical operators.
786+
787+
For equality comparison (``==`` and ``!=``), both the IP address and network
788+
must be the same for the objects to be equal. An interface will not compare
789+
equal to any address or network object.
790+
791+
For ordering (``<``, ``>``, etc) the rules are different. Interface and
792+
address objects with the same IP version can be compared, and the address
793+
objects will always sort before the interface objects. Two interface objects
794+
are first compared by their networks and, if those are the same, then by their
795+
IP addresses.
796+
797+
767798
Other Module Level Functions
768799
----------------------------
769800

@@ -829,7 +860,7 @@ The module also provides the following module level functions:
829860

830861
doesn't make sense. There are some times however, where you may wish to
831862
have :mod:`ipaddress` sort these anyway. If you need to do this, you can use
832-
this function as the ``key`` argument to :func:`sorted()`.
863+
this function as the *key* argument to :func:`sorted()`.
833864

834865
*obj* is either a network or address object.
835866

@@ -847,4 +878,4 @@ module defines the following exceptions:
847878

848879
.. exception:: NetmaskValueError(ValueError)
849880

850-
Any value error related to the netmask.
881+
Any value error related to the net mask.

Lib/test/test_ipaddress.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,9 @@ def test_weakref(self):
405405
class NetmaskTestMixin_v4(CommonTestMixin_v4):
406406
"""Input validation on interfaces and networks is very similar"""
407407

408+
def test_no_mask(self):
409+
self.assertEqual(str(self.factory('1.2.3.4')), '1.2.3.4/32')
410+
408411
def test_split_netmask(self):
409412
addr = "1.2.3.4/32/24"
410413
with self.assertAddressError("Only one '/' permitted in %r" % addr):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Documentation changes for ipaddress. Patch by Jon Foster and Berker Peksag.

0 commit comments

Comments
 (0)