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

slice not hashable #84783

Closed
WillBradshaw mannequin opened this issue May 12, 2020 · 12 comments
Closed

slice not hashable #84783

WillBradshaw mannequin opened this issue May 12, 2020 · 12 comments
Assignees
Labels
3.12 bugs and security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) release-blocker type-feature A feature request or enhancement

Comments

@WillBradshaw
Copy link
Mannequin

WillBradshaw mannequin commented May 12, 2020

BPO 40603
Nosy @rhettinger, @stevendaprano, @remilapeyre, @isdanni
Files
  • patches.zip: patch to add slicing functionality to slices
  • 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 = None
    created_at = <Date 2020-05-12.01:23:39.253>
    labels = ['interpreter-core', 'easy', 'type-feature', '3.9']
    title = 'slice not hashable'
    updated_at = <Date 2020-05-12.08:34:03.335>
    user = 'https://bugs.python.org/WillBradshaw'

    bugs.python.org fields:

    activity = <Date 2020-05-12.08:34:03.335>
    actor = 'remi.lapeyre'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Interpreter Core']
    creation = <Date 2020-05-12.01:23:39.253>
    creator = 'Will Bradshaw'
    dependencies = []
    files = ['49149']
    hgrepos = []
    issue_num = 40603
    keywords = ['easy (C)']
    message_count = 5.0
    messages = ['368693', '368694', '368696', '368700', '368708']
    nosy_count = 5.0
    nosy_names = ['rhettinger', 'steven.daprano', 'remi.lapeyre', 'isdanni', 'Will Bradshaw']
    pr_nums = []
    priority = 'normal'
    resolution = None
    stage = None
    status = 'open'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue40603'
    versions = ['Python 3.9']

    Linked PRs

    @WillBradshaw
    Copy link
    Mannequin Author

    WillBradshaw mannequin commented May 12, 2020

    slice cannot be hashed which make some operations significantly more annoying. see https://groups.google.com/forum/#!topic/comp.lang.python/SvhkWwSDeIw

    @WillBradshaw WillBradshaw mannequin added type-bug An unexpected behavior, bug, or error topic-ctypes 3.9 only security fixes labels May 12, 2020
    @stevendaprano
    Copy link
    Member

    Please re-upload the patch file as an uncompressed text file, as it is quite difficult for many people to view zip files in their browser.

    @stevendaprano stevendaprano changed the title slice does not slice slice not hashable May 12, 2020
    @stevendaprano stevendaprano changed the title slice does not slice slice not hashable May 12, 2020
    @stevendaprano stevendaprano added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement and removed topic-ctypes type-bug An unexpected behavior, bug, or error labels May 12, 2020
    @rhettinger
    Copy link
    Contributor

    This is a reasonable use case.

    +1 for making slice() hashable.

    Will, you're welcome to submit a PR. If not, I'm sure someone else would be happy to scoop this up :-)

    @isdanni
    Copy link
    Mannequin

    isdanni mannequin commented May 12, 2020

    Would be happy to help with this. Sent a PR soon ;)

    @remilapeyre
    Copy link
    Mannequin

    remilapeyre mannequin commented May 12, 2020

    I think slices were explicitly made not hashable to avoid issues to avoid issues with dictionaries, see discussion at https://mail.python.org/pipermail/python-list/2001-March/076101.html and bpo-408326.

    The commit that did this is a1351fb

    Is this not needed anymore? Wouldn't this need to be discussed on python-ideas?

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    @rhettinger
    Copy link
    Contributor

    rhettinger commented Feb 18, 2023

    Guido, what are your thoughts on this proposal?

    On the one hand, the OP has a legitimate use case:

    I have an object that presents itself as a multidimensional array in the numpy style but is computes it's values on _getitem_ calls as it represents an infinite field of numbers. However this operation is expensive. In many cases the same slice of the field will be needed repeatedly. This lends itself to using an lru cache on _getitem_() however this does not work as slices cannot be stored in the dictionary key used for the lru_cache.

    On the other hand, slice literals for mappings are a mixed bag, possibly useful in some cases, possibly surprising in others:

    >>> d = {'hello': 1, 'world': 2}
    >>> d['hello': 'world']
    Traceback:
       ...
    KeyError:  slice('hello', 'world', None)  
    

    I like the idea of being able to cache array or sequence slices but worry about opening a can of worms for the general case.

    @rhettinger rhettinger added 3.12 bugs and security fixes and removed easy 3.9 only security fixes labels Feb 18, 2023
    @gvanrossum
    Copy link
    Member

    I think this is fine. The "danger" of d[a:b] seems overstated, it produces a clear KeyError.

    @rhettinger
    Copy link
    Contributor

    @gvanrossum Thanks for the looking at this.

    @gvanrossum
    Copy link
    Member

    Note that this still needs a “what’s new” entry, since it is a new feature and might even trip some folks up (see the test changes needed).

    carljm added a commit to carljm/cpython that referenced this issue Feb 20, 2023
    * main: (60 commits)
      pythongh-102056: Fix a few bugs in error handling of exception printing code (python#102078)
      pythongh-102011: use sys.exception() instead of sys.exc_info() in docs where possible (python#102012)
      pythongh-101566: Sync with zipp 3.14. (pythonGH-102018)
      pythonGH-99818: improve the documentation for zipfile.Path and Traversable (pythonGH-101589)
      pythongh-88233: zipfile: handle extras after a zip64 extra (pythonGH-96161)
      pythongh-101981: Apply HOMEBREW related environment variables (pythongh-102074)
      pythongh-101907: Stop using `_Py_OPCODE` and `_Py_OPARG` macros (pythonGH-101912)
      pythongh-101819: Adapt _io types to heap types, batch 1 (pythonGH-101949)
      pythongh-101981: Build macOS as recommended by the devguide (pythonGH-102070)
      pythongh-97786: Fix compiler warnings in pytime.c (python#101826)
      pythongh-101578: Amend PyErr_{Set,Get}RaisedException docs (python#101962)
      Misc improvements to the float tutorial (pythonGH-102052)
      pythongh-85417: Clarify behaviour on branch cuts in cmath module (python#102046)
      pythongh-100425: Update tutorial docs related to sum() accuracy (FH-101854)
      Add missing 'is' to `cmath.log()` docstring (python#102049)
      pythongh-100210: Correct the comment link for unescaping HTML (python#100212)
      pythongh-97930: Also include subdirectory in makefile. (python#102030)
      pythongh-99735: Use required=True in argparse subparsers example (python#100927)
      Fix incorrectly documented attribute in csv docs (python#101250)
      pythonGH-84783: Make the slice object hashable (pythonGH-101264)
      ...
    carljm added a commit to carljm/cpython that referenced this issue Feb 22, 2023
    * main: (225 commits)
      pythongh-102056: Fix a few bugs in error handling of exception printing code (python#102078)
      pythongh-102011: use sys.exception() instead of sys.exc_info() in docs where possible (python#102012)
      pythongh-101566: Sync with zipp 3.14. (pythonGH-102018)
      pythonGH-99818: improve the documentation for zipfile.Path and Traversable (pythonGH-101589)
      pythongh-88233: zipfile: handle extras after a zip64 extra (pythonGH-96161)
      pythongh-101981: Apply HOMEBREW related environment variables (pythongh-102074)
      pythongh-101907: Stop using `_Py_OPCODE` and `_Py_OPARG` macros (pythonGH-101912)
      pythongh-101819: Adapt _io types to heap types, batch 1 (pythonGH-101949)
      pythongh-101981: Build macOS as recommended by the devguide (pythonGH-102070)
      pythongh-97786: Fix compiler warnings in pytime.c (python#101826)
      pythongh-101578: Amend PyErr_{Set,Get}RaisedException docs (python#101962)
      Misc improvements to the float tutorial (pythonGH-102052)
      pythongh-85417: Clarify behaviour on branch cuts in cmath module (python#102046)
      pythongh-100425: Update tutorial docs related to sum() accuracy (FH-101854)
      Add missing 'is' to `cmath.log()` docstring (python#102049)
      pythongh-100210: Correct the comment link for unescaping HTML (python#100212)
      pythongh-97930: Also include subdirectory in makefile. (python#102030)
      pythongh-99735: Use required=True in argparse subparsers example (python#100927)
      Fix incorrectly documented attribute in csv docs (python#101250)
      pythonGH-84783: Make the slice object hashable (pythonGH-101264)
      ...
    @Yhg1s
    Copy link
    Member

    Yhg1s commented Mar 9, 2023

    GH-102548 adds the missing What's New entry.

    @hauntsaninja
    Copy link
    Contributor

    What's New was merged, so I think this can be closed

    @carljm carljm closed this as completed Mar 13, 2023
    terryjreedy added a commit that referenced this issue Mar 31, 2023
    Will Bradshaw contributed original patch on bpo-40603.
    ---------
    
    Co-authored-by: Terry Jan Reedy <[email protected]>
    warsaw pushed a commit to warsaw/cpython that referenced this issue Apr 11, 2023
    …ble) (python#103146)
    
    Will Bradshaw contributed original patch on bpo-40603.
    ---------
    
    Co-authored-by: Terry Jan Reedy <[email protected]>
    @asmeurer
    Copy link

    asmeurer commented Jul 16, 2023

    Thank you for fixing this.

    For anyone who was wanting hashable slices, my library ndindex may be of interest. It has a hashable Slice object but it also does a lot more like slice arithmetic which may be useful for a lot of the use cases described here.

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.12 bugs and security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) release-blocker type-feature A feature request or enhancement
    Projects
    Development

    No branches or pull requests

    7 participants