From be7311bd794b86644f295e071b349fb0d98d3680 Mon Sep 17 00:00:00 2001 From: Qanty_ <89341544+Qanty228@users.noreply.github.com> Date: Mon, 10 Aug 2026 20:40:42 +0300 Subject: [PATCH 01/11] document that float() accepts bytes-like objects --- Objects/clinic/floatobject.c.h | 4 ++-- Objects/floatobject.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Objects/clinic/floatobject.c.h b/Objects/clinic/floatobject.c.h index 8768555c909257..d1e46d43983640 100644 --- a/Objects/clinic/floatobject.c.h +++ b/Objects/clinic/floatobject.c.h @@ -210,7 +210,7 @@ PyDoc_STRVAR(float_new__doc__, "float(x=0, /)\n" "--\n" "\n" -"Convert a string or number to a floating-point number, if possible."); +"Convert a string, number, or bytes-like object to a float, if possible."); static PyObject * float_new_impl(PyTypeObject *type, PyObject *x); @@ -353,4 +353,4 @@ float___format__(PyObject *self, PyObject *arg) exit: return return_value; } -/*[clinic end generated code: output=5d7b0bf9e47ff997 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=de62deb586e0486a input=a9049054013a1b77]*/ diff --git a/Objects/floatobject.c b/Objects/floatobject.c index 17e6a729dcd83f..68e4c3c1dab5b1 100644 --- a/Objects/floatobject.c +++ b/Objects/floatobject.c @@ -1560,12 +1560,12 @@ float.__new__ as float_new x: object(c_default="NULL") = 0 / -Convert a string or number to a floating-point number, if possible. +Convert a string, number, or bytes-like object to a float, if possible. [clinic start generated code]*/ static PyObject * float_new_impl(PyTypeObject *type, PyObject *x) -/*[clinic end generated code: output=ccf1e8dc460ba6ba input=55909f888aa0c8a6]*/ +/*[clinic end generated code: output=ccf1e8dc460ba6ba input=27eb1f7c62226a39]*/ { if (type != &PyFloat_Type) { if (x == NULL) { From 083e8905d72cd9462bd8797127caf5ae84cfe88f Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Mon, 10 Aug 2026 18:13:41 +0000 Subject: [PATCH 02/11] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20b?= =?UTF-8?q?lurb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Documentation/2026-08-10-18-13-39.gh-issue-77748.aVLelN.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Documentation/2026-08-10-18-13-39.gh-issue-77748.aVLelN.rst diff --git a/Misc/NEWS.d/next/Documentation/2026-08-10-18-13-39.gh-issue-77748.aVLelN.rst b/Misc/NEWS.d/next/Documentation/2026-08-10-18-13-39.gh-issue-77748.aVLelN.rst new file mode 100644 index 00000000000000..769007eb718d78 --- /dev/null +++ b/Misc/NEWS.d/next/Documentation/2026-08-10-18-13-39.gh-issue-77748.aVLelN.rst @@ -0,0 +1 @@ +Document that float() accepts bytes-like objects. From 8923c210785994bcd9dabd0cece0e1caf07c5d54 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Mon, 10 Aug 2026 18:13:43 +0000 Subject: [PATCH 03/11] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20b?= =?UTF-8?q?lurb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Documentation/2026-08-10-18-13-42.gh-issue-77748.aVLelN.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Documentation/2026-08-10-18-13-42.gh-issue-77748.aVLelN.rst diff --git a/Misc/NEWS.d/next/Documentation/2026-08-10-18-13-42.gh-issue-77748.aVLelN.rst b/Misc/NEWS.d/next/Documentation/2026-08-10-18-13-42.gh-issue-77748.aVLelN.rst new file mode 100644 index 00000000000000..769007eb718d78 --- /dev/null +++ b/Misc/NEWS.d/next/Documentation/2026-08-10-18-13-42.gh-issue-77748.aVLelN.rst @@ -0,0 +1 @@ +Document that float() accepts bytes-like objects. From f0ce4e37b2e58788b27688f8fcf877acf18f9cbc Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Mon, 10 Aug 2026 18:13:44 +0000 Subject: [PATCH 04/11] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20b?= =?UTF-8?q?lurb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Documentation/2026-08-10-18-13-43.gh-issue-77748.aVLelN.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Documentation/2026-08-10-18-13-43.gh-issue-77748.aVLelN.rst diff --git a/Misc/NEWS.d/next/Documentation/2026-08-10-18-13-43.gh-issue-77748.aVLelN.rst b/Misc/NEWS.d/next/Documentation/2026-08-10-18-13-43.gh-issue-77748.aVLelN.rst new file mode 100644 index 00000000000000..769007eb718d78 --- /dev/null +++ b/Misc/NEWS.d/next/Documentation/2026-08-10-18-13-43.gh-issue-77748.aVLelN.rst @@ -0,0 +1 @@ +Document that float() accepts bytes-like objects. From 7b01c735835d3f04d8eb7db035d8bf40c7ed9044 Mon Sep 17 00:00:00 2001 From: Qanty_ <89341544+Qanty228@users.noreply.github.com> Date: Tue, 11 Aug 2026 17:06:04 +0300 Subject: [PATCH 05/11] Delete duplicate news entry --- .../Documentation/2026-08-10-18-13-39.gh-issue-77748.aVLelN.rst | 1 - 1 file changed, 1 deletion(-) delete mode 100644 Misc/NEWS.d/next/Documentation/2026-08-10-18-13-39.gh-issue-77748.aVLelN.rst diff --git a/Misc/NEWS.d/next/Documentation/2026-08-10-18-13-39.gh-issue-77748.aVLelN.rst b/Misc/NEWS.d/next/Documentation/2026-08-10-18-13-39.gh-issue-77748.aVLelN.rst deleted file mode 100644 index 769007eb718d78..00000000000000 --- a/Misc/NEWS.d/next/Documentation/2026-08-10-18-13-39.gh-issue-77748.aVLelN.rst +++ /dev/null @@ -1 +0,0 @@ -Document that float() accepts bytes-like objects. From 41deea844675e291b8c6d65d6b4d5b9eae486a8e Mon Sep 17 00:00:00 2001 From: Qanty_ <89341544+Qanty228@users.noreply.github.com> Date: Tue, 11 Aug 2026 17:06:58 +0300 Subject: [PATCH 06/11] Delete duplicate news entry --- .../Documentation/2026-08-10-18-13-42.gh-issue-77748.aVLelN.rst | 1 - 1 file changed, 1 deletion(-) delete mode 100644 Misc/NEWS.d/next/Documentation/2026-08-10-18-13-42.gh-issue-77748.aVLelN.rst diff --git a/Misc/NEWS.d/next/Documentation/2026-08-10-18-13-42.gh-issue-77748.aVLelN.rst b/Misc/NEWS.d/next/Documentation/2026-08-10-18-13-42.gh-issue-77748.aVLelN.rst deleted file mode 100644 index 769007eb718d78..00000000000000 --- a/Misc/NEWS.d/next/Documentation/2026-08-10-18-13-42.gh-issue-77748.aVLelN.rst +++ /dev/null @@ -1 +0,0 @@ -Document that float() accepts bytes-like objects. From 1e5761361cf24f494756c60773b5008a2ec228af Mon Sep 17 00:00:00 2001 From: Qanty_ <89341544+Qanty228@users.noreply.github.com> Date: Tue, 11 Aug 2026 19:24:11 +0300 Subject: [PATCH 07/11] document that float() accepts bytes-like objects (rst version) --- Doc/library/functions.rst | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Doc/library/functions.rst b/Doc/library/functions.rst index f45ab397e93693..97d942871d11c4 100644 --- a/Doc/library/functions.rst +++ b/Doc/library/functions.rst @@ -778,7 +778,7 @@ are always available. They are listed here in alphabetical order. single: NaN single: Infinity - Return a floating-point number constructed from a number or a string. + Return a floating-point number constructed from a string, number, or bytes-like object, if possible. Examples: @@ -794,12 +794,21 @@ are always available. They are listed here in alphabetical order. 1000000.0 >>> float('-Infinity') -inf + >>> float(bytearray(b'-2.5')) + -2.5 + >>> float(array.array('b', [49, 50, 51])) + 123.0 If the argument is a string, it should contain a decimal number, optionally preceded by a sign, and optionally embedded in whitespace. The optional sign may be ``'+'`` or ``'-'``; a ``'+'`` sign has no effect on the value produced. The argument may also be a string representing a NaN (not-a-number), or positive or negative infinity. + + If the argument is a bytes-like object, its contents are parsed as a decimal + string. Therefore, the bytes-like object must contain a valid representation + of a floating-point number, otherwise :exc:`ValueError` is raised. + More precisely, the input must conform to the :token:`~float:floatvalue` production rule in the following grammar, after leading and trailing whitespace characters are removed: From ea75d713813abaf380bc376ca7e437cd824b5030 Mon Sep 17 00:00:00 2001 From: Qanty_ <89341544+Qanty228@users.noreply.github.com> Date: Tue, 11 Aug 2026 19:29:20 +0300 Subject: [PATCH 08/11] functions.rst trailing whitespace fix --- Doc/library/functions.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/functions.rst b/Doc/library/functions.rst index 97d942871d11c4..2cd57d7b1fb2c2 100644 --- a/Doc/library/functions.rst +++ b/Doc/library/functions.rst @@ -806,7 +806,7 @@ are always available. They are listed here in alphabetical order. (not-a-number), or positive or negative infinity. If the argument is a bytes-like object, its contents are parsed as a decimal - string. Therefore, the bytes-like object must contain a valid representation + string. Therefore, the bytes-like object must contain a valid representation of a floating-point number, otherwise :exc:`ValueError` is raised. More precisely, the input must conform to the :token:`~float:floatvalue` From 4844e944d22c5a57f23ab8ea6c923318118f067e Mon Sep 17 00:00:00 2001 From: Qanty_ <89341544+Qanty228@users.noreply.github.com> Date: Tue, 11 Aug 2026 19:46:30 +0300 Subject: [PATCH 09/11] import array functions.rst float fix --- Doc/library/functions.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/Doc/library/functions.rst b/Doc/library/functions.rst index 2cd57d7b1fb2c2..b4924e24addb41 100644 --- a/Doc/library/functions.rst +++ b/Doc/library/functions.rst @@ -796,6 +796,7 @@ are always available. They are listed here in alphabetical order. -inf >>> float(bytearray(b'-2.5')) -2.5 + >>> import array >>> float(array.array('b', [49, 50, 51])) 123.0 From e09a351877e4b2ffc6a017b4f915ee9a9c0af539 Mon Sep 17 00:00:00 2001 From: Qanty_ <89341544+Qanty228@users.noreply.github.com> Date: Tue, 11 Aug 2026 21:43:54 +0300 Subject: [PATCH 10/11] add more examples of float conversions --- Doc/library/functions.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Doc/library/functions.rst b/Doc/library/functions.rst index b4924e24addb41..71217e4e7059c4 100644 --- a/Doc/library/functions.rst +++ b/Doc/library/functions.rst @@ -794,8 +794,12 @@ are always available. They are listed here in alphabetical order. 1000000.0 >>> float('-Infinity') -inf + >>> float(b'3.2e2') + 320.0 >>> float(bytearray(b'-2.5')) -2.5 + >>> float(memoryview(b'6.7')) + 6.7 >>> import array >>> float(array.array('b', [49, 50, 51])) 123.0 From 854a77e089971769e5a085cb9054dff95b8532e3 Mon Sep 17 00:00:00 2001 From: Qanty_ <89341544+Qanty228@users.noreply.github.com> Date: Tue, 11 Aug 2026 23:07:40 +0300 Subject: [PATCH 11/11] Delete NEWS entry --- .../Documentation/2026-08-10-18-13-43.gh-issue-77748.aVLelN.rst | 1 - 1 file changed, 1 deletion(-) delete mode 100644 Misc/NEWS.d/next/Documentation/2026-08-10-18-13-43.gh-issue-77748.aVLelN.rst diff --git a/Misc/NEWS.d/next/Documentation/2026-08-10-18-13-43.gh-issue-77748.aVLelN.rst b/Misc/NEWS.d/next/Documentation/2026-08-10-18-13-43.gh-issue-77748.aVLelN.rst deleted file mode 100644 index 769007eb718d78..00000000000000 --- a/Misc/NEWS.d/next/Documentation/2026-08-10-18-13-43.gh-issue-77748.aVLelN.rst +++ /dev/null @@ -1 +0,0 @@ -Document that float() accepts bytes-like objects.