bpo-44258: support PEP 515 for Fraction's initialization from string (GH-26422)

* bpo-44258: support PEP 515 for Fraction's initialization from string

* regexps's version

* A different regexps version, which doesn't suffer from catastrophic backtracking

* revert denom -> den

* strip "_" from the decimal str, add few tests

* drop redundant tests

* Add versionchanged & whatsnew entry

* Amend Fraction constructor docs

* Change .. versionchanged:...
This commit is contained in:
Sergey B Kirpichev 2021-06-07 10:06:33 +03:00 committed by GitHub
parent afb2eed72b
commit 89e50ab36f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 85 additions and 11 deletions

View file

@ -42,7 +42,8 @@ another rational number, or from a string.
where the optional ``sign`` may be either '+' or '-' and
``numerator`` and ``denominator`` (if present) are strings of
decimal digits. In addition, any string that represents a finite
decimal digits (underscores may be used to delimit digits as with
integral literals in code). In addition, any string that represents a finite
value and is accepted by the :class:`float` constructor is also
accepted by the :class:`Fraction` constructor. In either form the
input string may also have leading and/or trailing whitespace.
@ -89,6 +90,10 @@ another rational number, or from a string.
and *denominator*. :func:`math.gcd` always return a :class:`int` type.
Previously, the GCD type depended on *numerator* and *denominator*.
.. versionchanged:: 3.11
Underscores are now permitted when creating a :class:`Fraction` instance
from a string, following :PEP:`515` rules.
.. attribute:: numerator
Numerator of the Fraction in lowest term.