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

Yappi 3.12 AttributeError: _mock_methods when using MagicMock #172

Open
olejorgenb opened this issue May 22, 2024 · 3 comments
Open

Yappi 3.12 AttributeError: _mock_methods when using MagicMock #172

olejorgenb opened this issue May 22, 2024 · 3 comments

Comments

@olejorgenb
Copy link

My program fails when trying to profile using yappi through 3.12 (works fine with cProfile)

@esantorella
Copy link

I'm seeing this when I use yappi in combination with unittest.mock and Python 3.12. Removing any of those three elements fixes the issue. Here's a repro:

from unittest import mock
import yappi


if __name__ == "__main__":
    yappi.start()
    mock.MagicMock()
    yappi.stop()

Traceback:

Traceback (most recent call last):
  File "/Users/santorella/miniconda3/envs/ax_312_test/lib/python3.12/unittest/mock.py", line 656, in __getattr__
    elif self._mock_methods is not None:
         ^^^^^^^^^^^^^^^^^^
  File "/Users/santorella/miniconda3/envs/ax_312_test/lib/python3.12/unittest/mock.py", line 655, in __getattr__
    raise AttributeError(name)
AttributeError: _mock_methods

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "[...]/test_debug.py", line 15, in <module>
    mock.MagicMock()
  File "/Users/santorella/miniconda3/envs/ax_312_test/lib/python3.12/unittest/mock.py", line 2119, in __init__
    def __init__(self, /, *args, **kw):

SystemError: <sys.legacy_event_handler object at 0x1010a1370> returned a result with an exception set

esantorella added a commit to esantorella/Ax that referenced this issue May 25, 2024
Summary:
A few changes were needed to enable Python 3.12 in tests:
* I unpinned the pyfakefs version.
* I removed Yappi entirely. Yappi was used to print basic profiling output on test runtime when tests timed out. It seems to be incompatible with unittest.mock with Python 3.12; see here: sumerc/yappi#172 I don't think anyone was using Yappi's output.

Note: This previously did not work when facebook#2349 was put in due to an issue with pyzmq, but since then there have been new pyzmq releases, which added 3.12 support.

Differential Revision: D57815360
esantorella added a commit to esantorella/Ax that referenced this issue May 25, 2024
Summary:

A few changes were needed to enable Python 3.12 in tests:
* I unpinned the pyfakefs version.
* I removed Yappi entirely. Yappi was used to print basic profiling output on test runtime when tests timed out. It seems to be incompatible with unittest.mock with Python 3.12; see here: sumerc/yappi#172 I don't think anyone was using Yappi's output.

Note: This previously did not work when facebook#2349 was put in due to an issue with pyzmq, but since then there have been new pyzmq releases, which added 3.12 support.

Differential Revision: D57815360
facebook-github-bot pushed a commit to facebook/Ax that referenced this issue May 25, 2024
Summary:
Pull Request resolved: #2478

A few changes were needed to enable Python 3.12 in tests:
* I unpinned the pyfakefs version.
* I removed Yappi entirely. Yappi was used to print basic profiling output on test runtime when tests timed out. It seems to be incompatible with unittest.mock with Python 3.12; see here: sumerc/yappi#172 I don't think anyone was using Yappi's output.

Note: This previously did not work when #2349 was put in due to an issue with pyzmq, but since then there have been new pyzmq releases, which added 3.12 support.

Reviewed By: Balandat

Differential Revision: D57815360

fbshipit-source-id: 4c40fcde516034e1c6065e45c37f9d3110bd2b78
@mcarans
Copy link

mcarans commented Jul 11, 2024

@sumerc I see an AttributeError as well on Python 3.12.

It is easy to reproduce:
pip install the ruamel.yaml and yappi libraries, then create a file with this code:

from ruamel.yaml import YAML

print("hi")

Profile on the command line with yappi file.py. It will fail with an AttributeError. However, if you run the file directly, it will work.

Traceback:

Traceback (most recent call last):
  File "/home/mcarans/Code/VirtualEnvs/scratch/lib/python3.12/site-packages/ruamel/yaml/util.py", line 38, in __getattribute__
    lazy_self = object.__getattribute__(self, 'lazy_self')
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'LazyEval' object has no attribute 'lazy_self'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/mcarans/Code/VirtualEnvs/scratch/bin/yappi", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/home/mcarans/Code/VirtualEnvs/scratch/lib/python3.12/site-packages/yappi.py", line 1500, in main
    exec(
  File "yappi_ruamel_fail.py", line 1, in <module>
    from ruamel.yaml import YAML
  File "/home/mcarans/Code/VirtualEnvs/scratch/lib/python3.12/site-packages/ruamel/yaml/__init__.py", line 51, in <module>
    from .cyaml import *  # NOQA
    ^^^^^^^^^^^^^^^^^^^^
  File "/home/mcarans/Code/VirtualEnvs/scratch/lib/python3.12/site-packages/ruamel/yaml/cyaml.py", line 4, in <module>
    from _ruamel_yaml import CParser, CEmitter  # type: ignore
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "_ruamel_yaml.pyx", line 18, in init ruamel.yaml.clib._ruamel_yaml
  File "/home/mcarans/Code/VirtualEnvs/scratch/lib/python3.12/site-packages/ruamel/yaml/reader.py", line 26, in <module>
    from ruamel.yaml.util import RegExp
  File "/home/mcarans/Code/VirtualEnvs/scratch/lib/python3.12/site-packages/ruamel/yaml/util.py", line 49, in <module>
    timestamp_regexp = RegExp(
                       ^^^^^^^
  File "/home/mcarans/Code/VirtualEnvs/scratch/lib/python3.12/site-packages/ruamel/yaml/util.py", line 29, in __init__
    def __init__(self, func: Callable[..., Any], *args: Any, **kwargs: Any) -> None:

SystemError: <sys.legacy_event_handler object at 0x74ff20e77f10> returned a result with an exception set

@sumerc sumerc changed the title Is python 3.12 supported? Yappi 3.12 AttributeError: _mock_methods when using MagicMock Jul 19, 2024
@sumerc
Copy link
Owner

sumerc commented Jul 19, 2024

I will look into this!

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

No branches or pull requests

4 participants