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

fix(serializer): Make sentry_repr dunder method to avoid mock problems #1364

Merged
merged 5 commits into from
Mar 8, 2022

Conversation

sl0thentr0py
Copy link
Member

@sl0thentr0py sl0thentr0py commented Mar 7, 2022

The addition of sentry_repr in #1322 breaks MagicMock since it creates attrs on the fly.
This converts it to a dunder magic method __sentry_repr__.

See here that mock doesn't mess with custom magics and the following test code:

In [22]: from unittest import mock

In [23]: m = mock.Mock()

In [24]: m
Out[24]: <Mock id='140712474488560'>

In [25]: type(m)
Out[25]: unittest.mock.Mock

In [26]: getattr(m, "foo")
Out[26]: <Mock name='mock.foo' id='140712490825808'>

In [27]: getattr(m, "__foo__")
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [27], in <module>
----> 1 getattr(m, "__foo__")

File /usr/lib/python3.10/unittest/mock.py:636, in NonCallableMock.__getattr__(self, name)
    634         raise AttributeError("Mock object has no attribute %r" % name)
    635 elif _is_magic(name):
--> 636     raise AttributeError(name)
    637 if not self._mock_unsafe:
    638     if name.startswith(('assert', 'assret', 'asert', 'aseert', 'assrt')):

AttributeError: __foo__

@sl0thentr0py sl0thentr0py merged commit a14c127 into master Mar 8, 2022
@sl0thentr0py sl0thentr0py deleted the neel/fix-sentry-repr-magicv branch March 8, 2022 10:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants