-
-
Notifications
You must be signed in to change notification settings - Fork 922
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
Improve 'set_wrapper_attr'. #1294
Improve 'set_wrapper_attr'. #1294
Conversation
dc61832
to
5d7de3e
Compare
5d7de3e
to
58dd2b2
Compare
@pseudo-rnd-thoughts Any thought on this PR ? |
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.
Apologies for taking a while to look at it @duburcqa
I like the idea, though I dislike returning an error.
Could we use a return True
if the attribute has been updated, then we are checking at the "top" wrapper if the lower wrappers have returned True, else set the attribute there?
This seems like a better way and possibly faster because there is no error checking
What I like with the implementation I suggest, is especially the ability to raise an exception. I think it adding an attribute in the base environment is rarely the intended objective. Raising an exception is a nice way to catch silly mistakes for the practitioner. Personally I would even prefer to set Besides, error CATCHING is slow, but error checking is free of cost (since Python 3.11). Since adding a new attribute is very unlikely, this approach is overall cheaper than |
Oh, I've learnt something new, but aren't we doing error-catching multiple times if none of the wrappers have the attribute. Also return True / False seems more elegant therefore, I still want to change it |
I don't like the idea of silently NOT adding the attribute if the user specifies At this point, having to do the check manually and raise an exception does not bring any improvement compared to calling I just do not see any issue regarding raising an exception as it point out a bug in 99.9% of the cases. And if it was intended, the user can just write |
Description
I propose to further improve 'set_wrapper_attr' (as a follow-up of PR #1293).
I added the optional keyword argument 'force'. Its role is to make sure that the attribute exists before setting it, so that no attribute while be create on the top-level wrapper. While this could already be done using 'has_wrapper_attr', but specifying the new optional argument is not only more convenient but also faster. Moreover, adding this extra argument enables to refactor the implementation of 'set_wrapper_attr' as a recursive function calling itself on the lower-level environment (in case of wrappers). This is not only more than twice faster (because
isinstance
is very slow...), but also more robust, as it allows anybody to derive its own wrapper fromgym.Env
and provides its own implementation of 'set_wrapper_attr' (this aspect is very important to me, because my library is doing exactly this). Finally, the implementation is easier to understand and closely align withget_wrapper_attr
.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)