classification
Title: Tests for `id(a) == id(a * 1)` for `bytes` and `str`
Type: enhancement Stage: resolved
Components: Tests Versions:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Dennis Sweeney, lukasz.langa, miss-islington, sobolevn
Priority: normal Keywords: patch

Created on 2021-08-11 20:01 by sobolevn, last changed 2021-08-13 11:06 by lukasz.langa. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 27745 merged sobolevn, 2021-08-12 16:44
PR 27756 merged miss-islington, 2021-08-13 10:36
Messages (6)
msg399414 - (view) Author: Nikita Sobolev (sobolevn) * Date: 2021-08-11 20:01
While working on `RustPython` (original issue: https://github.com/RustPython/RustPython/issues/2840), I've noticed that `tuple` in CPython has explicit tests that `id` does not change when multiplied by `1`, related:
- https://github.com/python/cpython/blob/64a7812c170f5d46ef16a1517afddc7cd92c5240/Lib/test/seq_tests.py#L322
- https://github.com/python/cpython/blob/64a7812c170f5d46ef16a1517afddc7cd92c5240/Lib/test/seq_tests.py#L286-L287

But, I cannot find similar tests for `str` and `bytes` which also have the same behavior: 
- `str`: https://github.com/python/cpython/blob/64a7812c170f5d46ef16a1517afddc7cd92c5240/Objects/unicodeobject.c#L12709-L12710
- `bytes`: https://github.com/python/cpython/blob/64a7812c170f5d46ef16a1517afddc7cd92c5240/Objects/bytesobject.c#L1456-L1458

Code:

```python
>>> b = b'abc'
>>> id(b), id(b * 1), id(b) == id(b * 1)
(4491073360, 4491073360, True)

>>> s = 'abc'
>>> id(s), id(s * 1), id(s) == id(s * 1)
(4489513776, 4489513776, True)
```

If tests are indeed missing and should be added, I would love to contribute them.
msg399484 - (view) Author: Dennis Sweeney (Dennis Sweeney) * (Python triager) Date: 2021-08-12 22:22
Perhaps it would be better to convert existing such tests to @cpython_only, since as far as I know, id() and `is` are implementation-defined for immutable objects.
msg399514 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-08-13 10:04
Dennis, there's an existing issue on making the test suite more pypy-compatible:

https://bugs.python.org/issue25130


In that issue Maciej says pypy adopted a policy to adjust tests when they have to so I'd say that unless we get a report on which additional tests should be marked as cpython_only, I wouldn't proactively do it myself.

In fact, a RustPython core dev is specifically asking for more tests here so I don't see why we shouldn't accommodate that.
msg399518 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-08-13 10:36
New changeset a2ce538e16d5e3a6168704366bdd7a8c5af29881 by Nikita Sobolev in branch 'main':
bpo-44891: Tests `id` preserving on `* 1` for `str` and `bytes` (GH-27745)
https://github.com/python/cpython/commit/a2ce538e16d5e3a6168704366bdd7a8c5af29881
msg399523 - (view) Author: miss-islington (miss-islington) Date: 2021-08-13 11:04
New changeset 45a97d91a42795fb0b220e67ee25a14b940c7e50 by Miss Islington (bot) in branch '3.10':
bpo-44891: Tests `id` preserving on `* 1` for `str` and `bytes` (GH-27745)
https://github.com/python/cpython/commit/45a97d91a42795fb0b220e67ee25a14b940c7e50
msg399524 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-08-13 11:06
I opted for landing this and backporting to 3.10 to increase visibility. There will be a NEWS entry about this so if any alt implementations have issues with this test, they can reach us and we'll adapt.

Thanks, Nikita! ✨ 🍰 ✨
History
Date User Action Args
2021-08-13 11:06:03lukasz.langasetstatus: open -> closed
resolution: fixed
messages: + msg399524

stage: patch review -> resolved
2021-08-13 11:04:12miss-islingtonsetmessages: + msg399523
2021-08-13 10:36:36lukasz.langasetmessages: + msg399518
2021-08-13 10:36:30miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request26232
2021-08-13 10:04:18lukasz.langasetnosy: + lukasz.langa
messages: + msg399514
2021-08-12 22:22:23Dennis Sweeneysetnosy: + Dennis Sweeney
messages: + msg399484
2021-08-12 16:44:38sobolevnsetkeywords: + patch
stage: patch review
pull_requests: + pull_request26223
2021-08-11 20:01:19sobolevncreate