GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — mock raises exception when using a spec with an attribute that raises exception on access · Issue #89917 · python/cpython

mock raises exception when using a spec with an attribute that raises exception on access #89917

New issue
Copy link
New issue
Copy link
Closed as not planned
Closed as not planned
mock raises exception when using a spec with an attribute that raises exception on access#89917
Copy link
Labels
3.10 (EOL)end of lifeend of life3.11only security fixesonly security fixes3.8 (EOL)end of lifeend of life3.9 (EOL)end of lifeend of lifetype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

kjamieson
mannequin
opened on Nov 8, 2021
Issue body actions
BPO 45756
Nosy @ambv, @hongweipeng, @tirkarthi, @sobolevn, @jaap3
PRs
  • bpo-45756: do not execute @property descrs while creating mock autospecs #29901
  • bpo-45756: Fix mock triggers dynamic lookup via the descriptor protocol. #31348
  • 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 2021-11-08.19:47:53.494>
    labels = ['type-bug', '3.8', '3.9', '3.10', '3.11']
    title = 'mock raises exception when using a spec with an attribute that raises exception on access'
    updated_at = <Date 2022-02-15.06:36:26.841>
    user = 'https://bugs.python.org/kjamieson'

    bugs.python.org fields:

    activity = <Date 2022-02-15.06:36:26.841>
    actor = 'hongweipeng'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = []
    creation = <Date 2021-11-08.19:47:53.494>
    creator = 'kjamieson'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 45756
    keywords = ['patch']
    message_count = 3.0
    messages = ['405982', '406284', '406452']
    nosy_count = 6.0
    nosy_names = ['lukasz.langa', 'hongweipeng', 'xtreak', 'sobolevn', 'kjamieson', 'jaap3']
    pr_nums = ['29901', '31348']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue45756'
    versions = ['Python 3.8', 'Python 3.9', 'Python 3.10', 'Python 3.11']

    Activity

    1. kjamieson commented on Nov 8, 2021

      @kjamieson
      kjamiesonmannequin
      on Nov 8, 2021
      MannequinAuthor
      More actions

      In Python 3.8 and later creating a mock with a spec specifying an object containing a property that happens to raise an exception when accessed will fail, because _mock_add_spec calls getattr() on every attribute of the spec. This did not happen in Python 3.6/3.7.

      This is likely a fairly unusual scenario (and in the particular case where I encountered this I could just use a class instead of an instance for the spec), but it was surprising.

      For example:

      # cat test.py
      from unittest import mock
      
      class Foo:
          @property
          def bar(self) -> str:
              raise Exception('xxx')
      
      m = mock.MagicMock(spec=Foo())
      # python3.11 test.py
      Traceback (most recent call last):
        File "/root/test.py", line 8, in <module>
          m = mock.MagicMock(spec=Foo())
              ^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/usr/lib/python3.11/unittest/mock.py", line 2069, in __init__
          _safe_super(MagicMixin, self).__init__(*args, **kw)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/usr/lib/python3.11/unittest/mock.py", line 1087, in __init__
          _safe_super(CallableMixin, self).__init__(
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/usr/lib/python3.11/unittest/mock.py", line 442, in __init__
          self._mock_add_spec(spec, spec_set, _spec_as_instance, _eat_self)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/usr/lib/python3.11/unittest/mock.py", line 497, in _mock_add_spec
          if iscoroutinefunction(getattr(spec, attr, None)):
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/root/test.py", line 6, in bar
          raise Exception('xxx')
          ^^^^^^^^^^^^^^^^^^^^^^
      Exception: xxx
    2. kjamieson
      added
      3.8 (EOL)end of lifeend of life
      3.9 (EOL)end of lifeend of life
      3.10 (EOL)end of lifeend of life
      3.11only security fixesonly security fixes
      type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
      on Nov 8, 2021
    3. jaap3 commented on Nov 13, 2021

      @jaap3
      jaap3mannequin
      on Nov 13, 2021
      Mannequin
      More actions

      I think I encountered this when trying to mock requests.Response: psf/requests#5944

    4. tirkarthi commented on Nov 17, 2021

      @tirkarthi
      tirkarthi
      on Nov 17, 2021
      Member
      More actions

      This seems to be similar to https://bugs.python.org/issue41768 .

    5. ezio-melotti
      transferred this issue fromon Apr 10, 2022
    6. serhiy-storchaka
      added this to Unittest & doctest issueson May 20, 2022
    7. carljm commented on May 5, 2023

      @carljm
      carljm
      on May 5, 2023
      Member
      More actions

      Closing as duplicate of #85934

    8. carljm
      closed this as not plannedon May 5, 2023
    9. github-project-automation
      moved this to Done in Unittest & doctest issueson May 5, 2023
    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.10 (EOL)end of lifeend of life3.11only security fixesonly security fixes3.8 (EOL)end of lifeend of life3.9 (EOL)end of lifeend of lifetype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

      Projects

      • Unittest & doctest issues
        Status
        Done
        Show more project fields

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.