Skip to content

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

Closed

Description

@sobolevn
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 = []

    Activity

    1. sobolevn commented on Aug 11, 2021

      @sobolevn
      MemberAuthor

      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.

    2. added
      testsTests in the Lib/test dir
      type-featureA feature request or enhancement
      on Aug 11, 2021
    3. sweeneyde commented on Aug 12, 2021

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

    4. ambv commented on Aug 13, 2021

      @ambv
      Contributor

      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.

    5. ambv commented on Aug 13, 2021

      @ambv
      Contributor

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

    6. miss-islington commented on Aug 13, 2021

      @miss-islington
      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

    7. ambv commented on Aug 13, 2021

      @ambv
      Contributor

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

    8. transferred this issue fromon Apr 10, 2022
    9. mattip commented on Feb 14, 2023

      @mattip
      Contributor

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

    10. sobolevn commented on Feb 14, 2023

      @sobolevn
      MemberAuthor

      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.

    11. mattip commented on Feb 14, 2023

      @mattip
      Contributor

      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

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      testsTests in the Lib/test dirtype-featureA feature request or enhancement

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions