Fix get_wrapper_attr
/ set_wrapper_attr
.
#1293
Merged
+22
−7
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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)