-
-
Notifications
You must be signed in to change notification settings - Fork 73
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
support graphing mocked methods #26
Conversation
Thank you for the PR! I'm likely to be a bit busy in the next couple of weeks, so I cannot promise a prompt review. I'd like to understand what kind of error using mocks causes in At first sight this looks like a change I'll want to include (after figuring out the CI failures). I'm not against adding a |
No rush on the PR. Thanks for your consideration of this. I've added a few more test cases and some more handling for unusual mock related cases. The tests should pass on all python versions. The first test wasn't a problem on python 3.x so I've only employed the I wasn't able to come up with an exact replica of what I was seeing with my app, which was that |
Appveyor tests failed because |
@mgedmin Hi, sorry to disappear. I don't really have a good idea about why this isn't working on appveyor and I'm not eager to set up some sort of windows vm on my mac. If you have any suggestions, I'd welcome them. Thanks. |
The reason why your commit didn't work is that my appveyor.yml runs If you make the |
@mgedmin Got it, thanks. I thought maybe mock wouldn't be available on python 3 since it is now part of unittest, but it appears to work. |
except AttributeError: | ||
result = repr(value)[:40] | ||
|
||
if _isinstance(result, str if sys.version_info[0] == 3 else basestring): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a compat redefinition of basestring = str
on Python 3 at the top of this file, so this can be simplified to if _isinstance(result, basestring)
.
The rest looks good to me. Can you help me come up with a changelog entry for this change? I imagine something like
but I would prefer to mention the specific exception type (is it a TypeError?). I still haven't seen a traceback |
@@ -6,6 +6,7 @@ commands = | |||
python tests.py {posargs} | |||
|
|||
[testenv:py] | |||
deps = mock |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tox -e py27
also fails for me (on Linux) because it lacks mock
.
For posterity: here's what the various errors look like if I apply the patch to the tests, but run them against unmodified objgraph.py:
|
I'll work on a change name, but I wanted to mention that both python 2 and python 3 have the behavior:
Interestingly, the tests all pass now if I revert |
I've added a test for mock.Mock(spec=...) that proves that we do need a different definition of |
@@ -65,6 +65,17 @@ | |||
iteritems = dict.items | |||
|
|||
|
|||
def _isinstance(object, classinfo): | |||
""" Return whether an object is an instance of a class or of a subclass | |||
thereof. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be on a single line without a space after the opening """
.
Thank you! Other than the minor docstring formatting issues, this looks good. (I haven't made up my mind if I prefer squashed commits or a true history of the changes. Either works fine.) |
I tried to trigger a failure with unpatched objgraph (so I can write a good changelog message), but failed. Here's what I tried:
No crash. Similarly, including plain Can you help me find a crashing example? |
This supports graphing unittest mock objects. Also, ensure that _short_repr always returns a string.
I've made the changes you suggested and squashed the commits. Here's an example that crashes:
I wish I could explain how I ended up with a unbound instance method with a name that happened to be a mock, but I'm afraid I can't. For your changelog, I'd say the theme of these changes is that, in the presence of mocks, "don't trust |
Thank you very much for your patience! |
I've released objgraph 3.0.0 with this fix. |
Excellent. Thank you for guiding me through getting the change right. |
I've found a problem using objgraph to graph the relationships involving methods mocked by the mock library. Specifically, when applied to a mocked instance method,
_safe_repr
returns a mock, rather than a string, which causes_obj_label
to fail. This is because the mock library delegates__class__
to the spec it is using and it doesn't returnmock.Mock
(or some variant of that). This PR usestype()
instead of__class__
to determine the class of an object.Assuming you're open to this PR, I'd like to include tests with this PR, but I need to know whether whether you'd be open to include the mock library as a dependency (it is a standard library in python 3 but not before). If not, I can write lower level tests, but they wouldn't automatically break if mock ever changes how it is implemented (which may or may not be desirable). Thanks for your consideration and maintaining such a useful tool.