Skip to content

Conversation

serhiy-storchaka
Copy link
Member

@serhiy-storchaka serhiy-storchaka commented Jun 21, 2021

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

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

@github-actions github-actions bot added the stale Stale PR or inactive for long period of time. label Jul 22, 2021
@github-actions github-actions bot removed the stale Stale PR or inactive for long period of time. label Aug 10, 2022
@mdickinson
Copy link
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
Member Author

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
Contributor

@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
Member Author

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
Contributor

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-storchaka serhiy-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
Member Author

@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>
@iritkatriel iritkatriel added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Nov 28, 2023
Copy link
Member

@mdickinson mdickinson left a comment

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!

@@ -624,6 +624,23 @@ Additional Methods on Float
The float type implements the :class:`numbers.Real` :term:`abstract base
class`. float also has the following additional methods.

.. classmethod:: float.from_number(x)

Class method to return a floating point number constructed from a number *x*.
Copy link
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
Member Author

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.

@serhiy-storchaka serhiy-storchaka enabled auto-merge (squash) July 15, 2024 15:44
@serhiy-storchaka serhiy-storchaka merged commit 94bee45 into python:main Jul 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 free to 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