Repository navigation
fix: don't route specced Mocks through str/dict tracing - #796
Closed
DawnofGenX wants to merge 1 commit into
Closed
DawnofGenX wants to merge 1 commit into
DawnofGenX wants to merge 1 commit into
Conversation
Hook tracing crashed with TypeError when a hook argument or first-result value was a specced unittest.mock.Mock (Mock(spec=str) or Mock(spec=dict)). Mock(spec=...) reports its spec through __class__, so the isinstance checks in _tracing.py matched the mock and sent it into the string formatting / dict metadata path. Use exact-type checks (type(obj) is str, type(args[-1]) is dict) so only real strings and dicts take those paths; a specced Mock now renders as an ordinary value and the hook result is left untouched. Adds a parametrized regression test covering both str and dict specs, and a changelog fragment.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem:
Enabling hook tracing makes a successful hook call raise
TypeErrorwhen anargument or first-result return value is a specced
unittest.mock.Mock.Mock(spec=str)→TypeError: object of type 'Mock' has no len()(before theimplementation runs).
Mock(spec=dict)→TypeError: 'Mock' object is not iterable(while tracing the result).Fix:
Mock(spec=...)reports its spec through__class__, so theisinstance(obj, str)and
isinstance(args[-1], dict)checks in_tracing.pymatched the mocks androuted them into the string-formatting / dict-metadata paths. Switched both to
exact-type checks (
type(obj) is str,type(args[-1]) is dict), so only realstrings and dicts take those paths and a specced Mock renders as an ordinary
value.
Verification:
test_tracing_specced_mock_does_not_raise(parametrized overstranddict): both cases fail withTypeErrorwithout the fix, pass with it.pre-commit run --all-files: all hooks pass.