This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Support PEP 515 for Fraction's initialization from string
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.11
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Sergey.Kirpichev, mark.dickinson, veky
Priority: normal Keywords: patch

Created on 2021-05-28 05:47 by Sergey.Kirpichev, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
fractions-from-str.diff Sergey.Kirpichev, 2021-05-28 05:47
Pull Requests
URL Status Linked Edit
PR 26422 merged Sergey.Kirpichev, 2021-05-28 07:24
Messages (6)
msg394633 - (view) Author: Sergey B Kirpichev (Sergey.Kirpichev) * Date: 2021-05-28 05:47
Right now:
>>> from fractions import Fraction as F
>>> F(1_2_3, 3_2_1)
Fraction(41, 107)

but

>>> F('1_2_3/3_2_1')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/sk/src/cpython/Lib/fractions.py", line 115, in __new__
    raise ValueError('Invalid literal for Fraction: %r' %
ValueError: Invalid literal for Fraction: '1_2_3/3_2_1'

or even this (should be consistent with int constructor, isn't?):
>>> F('1_2_3')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/sk/src/cpython/Lib/fractions.py", line 115, in __new__
    raise ValueError('Invalid literal for Fraction: %r' %
ValueError: Invalid literal for Fraction: '1_2_3'

Tentative patch attached.  Let me know if this does make sense as a PR.
msg394638 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2021-05-28 06:48
+1 to the idea: `Decimal`, `float`, `complex` and `int` all understand the underscores; there's no good reason for `Fraction` not to.

So yes please to the PR. There should be tests that check that only the underscore rules allowed by `int` and the others are supported: e.g., `Fraction("1__2")`, and `Fraction("1_2_")` should be errors.
msg394642 - (view) Author: Sergey B Kirpichev (Sergey.Kirpichev) * Date: 2021-05-28 07:28
On Fri, May 28, 2021 at 06:48:14AM +0000, Mark Dickinson wrote:
> So yes please to the PR. There should be tests that check that only the
> underscore rules allowed by `int` and the others are supported: e.g.,
> `Fraction("1__2")`, and `Fraction("1_2_")` should be errors.

Ok, I did.

In the initial version I catch int()'s exceptions to
return a correct exception details.  Maybe it's better to fix
the regexp instead (don't match "wrong" strings).  I think
this might be more complicated for readers...
msg394730 - (view) Author: Vedran Čačić (veky) * Date: 2021-05-29 13:36
How about '1_/_2'? I think making / more separated adds value... though of course, someone will ask about '1 / 2' next. :-)
msg394731 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2021-05-29 13:41
> How about '1_/_2'?

I'd rather keep it consistent with the rules for int: that is, if I split on `'/'`, I'd expect the pieces to be parseable by `int`.

As for spaces around the `/`, let's make that a separate issue.
msg395249 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2021-06-07 07:06
New changeset 89e50ab36fac6a0e7f1998501f36fcd2872a6604 by Sergey B Kirpichev in branch 'main':
bpo-44258: support PEP 515 for Fraction's initialization from string (GH-26422)
https://github.com/python/cpython/commit/89e50ab36fac6a0e7f1998501f36fcd2872a6604
History
Date User Action Args
2022-04-11 14:59:46adminsetgithub: 88424
2021-06-07 07:07:33mark.dickinsonsetstatus: open -> closed
type: enhancement
resolution: fixed
stage: patch review -> resolved
2021-06-07 07:06:42mark.dickinsonsetmessages: + msg395249
2021-05-29 13:41:10mark.dickinsonsetmessages: + msg394731
2021-05-29 13:36:25vekysetnosy: + veky
messages: + msg394730
2021-05-28 07:28:35Sergey.Kirpichevsetmessages: + msg394642
2021-05-28 07:24:04Sergey.Kirpichevsetstage: patch review
pull_requests: + pull_request25017
2021-05-28 06:48:14mark.dickinsonsetnosy: + mark.dickinson
messages: + msg394638
2021-05-28 05:47:31Sergey.Kirpichevcreate