-
-
Notifications
You must be signed in to change notification settings - Fork 166
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
Raise TypeError when a dictionary is passed to MeasurementResult constructor #2523
Conversation
it's natural to think that you can pass results directly from some sort of experiment into the MeasurementResult class.
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #2523 +/- ##
=======================================
Coverage 98.72% 98.72%
=======================================
Files 90 90
Lines 4156 4158 +2
=======================================
+ Hits 4103 4105 +2
Misses 53 53 ☔ View full report in Codecov by Sentry. |
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.
Solution 1 sounds reasonable to me, and a strict improvement on the current behavior.
Note: in the docstring, we should have an example of how to instantiate MeasurementResult, at the moment it's not very clear (unless I am overlooking it).
@@ -125,6 +125,11 @@ class MeasurementResult: | |||
|
|||
def __post_init__(self) -> None: | |||
# Validate arguments | |||
if isinstance(self.result, dict): |
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.
We could have something stricter that checks for result
not being an instance of Sequence[Bitstring]
, which is what we expect as type, but then both the check and the error message become more complicated, so I would leave it as is.
Thanks for the suggestion of adding an example in the docstring. I like that. Done in 71a0a63. |
Motivation: Multiple students at the UMass Amherst summer school attempted to pass a dictionary to instantiate a
MeasurementResult
object while working through the challenges. This silently accepts, but provides erroneous results. E.g.:Considerations: I could see the following two potential solutions to help with this issue.
TypeError
, letting the user know we have a methodMeasurementResult.from_counts
for this specific pattern.MeasurementResult.from_counts
in the__post_init__
method to re-instantiateself
.I chose to raise solution 1 as it seems to be a simple approach to address the problem. Admittedly, I am less familiar with the technical details of re-instantiating an object within the
__post_init__
function, but happy to test that out if reviewers think it is more appropriate.