python / cpython

The Python programming language
https://www.python.org
Other
62.35k stars 29.94k forks source link

Metaclasses interfere with __class_getitem__ #80173

Closed 4951fabd-1bd2-4941-b979-084cf05fde06 closed 5 years ago

4951fabd-1bd2-4941-b979-084cf05fde06 commented 5 years ago
BPO 35992
Nosy @gvanrossum, @ilevkivskyi, @miss-islington, @cdonovick
PRs
  • python/cpython#11857
  • python/cpython#11910
  • 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: ```python assignee = None closed_at = created_at = labels = ['interpreter-core', 'type-bug', '3.7', 'docs'] title = 'Metaclasses interfere with __class_getitem__' updated_at = user = 'https://github.com/cdonovick' ``` bugs.python.org fields: ```python activity = actor = 'levkivskyi' assignee = 'docs@python' closed = True closed_date = closer = 'levkivskyi' components = ['Documentation', 'Interpreter Core'] creation = creator = 'donovick' dependencies = [] files = [] hgrepos = [] issue_num = 35992 keywords = ['patch'] message_count = 4.0 messages = ['335497', '335521', '335790', '335791'] nosy_count = 5.0 nosy_names = ['gvanrossum', 'docs@python', 'levkivskyi', 'miss-islington', 'donovick'] pr_nums = ['11857', '11910'] priority = 'normal' resolution = 'fixed' stage = 'resolved' status = 'closed' superseder = None type = 'behavior' url = 'https://bugs.python.org/issue35992' versions = ['Python 3.7'] ```

    4951fabd-1bd2-4941-b979-084cf05fde06 commented 5 years ago

    OS: Debian testing

    python3 -VV: Python 3.7.2+ (default, Feb 2 2019, 14:31:48) [gcc 8.2.0]

    The following:

    class Meta(type): pass
    
    class X(metaclass=Meta):
        def __class_getitem__(cls, key):
            return key
    
    X[10]

    Results in

    TypeError: 'Meta' object does not support indexing

    However, PEP-560 specifically states that __class_getitem should be used as fall back for when a metaclass does not implement __getitem.

    ilevkivskyi commented 5 years ago

    Yes, I think this is a bug. Created a PR with a possible simple fix.

    ilevkivskyi commented 5 years ago

    New changeset ac28147e78c45a6217d348ce90ca5281d91f676f by Ivan Levkivskyi in branch 'master': bpo-35992: Use PySequence_GetItem only if sq_item is not NULL (GH-11857) https://github.com/python/cpython/commit/ac28147e78c45a6217d348ce90ca5281d91f676f

    miss-islington commented 5 years ago

    New changeset a7f929db605326da452fbdeebfe341afa9316d25 by Miss Islington (bot) in branch '3.7': bpo-35992: Use PySequence_GetItem only if sq_item is not NULL (GH-11857) https://github.com/python/cpython/commit/a7f929db605326da452fbdeebfe341afa9316d25