Skip to content

fix: Use unittest.mock.patch.object to mock attributes#2144

Merged
matthewfeickert merged 1 commit intomainfrom
fix/fix-patching-of-_setup-attribute
Mar 23, 2023
Merged

fix: Use unittest.mock.patch.object to mock attributes#2144
matthewfeickert merged 1 commit intomainfrom
fix/fix-patching-of-_setup-attribute

Conversation

@matthewfeickert
Copy link
Copy Markdown
Member

@matthewfeickert matthewfeickert commented Mar 23, 2023

Description

Resolves #2143

In Python 3.11 mocking of attributes through unittest.mock.patch will fail with an AttributeError. To avoid this, use unittest.mock.patch.object to mock the _setup attribute of a pyhf.tensorlib object.

c.f. https://docs.python.org/3.11/library/unittest.mock.html#the-mock-class footnote:

The only exceptions are magic methods and attributes (those that have leading and trailing double underscores). Mock doesn’t create these but instead raises an AttributeError. This is because the interpreter will often implicitly request these methods, and gets very confused to get a new Mock object when it expects a magic method

Checklist Before Requesting Reviewer

  • Tests are passing
  • "WIP" removed from the title of the pull request
  • Selected an Assignee for the PR to be responsible for the log summary

Before Merging

For the PR Assignees:

  • Summarize commit messages into a comprehensive review of the PR
* In Python 3.11 mocking of attributes through unittest.mock.patch will
  fail with an AttributeError. To avoid this, use unittest.mock.patch.object
  to mock the "_setup" attribute of a pyhf.tensorlib object.
   - c.f. https://docs.python.org/3.11/library/unittest.mock.html#the-mock-class
     footnote:

     > The only exceptions are magic methods and attributes (those that have
     > leading and trailing double underscores). Mock doesn’t create these but
     > instead raises an AttributeError. This is because the interpreter will
     > often implicitly request these methods, and gets very confused to get a
     > new Mock object when it expects a magic method.

* In Python 3.11 mocking of attributes through unittest.mock.patch will
  fail with an AttributeError. To avoid this, use unittest.mock.patch.object
  to mock the "_setup" attribute of a pyhf.tensorlib object.
   - c.f. https://docs.python.org/3.11/library/unittest.mock.html#the-mock-class
     footnote:

     > The only exceptions are magic methods and attributes (those that have
     > leading and trailing double underscores). Mock doesn’t create these but
     > instead raises an AttributeError. This is because the interpreter will
     > often implicitly request these methods, and gets very confused to get a
     > new Mock object when it expects a magic method.
@matthewfeickert matthewfeickert added tests pytest fix A bug fix labels Mar 23, 2023
@matthewfeickert matthewfeickert self-assigned this Mar 23, 2023
@codecov
Copy link
Copy Markdown

codecov bot commented Mar 23, 2023

Codecov Report

Patch and project coverage have no change.

Comparison is base (e7e6961) 98.30% compared to head (84dc92a) 98.30%.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2144   +/-   ##
=======================================
  Coverage   98.30%   98.30%           
=======================================
  Files          69       69           
  Lines        4531     4531           
  Branches      645      645           
=======================================
  Hits         4454     4454           
  Misses         45       45           
  Partials       32       32           
Flag Coverage Δ
contrib 97.88% <ø> (ø)
doctest 61.15% <ø> (ø)
unittests-3.10 96.31% <ø> (ø)
unittests-3.8 96.33% <ø> (ø)
unittests-3.9 96.35% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@matthewfeickert matthewfeickert merged commit c56a327 into main Mar 23, 2023
@matthewfeickert matthewfeickert deleted the fix/fix-patching-of-_setup-attribute branch March 23, 2023 15:19
matthewfeickert added a commit that referenced this pull request Mar 23, 2023
* In Python 3.11 mocking of attributes through unittest.mock.patch will
  fail with an AttributeError. To avoid this, use unittest.mock.patch.object
  to mock the "_setup" attribute of a pyhf.tensorlib object.
   - c.f. https://docs.python.org/3.11/library/unittest.mock.html#the-mock-class
     footnote:

     > The only exceptions are magic methods and attributes (those that have
     > leading and trailing double underscores). Mock doesn’t create these but
     > instead raises an AttributeError. This is because the interpreter will
     > often implicitly request these methods, and gets very confused to get a
     > new Mock object when it expects a magic method.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix A bug fix tests pytest

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

mocker.patch fails to find tensorlib _setup attribute for Python 3.11

2 participants