Skip to content

Conversation

@Yashp002
Copy link
Contributor

@Yashp002 Yashp002 commented Nov 30, 2025

This PR clarifies how Decimal behaves differently when formatted using __format__ (f-strings, format(), str.format()) versus % formatting (old-style string formatting).

Changes:

  • Added a note in the Format Specification Mini-Language section explaining:
    • For __format__: Decimal uses maximum precision only when precision is omitted in f/e/g format types; explicit precision is respected
    • For % formatting: Decimal values are converted to float first, resulting in potential precision loss

Documentation preview: (will be auto-generated)

Fixes #142108


📚 Documentation preview 📚: https://cpython-previews--142117.org.readthedocs.build/

Copy link
Member

@StanFromIreland StanFromIreland left a comment

Choose a reason for hiding this comment

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

You have multiple different changes mixed together, please split it out.

Copy link
Contributor

@skirpichev skirpichev left a comment

Choose a reason for hiding this comment

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

I'll mark this a draft. Sorry, but current approach seems unacceptable for me.

PS: Please also don't alter commit history in any way (force-pushes, etc).

.. c:function:: PyObject* PyDescr_NewMember(PyTypeObject *type, struct PyMemberDef *meth)
.. c:var:: PyTypeObject PyMemberDescr_Type
Copy link
Contributor

Choose a reason for hiding this comment

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

Changes in Doc/c-api/ are not related to this pr, please revert.

Comment on lines +637 to +638
* When using ``__format__`` (f-strings, ``format()``, ``str.format()``),
Decimal formats with maximum precision only when precision is **omitted**
Copy link
Contributor

Choose a reason for hiding this comment

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

This is already documented, please revert. Also, the whole section of documentation is about string formatting, it's pointless to say that you are using __format__().

Comment on lines +642 to +643
* When using ``%`` formatting (old-style string formatting with the ``%`` operator),
Decimal values are first converted to :class:`float`, which may result in
Copy link
Contributor

Choose a reason for hiding this comment

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

  1. This note not belongs to this section, it's about printf-like formatting.
  2. In the issue thread it was explained, that there is nothing special about Decimal's here. If we have to document something, it should be about behavior of printf-like formatting for non-builtin types.

@skirpichev skirpichev marked this pull request as draft November 30, 2025 21:16
@Yashp002
Copy link
Contributor Author

Yashp002 commented Dec 1, 2025

Apologies for the confusion—I had an out-of-sync branch that mixed commits from multiple PRs.

After reviewing the feedback, I realize:

  • The note placement was incorrect (this section is about printf-like formatting)
  • This behavior may already be documented elsewhere

Closing this PR. I'll discuss on the issue thread whether any documentation changes are still needed before opening a fresh PR.

Thanks for the review.

@Yashp002 Yashp002 closed this Dec 1, 2025
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 skip news

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

Improve printf-style formatting docs for non-built-in types

3 participants