-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
MAINT: back printoptions with a true context variable #26846
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
Conversation
I was wondering if we should stop passing around Unlike the old code, contextvars should protect us from having to "freeze" the state early on to avoid threading issues. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can see the slight worry about going out to python for every repr, but this does have the advantage of being quite clean (plus printing is presumably quite slow).
My comments mostly seem to be about naming...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, as Marten already reviewed anyway. A few comments, new context safety tests could be nice, but I can live without most changes here.
Overall, it is nice and simple (I do think this code can use more love, which this old comment was about, but I had expected that this might end up changing more, forcing other refactors a bit).
Last push responds to all the review comments from last week. Thanks all for taking a look at this. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, was considering to just fix it up, add a tiny release note and put it in. But, I think checking error for get_legacy_print_mode()
is a slightly bigger change that is unfortunately needed for sure.
b340c76
to
02506cc
Compare
The last pushes respond to review comments and add a release note. I also figured out how to write async tests using the same patterns as the threading tests, cribbing off the errstate tests. It turns out |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn't look super carefully, but only found three nitpicks now, so approving.
Not merging yet, though, since I did wonder a bit whether it would make sense to subclass ContextDict(ContextVar)
and make it more dict
-like - in particular, one would add __getitem__
, __or__
and copy
. But perhaps that would just hide too much for too little gain.
a76d841
to
fe9add9
Compare
I don't think it's worth it. The explicit |
Thanks, @ngoldbaum. Yeah, if we had to get individual options a lot, a class might be nice. But as is, I also prefer it as is. |
This is a re-do of numpygh-26345, I'm taking over from @mtsokol because this is needed for the free-threaded work. The new _printoptions.py file exists to avoid a circular import during setup of the multiarray module. I'm guessing this adds some overhead to printing. I haven't benchmarked it because it wasn't clear to me: do we care about printing performance? I could certainly add some caching or a way to avoid repeatedly calling get_legacy_print_mode for every printed value. We could also keep the C global we had before but make it thread-local. I just thought it made things conceptually simpler to store all the printoptions state in the context variable. Co-authored-by: Mateusz Sokół <mat646@gmail.com>
This is a re-do of #26345, I'm taking over from @mtsokol because this is needed for the free-threaded work.
The new
_printoptions.py
file exists to avoid a circular import during setup of the multiarray module.I'm guessing this adds some overhead to printing. I haven't benchmarked it because it wasn't clear to me: do we care about printing performance? I could certainly add some caching or a way to avoid repeatedly calling
get_legacy_print_mode
for every printed value. We could also keep the C global we had before but make it thread-local. I just thought it made things conceptually simpler to store all the printoptions state in the context variable.I also looked at Sebastians comment from the old PR and I'm not totally sure what he wants. It's not immediately clear to me how to expose the context variable we're creating as an internal-only context manager, if that's even what Sebastian was getting at.