Skip to content
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

bpo-42061: Document __format__ for IP addresses #23018

Merged
merged 10 commits into from
Oct 29, 2020
Merged

bpo-42061: Document __format__ for IP addresses #23018

merged 10 commits into from
Oct 29, 2020

Conversation

John-Ted
Copy link
Contributor

@John-Ted John-Ted commented Oct 28, 2020

https://bugs.python.org/issue42061

Automerge-Triggered-By: GH:ericvsmith

@@ -317,6 +317,32 @@ the :func:`str` and :func:`int` builtin functions::
Note that IPv6 scoped addresses are converted to integers without scope zone ID.


Other formatting options
Copy link
Member

Choose a reason for hiding this comment

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

I think we should add __format__ under the list of methods and document this there, similar to how datetime does it. As written, this section doesn't mention __format__. Unless you know that format() calls __format__ (and most people don't), then I'm not sure you'd make the connection that these format options are also available with str.format() and f-strings.

representations, the form specifier ``'#'`` and the grouping option
``'_'`` are available.

>>> ipaddress.IPv4Address('192.168.0.1').__format__('s')
Copy link
Member

Choose a reason for hiding this comment

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

Having said that I think the documentation belongs under __format__ (which I still think is the right thing to do), I'm not sure using __format__ in the examples is the right thing. We discourage people from calling dunders directly. Maybe there should be a note that __format__ is used by format, str.format, and f-strings, and then show examples using one or more of those? I'm open to suggestions here.

.. _iana-ipv4-special-registry: https://www.iana.org/assignments/iana-ipv4-special-registry/iana-ipv4-special-registry.xhtml
.. _iana-ipv6-special-registry: https://www.iana.org/assignments/iana-ipv6-special-registry/iana-ipv6-special-registry.xhtml

Copy link
Member

Choose a reason for hiding this comment

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

Extraneous whitespace here.

The examples no longer use the dunder directly and the
__format__ documentation is changed to be like that of datetime
(as suggested in the first review)
Copy link
Member

@ericvsmith ericvsmith left a comment

Choose a reason for hiding this comment

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

Looks great to me! Thanks for your patience.

>>> '{:#b}'.format(ipaddress.IPv4Address('192.168.0.1'))
'0b11000000101010000000000000000001'
>>> ipv6_address = ipaddress.IPv6Address('2001:db8::1000')
>>> f'{ipv6_address:'s'}'
Copy link
Member

Choose a reason for hiding this comment

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

Oops, I spoke too soon. There's a typo in the f-string that the automated doctest bot is objecting to.

@miss-islington
Copy link
Contributor

@John-Ted: Status check is done, and it's a success ✅ .

1 similar comment
@miss-islington
Copy link
Contributor

@John-Ted: Status check is done, and it's a success ✅ .

@miss-islington
Copy link
Contributor

Sorry, I can't merge this PR. Reason: 2 of 4 required status checks are expected..

1 similar comment
@miss-islington
Copy link
Contributor

Sorry, I can't merge this PR. Reason: 2 of 4 required status checks are expected..

@miss-islington
Copy link
Contributor

@John-Ted: Status check is done, and it's a success ✅ .

1 similar comment
@miss-islington
Copy link
Contributor

@John-Ted: Status check is done, and it's a success ✅ .

@miss-islington
Copy link
Contributor

Sorry, I can't merge this PR. Reason: 2 of 4 required status checks are expected..

1 similar comment
@miss-islington
Copy link
Contributor

Sorry, I can't merge this PR. Reason: 2 of 4 required status checks are expected..

@miss-islington
Copy link
Contributor

@John-Ted: Status check is done, and it's a failure ❌ .

@miss-islington
Copy link
Contributor

@John-Ted: Status check is done, and it's a success ✅ .

@miss-islington miss-islington merged commit 3317466 into python:master Oct 29, 2020
@miss-islington
Copy link
Contributor

Thanks @John-Ted for the PR 🌮🎉.. I'm working now to backport this PR to: 3.9.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Oct 29, 2020
Automerge-Triggered-By: GH:ericvsmith
(cherry picked from commit 3317466)

Co-authored-by: Teugea Ioan-Teodor <teodor.teugea@gmail.com>
@bedevere-bot bedevere-bot removed the needs backport to 3.9 only security fixes label Oct 29, 2020
@bedevere-bot
Copy link

GH-23032 is a backport of this pull request to the 3.9 branch.

miss-islington added a commit that referenced this pull request Oct 29, 2020
Automerge-Triggered-By: GH:ericvsmith
(cherry picked from commit 3317466)

Co-authored-by: Teugea Ioan-Teodor <teodor.teugea@gmail.com>
@John-Ted John-Ted deleted the document_ip_format branch October 29, 2020 22:38
adorilson pushed a commit to adorilson/cpython that referenced this pull request Mar 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants