Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests for id(a) == id(a * 1) for bytes and str #89054

Closed
sobolevn opened this issue Aug 11, 2021 · 9 comments
Closed

Tests for id(a) == id(a * 1) for bytes and str #89054

sobolevn opened this issue Aug 11, 2021 · 9 comments
Labels
tests Tests in the Lib/test dir type-feature A feature request or enhancement

Comments

@sobolevn
Copy link
Member

BPO 44891
Nosy @ambv, @miss-islington, @sweeneyde, @sobolevn
PRs
  • bpo-44891: Tests id preserving on * 1 for str and bytes #27745
  • [3.10] bpo-44891: Tests id preserving on * 1 for str and bytes (GH-27745) #27756
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2021-08-13.11:06:03.632>
    created_at = <Date 2021-08-11.20:01:19.832>
    labels = ['type-feature', 'tests']
    title = 'Tests for `id(a) == id(a * 1)` for `bytes` and `str`'
    updated_at = <Date 2021-08-13.11:06:03.631>
    user = 'https://github.com/sobolevn'

    bugs.python.org fields:

    activity = <Date 2021-08-13.11:06:03.631>
    actor = 'lukasz.langa'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-08-13.11:06:03.632>
    closer = 'lukasz.langa'
    components = ['Tests']
    creation = <Date 2021-08-11.20:01:19.832>
    creator = 'sobolevn'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 44891
    keywords = ['patch']
    message_count = 6.0
    messages = ['399414', '399484', '399514', '399518', '399523', '399524']
    nosy_count = 4.0
    nosy_names = ['lukasz.langa', 'miss-islington', 'Dennis Sweeney', 'sobolevn']
    pr_nums = ['27745', '27756']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue44891'
    versions = []

    @sobolevn
    Copy link
    Member Author

    While working on RustPython (original issue: RustPython/RustPython#2840), I've noticed that tuple in CPython has explicit tests that id does not change when multiplied by 1, related:

    But, I cannot find similar tests for str and bytes which also have the same behavior:

    Code:

    >>> 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.

    @sobolevn sobolevn added tests Tests in the Lib/test dir type-feature A feature request or enhancement labels Aug 11, 2021
    @sweeneyde
    Copy link
    Member

    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.

    @ambv
    Copy link
    Contributor

    ambv commented Aug 13, 2021

    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.

    @ambv
    Copy link
    Contributor

    ambv commented Aug 13, 2021

    New changeset a2ce538 by Nikita Sobolev in branch 'main':
    bpo-44891: Tests id preserving on * 1 for str and bytes (GH-27745)
    a2ce538

    @miss-islington
    Copy link
    Contributor

    New changeset 45a97d9 by Miss Islington (bot) in branch '3.10':
    bpo-44891: Tests id preserving on * 1 for str and bytes (GH-27745)
    45a97d9

    @ambv
    Copy link
    Contributor

    ambv commented Aug 13, 2021

    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! ✨ 🍰 ✨

    @ambv ambv closed this as completed Aug 13, 2021
    @ambv ambv closed this as completed Aug 13, 2021
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    @mattip
    Copy link
    Contributor

    mattip commented Feb 14, 2023

    Why don't subclasses of str and bytes preserve this property? Is that a missed optimization?

    @sobolevn
    Copy link
    Member Author

    sobolevn commented Feb 14, 2023

    Subclasses might be mutable:

    >>> class S(str):
    ...   def __init__(self, *args, **kwargs):
    ...     super().__init__(*args, **kwargs)
    ...     self.mutable = []
    ... 
    >>> s = S()
    >>> s.mutable.append(1)

    They might also change how multiplication works.

    @mattip
    Copy link
    Contributor

    mattip commented Feb 14, 2023

    Makes sense, thanks. I just now added a test and fix for a * 1 for PyPy. I think I should also make PyPy respect addition:

    a = "abc!"
    b = "abc!"
    assert a is not b
    assert a + "" is a  # currently fails for PyPy
    

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    tests Tests in the Lib/test dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    5 participants