mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Allow whitespace around a slash in fraction string inputs (GH-96496)
This commit is contained in:
parent
91f40f3f78
commit
656167db81
4 changed files with 7 additions and 4 deletions
|
@ -98,6 +98,9 @@ another rational number, or from a string.
|
||||||
:class:`Fraction` implements ``__int__`` now to satisfy
|
:class:`Fraction` implements ``__int__`` now to satisfy
|
||||||
``typing.SupportsInt`` instance checks.
|
``typing.SupportsInt`` instance checks.
|
||||||
|
|
||||||
|
.. versionchanged:: 3.12
|
||||||
|
Space is allowed around the slash for string inputs: `Fraction('2 / 3')`.
|
||||||
|
|
||||||
.. attribute:: numerator
|
.. attribute:: numerator
|
||||||
|
|
||||||
Numerator of the Fraction in lowest term.
|
Numerator of the Fraction in lowest term.
|
||||||
|
|
|
@ -26,7 +26,7 @@ _RATIONAL_FORMAT = re.compile(r"""
|
||||||
(?=\d|\.\d) # lookahead for digit or .digit
|
(?=\d|\.\d) # lookahead for digit or .digit
|
||||||
(?P<num>\d*|\d+(_\d+)*) # numerator (possibly empty)
|
(?P<num>\d*|\d+(_\d+)*) # numerator (possibly empty)
|
||||||
(?: # followed by
|
(?: # followed by
|
||||||
(?:/(?P<denom>\d+(_\d+)*))? # an optional denominator
|
(?:\s*/\s*(?P<denom>\d+(_\d+)*))? # an optional denominator
|
||||||
| # or
|
| # or
|
||||||
(?:\.(?P<decimal>d*|\d+(_\d+)*))? # an optional fractional part
|
(?:\.(?P<decimal>d*|\d+(_\d+)*))? # an optional fractional part
|
||||||
(?:E(?P<exp>[-+]?\d+(_\d+)*))? # and optional exponent
|
(?:E(?P<exp>[-+]?\d+(_\d+)*))? # and optional exponent
|
||||||
|
|
|
@ -162,6 +162,7 @@ class FractionTest(unittest.TestCase):
|
||||||
def testFromString(self):
|
def testFromString(self):
|
||||||
self.assertEqual((5, 1), _components(F("5")))
|
self.assertEqual((5, 1), _components(F("5")))
|
||||||
self.assertEqual((3, 2), _components(F("3/2")))
|
self.assertEqual((3, 2), _components(F("3/2")))
|
||||||
|
self.assertEqual((3, 2), _components(F("3 / 2")))
|
||||||
self.assertEqual((3, 2), _components(F(" \n +3/2")))
|
self.assertEqual((3, 2), _components(F(" \n +3/2")))
|
||||||
self.assertEqual((-3, 2), _components(F("-3/2 ")))
|
self.assertEqual((-3, 2), _components(F("-3/2 ")))
|
||||||
self.assertEqual((13, 2), _components(F(" 013/02 \n ")))
|
self.assertEqual((13, 2), _components(F(" 013/02 \n ")))
|
||||||
|
@ -190,9 +191,6 @@ class FractionTest(unittest.TestCase):
|
||||||
self.assertRaisesMessage(
|
self.assertRaisesMessage(
|
||||||
ValueError, "Invalid literal for Fraction: '/2'",
|
ValueError, "Invalid literal for Fraction: '/2'",
|
||||||
F, "/2")
|
F, "/2")
|
||||||
self.assertRaisesMessage(
|
|
||||||
ValueError, "Invalid literal for Fraction: '3 /2'",
|
|
||||||
F, "3 /2")
|
|
||||||
self.assertRaisesMessage(
|
self.assertRaisesMessage(
|
||||||
# Denominators don't need a sign.
|
# Denominators don't need a sign.
|
||||||
ValueError, "Invalid literal for Fraction: '3/+2'",
|
ValueError, "Invalid literal for Fraction: '3/+2'",
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Fraction literals now support whitespace around the forward slash,
|
||||||
|
`Fraction('2 / 3')`.
|
Loading…
Add table
Add a link
Reference in a new issue