-
-
Notifications
You must be signed in to change notification settings - Fork 891
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 get_wrapper_attr
/ set_wrapper_attr
.
#1293
Conversation
28a7251
to
569bd4f
Compare
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 for the PR
Well done for spotting the bug, its subtle.
I like your improvement to get_wrapper_attr
.
I've slightly modified your updated set_wrapper_attr
with some comments as I don't like while True
loops.
Looking at the tests, I thought we might need to add some more but I think your new ones should fix the edge case that we missed
No problem. Indeed, additional tests could be added.
Glad you like it, but why did you remove it from the PR then ? x) PS: After benchmarking. It was not faster in practice. I don't really know why. I will have a look at it later. |
I'm confused, I thought I was editing a copy of your PR that included it. |
Apparently no !
After checking, I don't think it will happen. In the particular case where storing the value before returning can be avoided, some kind of copy-elision mechanism is making the function much faster, which ultimately beats the advantage of skipping the use of |
I guess the mistake was worth it then 😅 |
Description
IMHO, the current implementation of
get_wrapper_attr
andset_wrapper_attr
is flawed because they act inconsistently wrt each other. For instance, these two snippets is failing:First, I think that if the top-most wrapper has the attribute,
set_wrapper_attr
should set it (by the way, this is what the documentation is stating). When it does not, then it is not clear whether it should raise an exception (basically the same thanget_wrapper_attr
or create the attribute (at a level to be determined). In the latter case, I think it definitely makes more sense to add the attribute to the top-most wrapper, otherwise the second snippet right above would still be failing. Sincehas_wrapper_attr
is provided, it would still be possible for the user to implement its custom logic to rather add the attribute to the unwrapped environment, but I think in most casesenv.unwrapped.MY_ATTRIBUTE = False
would do fine. Besides, I think that adding the attribute to the top-most wrapper makes more sense, because it means thatset_wrapper_attr
andget_wrapper_attr
behave exactly asset_attr
,get_attr
when an attribute exists for a given wrapper, which is what one would expect intuitively I think. Regarding whetherset_wrapper_attr
should raise an exception when an attribute does not exist at any level in the stack, I think it is more appropriate to add the missing attribute to the top-most layer because it maintains consistency with bothset_attr
andget_attr
once again.PS: I also made
get_wrapper_attr
faster by avoiding callinghas_attr
in conjunction withget_attr
. This trick is quite common in the standard library.set_wrapper_attr
should also be slightly faster.Type of change
Please delete options that are not relevant.
Checklist:
pre-commit
checks withpre-commit run --all-files
(seeCONTRIBUTING.md
instructions to set it up)