# Bug in int('+␣42')?

**URL:** <https://discuss.python.org/t/bug-in-int-42/26360>\
**Category:** Core Development\
**Created:** [April 30, 2023, 7:39pm UTC](https://discuss.python.org/t/bug-in-int-42/26360 "2023-04-30T19:39:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![OPi](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/opi/32/11948_2.png) [@OPi](https://discuss.python.org/u/OPi)\
**Post date:** [April 30, 2023, 7:39pm UTC](https://discuss.python.org/t/bug-in-int-42/26360/1 "2023-04-30T19:39:03Z")

</div>

The documentation of **int(x)** and **int(x, base=10)** specifies:  
_“If x is not a number or if base is given, then x must be a string, bytes, or bytearray instance representing an integer in radix base. Optionally, the string can be preceded by **+ or - (with no space in between)**, have leading zeros, be surrounded by whitespace, and have single underscores interspersed between digits.”_  
[https://docs.python.org/3/library/functions.html?#int](https://docs.python.org/3/library/functions.html?#int)  
The Python 2 documentation is similar:  
[https://docs.python.org/2.7/library/functions.html?#int](https://docs.python.org/2.7/library/functions.html?#int)

**`int('+␣42')` is valid or not?**  
The Python 3 implementation (at least from 3.5.9 to 3.11.0) reject spaces between the sign and the integer. That is the expected behaviour by the documentation.  
**But spaces between the sign and the integer are valid for Pypy 3.7, Python 2.7, Pypy 2.7 and Jython 2.7!**

Is it really a bug or is there no really a precise specification and it is implementation dependent?  
I expected that implementations raise a ValuerError for these kind of values.

`float('+␣42')` is rejected by all implementations.

The **expected** behavior, by Python 3:

```auto
Python 3.9.2 (default, Feb 28 2021, 17:03:44)
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> int('+ 42')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '+ 42'
>>> int('- 42')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '- 42'
>>> float('+ 42')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: '+ 42'

```

See [https://gcc.godbolt.org/z/ETe731KPj](https://gcc.godbolt.org/z/ETe731KPj) to try other Python 3 versions.

The **unexpected** behavior, by Pypy 3, Python 2, Pypy 2 and Jython 2:

```auto
Python 3.7.10 (7.3.5+dfsg-2+deb11u2, Nov 01 2022, 20:16:36)
[PyPy 7.3.5 with GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>> int('+ 42')
42
>>>> int('- 42')
-42
>>>> float('+ 42')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: '+ 42'

```

```auto
Python 2.7.18 (default, Jul 14 2021, 08:11:37)
[GCC 10.2.1 20210110] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> int('+ 42')
42
>>> int('- 42')
-42
>>> float('+ 42')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: + 42

```

```auto
Python 2.7.18 (7.3.3+dfsg-2, May 02 2021, 15:10:01)
[PyPy 7.3.3 with GCC 10.2.1 20210110] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>> int('+ 42')
42
>>>> int('- 42')
-42
>>>> float('+ 42')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: '+ 42'

```

```auto
Jython 2.7.2-DEV (uncontrolled:+, Jan 3 1970, 22:46:05)
[OpenJDK 64-Bit Server VM (Debian)] on java11.0.18
Type "help", "copyright", "credits" or "license" for more information.
>>> int('+ 42')
42
>>> int('- 42')
-42
>>> float('+ 42')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for float: + 42

```

---

<div class="post-metadata">

**Author:** ![mattip](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/mattip/32/330_2.png) [@mattip](https://discuss.python.org/u/mattip)\
**Post date:** [April 30, 2023, 8:35pm UTC](https://discuss.python.org/t/bug-in-int-42/26360/2 "2023-04-30T20:35:14Z")

</div>

> Is it really a bug or is there no really a precise specification and it is implementation dependent?

It is a bug. While this forum is called “python” it is primarily CPython-oriented. Please open an issue on [the PyPy issue tracker](https://foss.heptapod.net/pypy/pypy/-/issues).

---

<div class="post-metadata">

**Author:** ![OPi](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/opi/32/11948_2.png) [@OPi](https://discuss.python.org/u/OPi)\
**Post date:** [April 30, 2023, 8:38pm UTC](https://discuss.python.org/t/bug-in-int-42/26360/3 "2023-04-30T20:38:19Z")

</div>

I will do that. But it is also a bug in CPython 2.  
I will also report for that.

---

<div class="post-metadata">

**Author:** ![hugovk](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/hugovk/32/14505_2.png) [@hugovk](https://discuss.python.org/u/hugovk)\
**Post date:** [April 30, 2023, 8:42pm UTC](https://discuss.python.org/t/bug-in-int-42/26360/4 "2023-04-30T20:42:58Z")

</div>

CPython 2 is no longer maintained so no need to report for that one.

> **[Status of Python Versions](https://devguide.python.org/versions/)**
>
> The main branch is currently the future Python 3.12, and is the only branch that accepts new features. The latest release for each Python version can be found on the download page. Python Release C...

---

<div class="post-metadata">

**Author:** ![OPi](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/opi/32/11948_2.png) [@OPi](https://discuss.python.org/u/OPi)\
**Post date:** [April 30, 2023, 9:15pm UTC](https://discuss.python.org/t/bug-in-int-42/26360/5 "2023-04-30T21:15:42Z")

</div>

That’s it:

> **[int('+␣42') is wrongly valid. It should raise a ValueError. (#3928) · Issues...](https://foss.heptapod.net/pypy/pypy/-/issues/3928)**
>
> The documentation of int(x) and int(x, base=10) specifies: "If x is not a number or if base is given, then x must be a string, bytes, or bytearray...

---

<div class="post-metadata">

**Author:** ![hauntsaninja](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/hauntsaninja/32/12451_2.png) [@hauntsaninja](https://discuss.python.org/u/hauntsaninja)\
**Post date:** [April 30, 2023, 10:26pm UTC](https://discuss.python.org/t/bug-in-int-42/26360/6 "2023-04-30T22:26:42Z")

</div>

Nice spot! Thanks for helping improve compatibility between PyPy and CPython!

Since I recently changed some of the documentation here in [https://github.com/python/cpython/pull/100436](https://github.com/python/cpython/pull/100436), I was curious how far back the “no space in between” wording goes. Looks like it was introduced in this commit: [#2580: int() docs revision. · python/cpython@225d3c8 · GitHub](https://github.com/python/cpython/commit/225d3c809cadfda94f4485ae10a30394dcb50f93)

---

<div class="post-metadata">

**Author:** ![tjreedy](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/tjreedy/32/137_2.png) [@tjreedy](https://discuss.python.org/u/tjreedy)\
**Post date:** [April 30, 2023, 11:06pm UTC](https://discuss.python.org/t/bug-in-int-42/26360/7 "2023-04-30T23:06:59Z")

</div>

Does CPython int tests have one to test that `int('+ 1')` raises ValueError? It should so that other implementations that do not fail this input will fail the test.

---

<div class="post-metadata">

**Author:** ![hauntsaninja](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/hauntsaninja/32/12451_2.png) [@hauntsaninja](https://discuss.python.org/u/hauntsaninja)\
**Post date:** [April 30, 2023, 11:24pm UTC](https://discuss.python.org/t/bug-in-int-42/26360/8 "2023-04-30T23:24:35Z")

</div>

Great point! I think this and a couple other tests were missing, I’ve opened [Improve int test coverage by hauntsaninja · Pull Request #104024 · python/cpython · GitHub](https://github.com/python/cpython/pull/104024)
