Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Doc/library/fractions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ another rational number, or from a string.
:class:`Fraction` implements ``__int__`` now to satisfy
``typing.SupportsInt`` instance checks.

.. versionchanged:: 3.12
Space is allowed around the slash for string inputs: `Fraction('2 / 3')`.

.. attribute:: numerator

Numerator of the Fraction in lowest term.
Expand Down
2 changes: 1 addition & 1 deletion Lib/fractions.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
(?=\d|\.\d) # lookahead for digit or .digit
(?P<num>\d*|\d+(_\d+)*) # numerator (possibly empty)
(?: # followed by
(?:/(?P<denom>\d+(_\d+)*))? # an optional denominator
(?:\s*/\s*(?P<denom>\d+(_\d+)*))? # an optional denominator
| # or
(?:\.(?P<decimal>d*|\d+(_\d+)*))? # an optional fractional part
(?:E(?P<exp>[-+]?\d+(_\d+)*))? # and optional exponent
Expand Down
4 changes: 1 addition & 3 deletions Lib/test/test_fractions.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ def testInitFromDecimal(self):
def testFromString(self):
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(" \n +3/2")))
self.assertEqual((-3, 2), _components(F("-3/2 ")))
self.assertEqual((13, 2), _components(F(" 013/02 \n ")))
Expand Down Expand Up @@ -190,9 +191,6 @@ def testFromString(self):
self.assertRaisesMessage(
ValueError, "Invalid literal for Fraction: '/2'",
F, "/2")
self.assertRaisesMessage(
ValueError, "Invalid literal for Fraction: '3 /2'",
F, "3 /2")
self.assertRaisesMessage(
# Denominators don't need a sign.
ValueError, "Invalid literal for Fraction: '3/+2'",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fraction literals now support whitespace around the forward slash,
`Fraction('2 / 3')`.