You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Deprecate the unused CodeObject flag CO_NESTED #96811
Both appear to be detecting nested functions in order to avoid trying to pickle them, so checking for freevars wouldn't be a suitable alternative in these cases. Checking for "<locals>" in fn.__qualname__ would be.
Both appear to be detecting nested functions in order to avoid trying to pickle them, so checking for freevars wouldn't be a suitable alternative in these cases. Checking for "<locals>" in fn.__qualname__ would be.
If people need this, and that's the best alternative then maybe we should keep the flag?
Is there an alternative without testing the CO_NESTED flag?
I mentioned both alternatives that I know of above. The only real equivalent AFAIK is to check "<locals>" in func.__qualname__, which should give the same result in all cases I'm aware of. A semi-equivalent is to check if the function has freevars, if what you really care about is whether it closes over any values, but some nested functions don't have freevars.
Activity
carljm commentedon Sep 13, 2022
Looking for third-party uses, I found https://github.com/pytorch/pytorch/blob/cfb9d0d23314fd28be118b6ca280ded55364e71c/torch/utils/data/datapipes/utils/common.py#L110 and https://github.com/catboost/catboost/blob/dfc3f04050ef9acc3f070f38fe363fec93a3bc8d/contrib/python/joblib/joblib/externals/loky/cloudpickle_wrapper.py#L70
Both appear to be detecting nested functions in order to avoid trying to pickle them, so checking for freevars wouldn't be a suitable alternative in these cases. Checking for
"<locals>" in fn.__qualname__would be.hugovk commentedon Sep 14, 2022
Using Victor's https://github.com/vstinner/misc/blob/main/cpython/search_pypi_top.py on the top 5k PyPI packages as of 2022-06-12 (or rather the 4,768 that have sdists), it found 90 matching lines in 13 projects.
They're mostly typeshed definitions.
Full output
The non-typeshed results:
vstinner commentedon Sep 14, 2022
None of these projects set the flag, only cloudpickle_wrapper.py of joblib checks if the flag is set or not.
vstinner commentedon Sep 14, 2022
IMO in this case, it's fine to deprecate the flag. Thanks for checking top 5000 PyPI projects, that's useful to take a decision.
iritkatriel commentedon Sep 14, 2022
If people need this, and that's the best alternative then maybe we should keep the flag?
vstinner commentedon Sep 14, 2022
Oh, right. In Python 3.12, CO_NESTED is still sets properly: it can be used to check if a function was declared inside another function.
Python 3.12 output:
Is there an alternative without testing the CO_NESTED flag?
carljm commentedon Sep 14, 2022
I mentioned both alternatives that I know of above. The only real equivalent AFAIK is to check
"<locals>" in func.__qualname__, which should give the same result in all cases I'm aware of. A semi-equivalent is to check if the function has freevars, if what you really care about is whether it closes over any values, but some nested functions don't have freevars.iritkatriel commentedon Sep 16, 2022
I think we should not remove it. It's used, there is no good alternative, and it's not in anybody's way.