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

del expects __delitem__ if __setitem__ is defined #75401

Closed
raumzeitkeks mannequin opened this issue Aug 16, 2017 · 5 comments
Closed

del expects __delitem__ if __setitem__ is defined #75401

raumzeitkeks mannequin opened this issue Aug 16, 2017 · 5 comments
Labels
3.11 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@raumzeitkeks
Copy link
Mannequin

raumzeitkeks mannequin commented Aug 16, 2017

BPO 31218
Nosy @rhettinger, @bitdancer, @hongweipeng, @tirkarthi, @iritkatriel
Files
  • delitem_example.py: Minimal example demonstrating the differing behaviour
  • 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-07-08.19:38:45.031>
    created_at = <Date 2017-08-16.10:48:18.005>
    labels = ['interpreter-core', 'type-bug', '3.11']
    title = 'del expects __delitem__ if __setitem__ is defined'
    updated_at = <Date 2021-07-08.19:38:45.030>
    user = 'https://bugs.python.org/raumzeitkeks'

    bugs.python.org fields:

    activity = <Date 2021-07-08.19:38:45.030>
    actor = 'rhettinger'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-07-08.19:38:45.031>
    closer = 'rhettinger'
    components = ['Interpreter Core']
    creation = <Date 2017-08-16.10:48:18.005>
    creator = 'raumzeitkeks'
    dependencies = []
    files = ['47085']
    hgrepos = []
    issue_num = 31218
    keywords = []
    message_count = 5.0
    messages = ['300346', '300354', '327857', '397037', '397167']
    nosy_count = 6.0
    nosy_names = ['rhettinger', 'r.david.murray', 'raumzeitkeks', 'hongweipeng', 'xtreak', 'iritkatriel']
    pr_nums = []
    priority = 'normal'
    resolution = 'wont fix'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue31218'
    versions = ['Python 3.11']

    @raumzeitkeks
    Copy link
    Mannequin Author

    raumzeitkeks mannequin commented Aug 16, 2017

    I noticed some odd behaviour on classes defining __setitem__.
    Using del on a class defining __setitem__ but not __delitem__ results in "AttributeError: __delitem__".
    On classes definig neiter __setitem__ nor __delitem__ on the other hand this results in "TypeError: 'WithoutSetItem' object doesn't support item deletion".

    See the appended example script.

    @raumzeitkeks raumzeitkeks mannequin added the type-bug An unexpected behavior, bug, or error label Aug 16, 2017
    @bitdancer
    Copy link
    Member

    I'm not sure we would consider this a bug (the message is accurate), but I wouldn't object to fixing it, since that would indeed seem more consistent with how __delitem__ and del are defined in the language reference.

    @rhettinger
    Copy link
    Contributor

    This is likely an implementation artifact. In the abstract API, both PyObject_DelItem() and PyObject_SetItem() route through the same slot, m->mp_ass_subscript. The set and delete operations are only differentiated in the downstream concrete APIs. When the *value* parameter is NULL, the operation is deemed to be a deletion.

    @iritkatriel
    Copy link
    Member

    It is still the same in 3.11:

    >>> class WithoutSetItem:
    ...     def __getitem__(self, key):
    ...         return "foo"
    ...
    >>> class WithSetItem:
    ...     def __getitem__(self, key):
    ...         return "foo"
    ...     def __setitem__(self, key, val):
    ...         return
    ...
    >>> wo = WithoutSetItem()
    >>> del wo[0]
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: 'WithoutSetItem' object doesn't support item deletion
    >>> w = WithSetItem()
    >>> del w[0]
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: __delitem__
    >>>

    @iritkatriel iritkatriel added interpreter-core (Objects, Python, Grammar, and Parser dirs) 3.11 only security fixes labels Jul 6, 2021
    @rhettinger
    Copy link
    Contributor

    I'm closing this as not being worth changing. It is only a minor irritant and arguably not a bug. "Fixing it" would be disruptive and likely not help anyone.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.11 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants