Skip to content

Fix documentation for __instancecheck__ #79264

Open

Description

@Rhodolite
mannequin
BPO 35083
Nosy @JulienPalard, @tirkarthi, @Rhodolite, @cdonovick, @eirrgang

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 2018-10-27.20:14:41.592>
labels = ['type-feature', '3.8', 'docs']
title = 'Fix documentation for __instancecheck__'
updated_at = <Date 2020-12-15.13:52:24.473>
user = 'https://github.com/Rhodolite'

bugs.python.org fields:

activity = <Date 2020-12-15.13:52:24.473>
actor = 'eirrgang'
assignee = 'docs@python'
closed = False
closed_date = None
closer = None
components = ['Documentation']
creation = <Date 2018-10-27.20:14:41.592>
creator = 'joydiamond'
dependencies = []
files = []
hgrepos = []
issue_num = 35083
keywords = []
message_count = 5.0
messages = ['328658', '328687', '330286', '383046', '383057']
nosy_count = 6.0
nosy_names = ['docs@python', 'mdk', 'xtreak', 'joydiamond', 'donovick', 'eirrgang']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue35083'
versions = ['Python 2.7', 'Python 3.8']

Activity

  1. Rhodolite commented on Oct 27, 2018

    Rhodolitemannequin
    MannequinAuthor

    This is a request to fix the documentation for __instancecheck__.

    Please add the following:

    """
    (Note that any object x is always considered to be an instance of
    type(x), and this cannot be overridden.)
    """

    Consider the following program:

    class M(type):
        def __instancecheck__(m, t):
            print('instancecheck(%s, %s)' % (m, t))
            return False                                    #   LIE!
    
    Test = M('Test', ((object,)), {})
    
    something = Test()
    
    print('Does *NOT* call __instancecheck__:')
    print('isinstance(something, Test): %s' % isinstance(something, Test))
    
    print()
    print('Does call __instancecheck__:')
    print('isinstance(0, Test):         %s' % isinstance(0,         Test))

    Under python 2, python 3, and pypy, in all cases, the first examples does *NOT* call __instancecheck__.

    You can see the optimization here:

    https://github.com/python/cpython/blob/master/Objects/abstract.c#L2397-L2405

    Which reads:

    int
    PyObject_IsInstance(PyObject *inst, PyObject *cls)
    {
    _Py_IDENTIFIER(__instancecheck__);
    PyObject *checker;

    /* Quick test for an exact match */
    if (Py_TYPE(inst) == (PyTypeObject *)cls)
    return 1;

    I'm fine with the optimization -- I am not suggesting to get rid of it.

    I just want the documentation to match the actual implementation.

    The following documentation needs to be fixed:

    https://docs.python.org/2/reference/datamodel.html
    https://docs.python.org/3/reference/datamodel.html
    https://www.python.org/dev/peps/pep-3119/

    It took me half an hour to figure out why my version of __instancecheck__ was not working, as I tried to test it using the super simple code above ...

    One of the best things about python is how accurate and consistent the documentation is.

    This request is to keep these high standards.

  2. added
    docs
    type-feature
    on Oct 27, 2018
  3. tirkarthi commented on Oct 28, 2018

    @tirkarthi
    Member
  4. JulienPalard commented on Nov 22, 2018

    @JulienPalard
    Member

    Currently 1 there's no concensus about:

    • Is this a documentation issue and we should document the optimisation?
    • Is the optimisation over-zealeous?
  5. eirrgang commented on Dec 15, 2020

    eirrgangmannequin
    Mannequin

    The optimization appears to have been made without this level of discussion. The change to PEP-3119 was likely overlooked. The intended PEP-3119 behavior seems clear and reasonable.

    r61575 was a small part of the SVN merge that became git commit d5e2b6f

    Suggest reverting the /* Quick test for an exact match */.

    If either the optimization is important, or the documented PEP-3119 behavior should be revised, suggest re-raising the issue as a proposed revision to PEP-3119.

  6. eirrgang commented on Dec 15, 2020

    eirrgangmannequin
    Mannequin

    Actually, it looks like performance concerns were raised as issues 2303 and 2534. For bpo-2534, the issue was different.

    For issue bpo-2303, behavior-changing optimization was proposed. The first comment describes the cost of requiring a check to whether __instancecheck__ is present (though I don't see what the comment refers to in 1b7f891, which is the commit containing the cited r58099, as best I can tell). However, the discussion may have ended prematurely when the optimization https://bugs.python.org/file9742/isinst.diff slipped in through a different change.

    See https://bugs.python.org/issue2303 for discussion.

    For what it's worth, Guido (author of PEP-3119) did not seem to object to https://codereview.appspot.com/504/diff/1/Objects/abstract.c and no one seems to have discussed the apparent inconsistency with PEP-3119.

  7. transferred this issue fromon Apr 10, 2022
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

    3.8 (EOL)docstype-feature

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions