Skip to content

unittest.mock.patch does not work as a decorator on generator functions #88893

Open

Description

@gmjw
mannequin
BPO 44730
Nosy @gmjw
PRs
  • bpo-44730: Add handling of generator functions #27315
  • Files
  • example_patch_failure.py: Example of failure on simple patch of len
  • 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-07-23.23:38:41.484>
    labels = ['3.8', 'type-feature', 'library']
    title = 'unittest.mock.patch does not work as a decorator on generator functions'
    updated_at = <Date 2021-07-24.00:57:01.742>
    user = 'https://github.com/gmjw'

    bugs.python.org fields:

    activity = <Date 2021-07-24.00:57:01.742>
    actor = 'garethmjwilliams'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2021-07-23.23:38:41.484>
    creator = 'garethmjwilliams'
    dependencies = []
    files = ['50176']
    hgrepos = []
    issue_num = 44730
    keywords = ['patch']
    message_count = 1.0
    messages = ['398104']
    nosy_count = 1.0
    nosy_names = ['garethmjwilliams']
    pr_nums = ['27315']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue44730'
    versions = ['Python 3.8']

    Activity

    1. gmjw commented on Jul 23, 2021

      gmjwmannequin
      MannequinAuthor

      unitest.mock.patch does not work well when applied as a decorator to a function which is a generator.

      It results in the function being changed from a generator function (co_flags 99) to a non-generator (co_flag 31) and the patch is not applied.

      [I have a MWE, attached, and fairly simple fix, to the file mock.py, which I will put up as a PR in due course. This is the first time I've submitted a bug or PR, so apologies if I've not done this particularly well.]

    2. added
      3.8 (EOL)
      stdlib
      type-feature
      on Jul 23, 2021
    3. 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)stdlibtype-feature

      Projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions