feat(python): Enable creation of independently reusable Config
instances
#20053
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.
At the moment we set the options on
Config
init (or via the associated@staticmethod
); when used as a context manger or function decorator this means we need to explicitly supply all the options on each usage - we cannot currently reuse an instance because scope exit resets the options to the state they were in at scope entry, and having the same object enter a new scope will not set the options again (as the options are set at init).This PR enables reusable
Config
instances. A new init parameter "apply_on_context_enter" is introduced which defers applying the indicated options until context/function scope is entered.Because the options are now stored and set on scope entry, bundles of
Config
options can now be created/managed centrally and then cleanly reused throughout a codebase without having to set all the options explicitly each time.Minor drive-by; added proper
__eq__
and__ne__
comparison methods forConfig
instances.Backwards-compatible update; no change to the current default behaviour.
Example
Declare some
Config
instances that take effect only when entering context/function scope:Can now use (and reuse) these objects as decorators or context managers without having to explicitly re-declare all the parameters: