Open stephanos-stephani opened 8 months ago
Also reproducible on python 3.12 and importing glom
This one is a complete mystery to me! Could you post a traceback for the glom one too?
It would also be helpful to know if it happens with other profilers e.g. cProfile?
Didn't happen when I was using cProfile. Here is the stacktrace: glom version 23.5.0 pyinstrument version 4.6.2 python version 3.12.2 TypeError Traceback (most recent call last) File Untitled-1:5 3 p = pyinstrument.Profiler() 4 p.start() ----> 5 import glom 6 p.stop()
File venv/lib/python3.12/site-packages/glom/init.py:48 34 from glom.reduction import Sum, Fold, Flatten, flatten, FoldError, Merge, merge 35 from glom.matching import (M, 36 Or, 37 And, (...) 46 Check, 47 CheckError) ---> 48 from glom.mutation import Assign, Delete, assign, delete, PathDeleteError 50 # there's no -ion word that really fits what "streaming" means. 51 # generation, production, iteration, all have more relevant meanings 52 # elsewhere. (maybe procrastination :P) 53 from glom.streaming import Iter
File .venv/lib/python3.12/site-packages/glom/mutation.py:209 204 return glom(obj, Assign(path, val, missing=missing)) 207 _ALL_BUILTIN_TYPES = [v for v in builtins.values() if isinstance(v, type)] 208 _BUILTIN_BASE_TYPES = [v for v in _ALL_BUILTIN_TYPES --> 209 if not issubclass(v, tuple([t for t in _ALL_BUILTIN_TYPES 210 if t not in (v, type, object)]))] 211 _UNASSIGNABLE_BASE_TYPES = tuple(set(_BUILTIN_BASE_TYPES) 212 - set([dict, list, BaseException, object, type])) 215 def _set_sequence_item(target, idx, val):
TypeError: issubclass() arg 2 must be a class, a tuple of classes, or a union
I've taken a good look at this and it appears to be a CPython bug, similar to this issue regarding tracing.
The problem is that nevergrad grabs a function's locals using locals()
, and then modifies it in a subfunction. This hits an edge-case of locals()
, where the return value isn't a snapshot but a reference to the underlying frame's data, where some bugs are lurking.
The good news is that since PEP 667, this is now fixed in Python 3.13 onwards. Alternatively, nevergrad could work around this issue by copying the contents of locals()
before modifying it, like dict(locals())
. @stephanos-stephani, would you like to raise an issue in the nevergrad repo to suggest that?
To reproduce:
This is under python 3.11, nevergrad 0.13.0, and pyinstrument 4.6.1
Traceback: