-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
providers/options: implement Mapping #9704
Conversation
Thank you for opening a new pull request. Before your PR can be merged it will first need to pass continuous integration tests and be reviewed. Sometimes the review process can be slow, so please be patient. While you're waiting, please feel free to review other open PRs. While only a subset of people are authorized to approve pull requests for merging, everyone is encouraged to review open pull requests. Doing reviews helps reduce the burden on the core team and helps make the project's code better for everyone. One or more of the the following people are requested to review 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.
Overall this LGTM, thanks for doing this! I have one inline comment about the docstring (it was preexisting but maybe we can clean it up while we're editing the text for clarity). The only other thing that's missing here is a release note. If you could add a release note for this new feature that would be great so we document this as part of 0.24.0. The process for doing this is documented here:
https://github.com/Qiskit/qiskit-terra/blob/main/CONTRIBUTING.md#release-notes
Pull Request Test Coverage Report for Build 4461035815
💛 - Coveralls |
Thanks for the positive review! I added the release notes. |
qiskit/providers/options.py
Outdated
def __delitem__(self, key): | ||
del self._fields[key] |
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.
Hmm, I know this is part of the mutable mapping abstract interface so it needs to be implemented (which is why I skipped over this in my earlier review). But on reflection I'm not sure deletion is actually part of the workflow for this class. The typical workflow is the provider package creates the Options
object at backend initialization and it contains the canonical set of fields for users to set along with their default values. Deletion here doesn't make a ton of sense because nothing should ever be deleting the fields after it's created as they contain the current default behavior for running on a backend.
I'm not 100% sure the best way to proceed with this. My first thought would be to just make this raise an exception. But, that's not great either because it would show methods like pop()
and clear()
as being available which they shouldn't be. I'm thinking the best path forward might be to change MutableMapping
to just Mapping
and keep the __setitem__
definition so we support assignment. What do you think?
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 review and apologies for the delayed feedback.
I feel like the actual usage of Options
is rather that of a (non-frozen) dataclass than a dict, isn't it? Option fields are defined by the backend implementation but the values can be edited. This would however be a quite massive change I guess.
Implementing __setitem__
however allows to dynamically add options (potentially by mistake in case of a typo), and by symmetry, it would be surprising not to implement the full MutableMapping
interface but something in-between the standard ABCs. To keep the learning curve smooth, I think that it's easier to say that Options
is a MutableMapping
with optional custom validators and no extra peculiarities.
I'd therefore rather be in favor of implementing the full MutableMapping
interface, in order not to surprise users. Do you find this reasonable?
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 don't think it's surprising if we say "it fulfills Mapping
, but you can also change the values" - it can fulfil part of one interface without fulfilling all of the next one in the stack, just like numpy.array
fulfils Iterable
but not Sequence
(because they don't want to implement the named methods that Sequence
requires).
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.
As in, there's nothing about implementing only Mapping
that means you can't be mutable. It just means that you provide a particular ABC's definition of "view" semantics around your keys and values.
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.
Fair enough. I updated the branch to have Options
implement Mapping
.
it fulfills Mapping, but you can also change the values
and add keys. This is why I find it strange to keep to asymmetry by not providing an interface to delete keys. But I see that it's not the wanted usage of the datastructure.
b29ca5a
to
e7167ec
Compare
Reword docstring. Co-authored-by: Matthew Treinish <[email protected]>
e7167ec
to
da2874a
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.
This LGTM now, thanks for doing this and the quick updates. Sorry for taking a while to circle back to this.
* providers/options: implement MutableMapping Resolves Qiskit#9686 * Update qiskit/providers/options.py Reword docstring. Co-authored-by: Matthew Treinish <[email protected]> * Add release notes. * providers/options: implement Mapping + setitem --------- Co-authored-by: Matthew Treinish <[email protected]>
* providers/options: implement MutableMapping Resolves Qiskit#9686 * Update qiskit/providers/options.py Reword docstring. Co-authored-by: Matthew Treinish <[email protected]> * Add release notes. * providers/options: implement Mapping + setitem --------- Co-authored-by: Matthew Treinish <[email protected]>
Summary
This is an option for resolving #9686 by implementing the
collections.abc.MutableMapping
interface for theOptions
class. This automatically provides implementations of the suggesteditems()
and of the existingget()
methods.Details and comments
As a
MutableMapping
,Options
instances behave like mutable dictionaries with the extra validator feature. The__setitem__
implementation goes throughupdate_options
to make sure that the validator is running. This is inconsistent with__setattr__
but I understand that the namespace interface is there for backwards compatibility and running the validator may break use cases (although this would arguably be a logic error).A side effect of implementing
MutableMapping
is that there's yet another way of accessing the options (indexing) which is however natural for an object that exposes aitems()
method.Resolves #9686