Skip to content

gh-84978: Add float.from_number() and complex.from_number() - #26827

Merged
serhiy-storchaka merged 9 commits into
python:mainfrom
serhiy-storchaka:float-from-number
Jul 15, 2024
Merged

gh-84978: Add float.from_number() and complex.from_number()#26827
serhiy-storchaka merged 9 commits into
python:mainfrom
serhiy-storchaka:float-from-number

Conversation

@serhiy-storchaka

@serhiy-storchakaserhiy-storchaka commented Jun 21, 2021

Copy link
Copy Markdown
Member

They are alternate constructors which only accept numbers
(including objects with special methods __float__, __complex__
and __index__), but not strings.

https://bugs.python.org/issue40801

They are alternate constructors which only accept numbers
(including objects with special methods __float__, __complex__
and __index__), but not strings.
@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

@github-actionsgithub-actionsBot added the stale Stale PR or inactive for long period of time. label Jul 22, 2021
@github-actionsgithub-actionsBot removed the stale Stale PR or inactive for long period of time. label Aug 10, 2022
@mdickinson

Copy link
Copy Markdown
Member

@serhiy-storchaka Are you still interested in pursuing this? Looks like it's still a draft; what's needed to make it ready for review?

@serhiy-storchaka

Copy link
Copy Markdown
MemberAuthor

It needs documentation. I'll write it if the rest be set.

It differs from your original idea (class methods instead of operator functions), so I do not know what you think about this. The spelling is also can be discussed (from_number vs fromnumber, or maybe something other?) Should int.from_number be added?

Should we add also special constructors for parsing? int.parse or int.from_string, etc. It can be done in a different PR, but it is useful to keep it in mind.

@skirpichev

Copy link
Copy Markdown
Member

@serhiy-storchaka, some tests refactoring in test_float/complex is irrelevant for this pr. Can I port this as a separate pr?

@serhiy-storchaka

Copy link
Copy Markdown
MemberAuthor

This tests refactoring in test_float/complex is required for testing from_number() methods. Without it they are irrelevant. Why do you need this?

@skirpichev

Copy link
Copy Markdown
Member

Why do you need this?

I was thinking about similar refactoring while improving test coverage for the complexobject.c. So, maybe this could be useful regardless of the fate of this pr.

@serhiy-storchaka
serhiy-storchaka marked this pull request as ready for review October 14, 2023 20:10
@serhiy-storchakaserhiy-storchaka changed the title bpo-40801: Add float.from_number() and complex.from_number()gh-84978: Add float.from_number() and complex.from_number()Oct 14, 2023
@serhiy-storchaka

Copy link
Copy Markdown
MemberAuthor

@mdickinson I added docs and remove the draft status. Hope it will help with review.

skirpichev added a commit to skirpichev/cpython that referenced this pull request Oct 17, 2023
Taken from python#26827
Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>
@iritkatrieliritkatriel added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Nov 28, 2023

@mdickinsonmdickinson left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This LGTM, and works as expected in my manual testing. I only have a couple of grammar nitpicks.

Thank you!

Comment threadDoc/library/functions.rst Outdated
Comment threadDoc/library/functions.rst Outdated

.. classmethod:: float.from_number(x)

Class method to return a floating point number constructed from a number *x*.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Super-nitpick: I prefer the spelling floating-point number, with a hyphen (the "floating-point" part acts as a compound adjective). I think that's what we mostly use throughout the docs (though no doubt there are exceptions).

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

This descriptions was simply copied from the float constructor description.

"floating-point" occurs 118 time, "floating point" occurs at least 159 times (in several cases it is split between lines, so it is not easy to get accurate number).

So for now I leave "floating point" for consistency with the constructor description. We will solve this in a separate issue.

Comment threadDoc/whatsnew/3.13.rst Outdated
@serhiy-storchaka
serhiy-storchaka enabled auto-merge (squash) July 15, 2024 15:44
@serhiy-storchaka
serhiy-storchaka merged commit 94bee45 into python:mainJul 15, 2024
estyxx pushed a commit to estyxx/cpython that referenced this pull request Jul 17, 2024
…thonGH-26827)
They are alternate constructors which only accept numbers
(including objects with special methods __float__, __complex__
and __index__), but not strings.
@serhiy-storchaka
serhiy-storchaka deleted the float-from-number branch October 21, 2024 13:43
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

interpreter-core(Objects, Python, Grammar, and Parser dirs)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants

@serhiy-storchaka@mdickinson@skirpichev@iritkatriel@the-knights-who-say-ni@ezio-melotti@bedevere-bot