-
-
Notifications
You must be signed in to change notification settings - Fork 30k
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
Conversation
Doc/library/ipaddress.rst
Outdated
@@ -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 |
There was a problem hiding this comment.
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.
Doc/library/ipaddress.rst
Outdated
representations, the form specifier ``'#'`` and the grouping option | ||
``'_'`` are available. | ||
|
||
>>> ipaddress.IPv4Address('192.168.0.1').__format__('s') |
There was a problem hiding this comment.
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.
Doc/library/ipaddress.rst
Outdated
.. _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 | ||
|
||
There was a problem hiding this comment.
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)
There was a problem hiding this 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.
Doc/library/ipaddress.rst
Outdated
>>> '{:#b}'.format(ipaddress.IPv4Address('192.168.0.1')) | ||
'0b11000000101010000000000000000001' | ||
>>> ipv6_address = ipaddress.IPv6Address('2001:db8::1000') | ||
>>> f'{ipv6_address:'s'}' |
There was a problem hiding this comment.
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.
@John-Ted: Status check is done, and it's a success ✅ . |
1 similar comment
@John-Ted: Status check is done, and it's a success ✅ . |
Sorry, I can't merge this PR. Reason: |
1 similar comment
Sorry, I can't merge this PR. Reason: |
@John-Ted: Status check is done, and it's a success ✅ . |
1 similar comment
@John-Ted: Status check is done, and it's a success ✅ . |
Sorry, I can't merge this PR. Reason: |
1 similar comment
Sorry, I can't merge this PR. Reason: |
@John-Ted: Status check is done, and it's a failure ❌ . |
@John-Ted: Status check is done, and it's a success ✅ . |
Thanks @John-Ted for the PR 🌮🎉.. I'm working now to backport this PR to: 3.9. |
Automerge-Triggered-By: GH:ericvsmith (cherry picked from commit 3317466) Co-authored-by: Teugea Ioan-Teodor <teodor.teugea@gmail.com>
GH-23032 is a backport of this pull request to the 3.9 branch. |
Automerge-Triggered-By: GH:ericvsmith
https://bugs.python.org/issue42061
Automerge-Triggered-By: GH:ericvsmith