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

Mock autospec does not work with subclasses of property() #70937

Closed
amauryfa opened this issue Apr 13, 2016 · 7 comments
Closed

Mock autospec does not work with subclasses of property() #70937

amauryfa opened this issue Apr 13, 2016 · 7 comments
Assignees
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@amauryfa
Copy link
Member

BPO 26750
Nosy @gpshead, @amauryfa, @voidspace, @berkerpeksag
Files
  • mock-descriptor.patch
  • mock-descriptor-2.patch
  • 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 = 'https://github.com/berkerpeksag'
    closed_at = <Date 2016-08-07.16:07:15.175>
    created_at = <Date 2016-04-13.22:13:06.644>
    labels = ['type-bug', 'library']
    title = 'Mock autospec does not work with subclasses of property()'
    updated_at = <Date 2016-08-17.22:40:09.697>
    user = 'https://github.com/amauryfa'

    bugs.python.org fields:

    activity = <Date 2016-08-17.22:40:09.697>
    actor = 'gregory.p.smith'
    assignee = 'berker.peksag'
    closed = True
    closed_date = <Date 2016-08-07.16:07:15.175>
    closer = 'gregory.p.smith'
    components = ['Library (Lib)']
    creation = <Date 2016-04-13.22:13:06.644>
    creator = 'amaury.forgeotdarc'
    dependencies = []
    files = ['42458', '42891']
    hgrepos = []
    issue_num = 26750
    keywords = ['patch']
    message_count = 7.0
    messages = ['263361', '265827', '272122', '272124', '272215', '272842', '272997']
    nosy_count = 5.0
    nosy_names = ['gregory.p.smith', 'amaury.forgeotdarc', 'michael.foord', 'python-dev', 'berker.peksag']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue26750'
    versions = ['Python 3.5', 'Python 3.6']

    @amauryfa
    Copy link
    Member Author

    When patching a class, mock.create_autospec() correctly detects properties and __slot__ attributes, but not subclasses of property() or other kinds of data descriptors.

    The attached patch detects all data descriptors and patch them the way they should be.

    @amauryfa amauryfa added type-feature A feature request or enhancement tests Tests in the Lib/test dir labels Apr 13, 2016
    @berkerpeksag berkerpeksag added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error and removed tests Tests in the Lib/test dir type-feature A feature request or enhancement labels Apr 21, 2016
    @amauryfa
    Copy link
    Member Author

    Updated patch with review comments.

    @berkerpeksag berkerpeksag self-assigned this May 18, 2016
    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Aug 7, 2016

    New changeset e947248100ae by Gregory P. Smith in branch '3.5':
    Issue bpo-26750: unittest.mock.create_autospec() now works properly
    https://hg.python.org/cpython/rev/e947248100ae

    New changeset 0bc14c91ef7e by Gregory P. Smith in branch 'default':
    Issue bpo-26750: unittest.mock.create_autospec() now works properly for
    https://hg.python.org/cpython/rev/0bc14c91ef7e

    @gpshead gpshead closed this as completed Aug 7, 2016
    @berkerpeksag
    Copy link
    Member

    Thanks! Is there a reason not to use inspect.isdatadescriptor() instead of _is_data_descriptor()?

    @gpshead
    Copy link
    Member

    gpshead commented Aug 9, 2016

    Probably just Amaury and I forgetting that existed. Amaury, inspect.isdatadescriptor's implementation is a bit different than this change's _is_data_descriptor. Thoughts?

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Aug 16, 2016

    New changeset 3ff02380b1bf by Gregory P. Smith in branch '3.5':
    Issue bpo-26750: use inspect.isdatadescriptor instead of our own
    https://hg.python.org/cpython/rev/3ff02380b1bf

    New changeset d51a66622266 by Gregory P. Smith in branch 'default':
    Issue bpo-26750: use inspect.isdatadescriptor instead of our own
    https://hg.python.org/cpython/rev/d51a66622266

    @amauryfa
    Copy link
    Member Author

    inspect.isdatadescriptor() is better indeed.
    (I was initially working on an old version of mock.py which does not import inspect, and I did not want to add the dependency there).

    • inspect uses hasattr(type(obj)) instead of hasatr(obj). This is better, (but does not work for 2.x old-style classes)

    • my patch tested for __del__... this is completely wrong, it should have been __delete__. oops.
      inspect.isdatadescriptor() does not test for __delete__. This is insaccurate, but I doubt it will ever matter. This is only possible for Python-defined descriptors, the C implementation always exposes both __set__ and __delete__ when tp_set is filled.

    IOW, I'm happy with the current state.

    @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
    stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants