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

unittest.mock.create_autospec() can crash when accessing properties from objects #98301

Closed
joaoe opened this issue Oct 15, 2022 · 3 comments
Closed
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@joaoe
Copy link

joaoe commented Oct 15, 2022

Bug report

This crashes. It should not.

from unittest.mock import create_autospec

class X:
    @property
    def myprop(self):
        raise Exception("some bug")

create_autospec(X())

Now you could tell for me to fix X. The problem though is that in my project the problem is much more complex. X is some other class that is being autospeced and the getter touches some mock objects it does not like.

Your environment

  • CPython versions tested on: 3.10
  • Operating system and architecture: Windows

Fix

--- a\unittest\mock.py
+++ b\unittest\mock.py
         for attr in dir(spec):
-            if iscoroutinefunction(getattr(spec, attr, None)):
-                _spec_asyncs.append(attr)
+            try::
+                value = getattr(spec, attr, None)
+            except:
+                pass
+            else:
+                if iscoroutinefunction(value):
+                    _spec_asyncs.append(attr)
@joaoe joaoe added the type-bug An unexpected behavior, bug, or error label Oct 15, 2022
@sobolevn
Copy link
Member

See #29901

@sobolevn
Copy link
Member

Right now property execution is "by design".

@carljm
Copy link
Member

carljm commented May 5, 2023

I don't think property execution is by design. Historically, properties were not executed by creation of a specced Mock. The execution of properties was introduced relatively recently (3.8) along with AsyncMock, not intentionally but as an accidental side effect of looking for async methods. This was a backwards incompatible change at the time, and reported reasonably promptly in #85934. Although we've failed to fix it promptly, it should still be fixed and restored to the long-term established behavior.

I don't think exception-catching is the right answer though; rather we should do something like #22209 to avoid executing the properties in the first place.

Closing as duplicate of #85934.

@carljm carljm closed this as not planned Won't fix, can't repro, duplicate, stale May 5, 2023
@arhadthedev arhadthedev added the stdlib Python modules in the Lib dir label May 5, 2023
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

4 participants