Skip to content

bpo-46646: mention that bytes are accepted in ipaddress docs #31139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions Doc/library/ipaddress.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ IP addresses, networks and interfaces:
.. function:: ip_address(address)

Return an :class:`IPv4Address` or :class:`IPv6Address` object depending on
the IP address passed as argument. Either IPv4 or IPv6 addresses may be
the IP address passed as argument.
*address* is a string or integer or bytes representing the IP address.
Either IPv4 or IPv6 addresses may be
supplied; integers less than ``2**32`` will be considered to be IPv4 by default.
A :exc:`ValueError` is raised if *address* does not represent a valid IPv4
or IPv6 address.
Comment on lines 42 to 48
Copy link
Contributor

@GBeauregard GBeauregard Feb 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes the diff ugly, but maybe it's still worth reflowing these to fit the style in the rest of the file? Is there a convention when making these changes (to not do this)? (would love a resource if you have one)

   Return an :class:`IPv4Address` or :class:`IPv6Address` object depending on
   the IP address passed as argument. *address* is a string or integer or bytes
   representing the IP address. Either IPv4 or IPv6 addresses may be supplied;
   integers less than ``2**32`` will be considered to be IPv4 by default. A
   :exc:`ValueError` is raised if *address* does not represent a valid IPv4 or
   IPv6 address.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have an opinion on that, I will do anything that is considered the best by maintainers 🙂

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The feedback I got from this PR was that it's generally preferred to keep the diff on docs PRs as small as possible, even if that means that some lines go way over (or way under) the 80-char PEP8 limit.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a line really goes way over 80 characters, then split it in two 🙂

Expand All @@ -54,8 +56,9 @@ IP addresses, networks and interfaces:
.. function:: ip_network(address, strict=True)

Return an :class:`IPv4Network` or :class:`IPv6Network` object depending on
the IP address passed as argument. *address* is a string or integer
representing the IP network. Either IPv4 or IPv6 networks may be supplied;
the IP address passed as argument.
*address* is a string or integer or bytes representing the IP network.
Either IPv4 or IPv6 networks may be supplied;
integers less than ``2**32`` will be considered to be IPv4 by default. *strict*
is passed to :class:`IPv4Network` or :class:`IPv6Network` constructor. A
:exc:`ValueError` is raised if *address* does not represent a valid IPv4 or
Expand All @@ -68,12 +71,16 @@ IP addresses, networks and interfaces:
.. function:: ip_interface(address)

Return an :class:`IPv4Interface` or :class:`IPv6Interface` object depending
on the IP address passed as argument. *address* is a string or integer
representing the IP address. Either IPv4 or IPv6 addresses may be supplied;
on the IP address passed as argument.
*address* is a string or integer or bytes representing the IP address.
Either IPv4 or IPv6 addresses may be supplied;
integers less than ``2**32`` will be considered to be IPv4 by default. A
:exc:`ValueError` is raised if *address* does not represent a valid IPv4 or
IPv6 address.

>>> ipaddress.ip_interface('192.168.0.0/28')
IPv4Interface('192.168.0.0/28')

One downside of these convenience functions is that the need to handle both
IPv4 and IPv6 formats means that error messages provide minimal
information on the precise error, as the functions don't know whether the
Expand Down
16 changes: 8 additions & 8 deletions Lib/ipaddress.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ class NetmaskValueError(ValueError):


def ip_address(address):
"""Take an IP string/int and return an object of the correct type.
"""Take an IP string/int/bytes and return an object of the correct type.

Args:
address: A string or integer, the IP address. Either IPv4 or
address: A string or integer or bytes, the IP address. Either IPv4 or
IPv6 addresses may be supplied; integers less than 2**32 will
be considered to be IPv4 by default.

Expand All @@ -56,18 +56,18 @@ def ip_address(address):


def ip_network(address, strict=True):
"""Take an IP string/int and return an object of the correct type.
"""Take an IP string/int/bytes and return an object of the correct type.

Args:
address: A string or integer, the IP network. Either IPv4 or
address: A string or integer or bytes, the IP network. Either IPv4 or
IPv6 networks may be supplied; integers less than 2**32 will
be considered to be IPv4 by default.

Returns:
An IPv4Network or IPv6Network object.

Raises:
ValueError: if the string passed isn't either a v4 or a v6
ValueError: if the *address* passed isn't either a v4 or a v6
address. Or if the network has host bits set.

"""
Expand All @@ -86,18 +86,18 @@ def ip_network(address, strict=True):


def ip_interface(address):
"""Take an IP string/int and return an object of the correct type.
"""Take an IP string/int/bytes and return an object of the correct type.

Args:
address: A string or integer, the IP address. Either IPv4 or
address: A string or integer or bytes, the IP address. Either IPv4 or
IPv6 addresses may be supplied; integers less than 2**32 will
be considered to be IPv4 by default.

Returns:
An IPv4Interface or IPv6Interface object.

Raises:
ValueError: if the string passed isn't either a v4 or a v6
ValueError: if the *address* passed isn't either a v4 or a v6
address.

Notes:
Expand Down