Skip to content
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

Removing broad-exception-raised lint rule and updates #12356

Merged
merged 6 commits into from May 10, 2024

Conversation

joesho112358
Copy link
Contributor

Summary

#9614 removing broad-exception-raised

Details and comments

Updates for removing the broad-exception-raised rule

@qiskit-bot qiskit-bot added the Community PR PRs from contributors that are not 'members' of the Qiskit repo label May 7, 2024
@qiskit-bot
Copy link
Collaborator

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:

@coveralls
Copy link

coveralls commented May 7, 2024

Pull Request Test Coverage Report for Build 9032720722

Details

  • 1 of 6 (16.67%) changed or added relevant lines in 4 files are covered.
  • 13 unchanged lines in 3 files lost coverage.
  • Overall coverage increased (+0.007%) to 89.638%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit/synthesis/clifford/clifford_decompose_bm.py 0 1 0.0%
qiskit/visualization/bloch.py 0 2 0.0%
qiskit/visualization/transition_visualization.py 0 2 0.0%
Files with Coverage Reduction New Missed Lines %
qiskit/transpiler/passes/synthesis/unitary_synthesis.py 2 88.2%
crates/qasm2/src/lex.rs 5 92.11%
crates/qasm2/src/parse.rs 6 97.61%
Totals Coverage Status
Change from base Build 9032084393: 0.007%
Covered Lines: 62208
Relevant Lines: 69399

💛 - Coveralls

Copy link
Member

@jakelishman jakelishman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for looking at this - this is definitely a good lint to get enabled. I left a couple of comments about avoiding RuntimeError - we usually try and avoid that one as much as possible because there's generally some tighter bound to apply. It's all a little bit subjective of course, but we (at least I!) generally try to keep RuntimeError for exceptions in defensive programming bits amongst ourselves, like if an internal private invariant has somehow become violated.

(Fwiw, it matters rather less what we use in tests, since if we're ever raising an Exception in tests, it's usually just to indicate a test failure or something.)

qiskit/visualization/bloch.py Outdated Show resolved Hide resolved
qiskit/visualization/bloch.py Outdated Show resolved Hide resolved
qiskit/quantum_info/quaternion.py Outdated Show resolved Hide resolved
qiskit/visualization/transition_visualization.py Outdated Show resolved Hide resolved
qiskit/visualization/transition_visualization.py Outdated Show resolved Hide resolved
test/benchmarks/utils.py Outdated Show resolved Hide resolved
@joesho112358
Copy link
Contributor Author

Thanks for looking at this - this is definitely a good lint to get enabled. I left a couple of comments about avoiding RuntimeError - we usually try and avoid that one as much as possible because there's generally some tighter bound to apply. It's all a little bit subjective of course, but we (at least I!) generally try to keep RuntimeError for exceptions in defensive programming bits amongst ourselves, like if an internal private invariant has somehow become violated.

(Fwiw, it matters rather less what we use in tests, since if we're ever raising an Exception in tests, it's usually just to indicate a test failure or something.)

seems fair to me! updates are in for another look

Copy link
Member

@jakelishman jakelishman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the changes! One comment about a pre-existing test that ought to be written in a different way, otherwise this is good to merge.

Comment on lines 92 to 100
def test_mul_by_array(self):
"""Quaternions cannot be multiplied with an array."""
other_array = np.array([0.1, 0.2, 0.3, 0.4])
self.assertRaises(Exception, self.quat_unnormalized.__mul__, other_array)
self.assertEqual(NotImplemented, self.quat_unnormalized.__mul__(other_array))

def test_mul_by_scalar(self):
"""Quaternions cannot be multiplied with a scalar."""
other_scalar = 0.123456789
self.assertRaises(Exception, self.quat_unnormalized.__mul__, other_scalar)
self.assertEqual(NotImplemented, self.quat_unnormalized.__mul__(other_scalar))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is pre-existing, stemming from the previous implementation of these magic methods being incorrect, but in general we don't want to call __mul__ directly - NotImplemented is just a sentinel value that's properly interpreted by Python. The normal forms of these tests would be

with self.assertRaises(TypeError):
    _ = self.quat_unnormalized * other_array

and similar for the other one.

This actually means that Python will also try the __rmul__ methods on other_array and other_scalar as well before raising the TypeError, in case either of those classes is something that can explicitly handle multiplication by Quaternion, though in this case, both will fail. The Numpy array test will actually start to succeed (Numpy will attempt to broadcast the multiplication), but then it'll fail the internal scalar multiplication.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i see. yeah, i was thinking this was more of a unit test vs integration test. updates on the way!

Copy link
Member

@jakelishman jakelishman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ace, thanks for the changes!

@jakelishman jakelishman added this pull request to the merge queue May 10, 2024
@jakelishman jakelishman added type: qa Issues and PRs that relate to testing and code quality Changelog: None Do not include in changelog labels May 10, 2024
Merged via the queue into Qiskit:main with commit f20dad0 May 10, 2024
15 checks passed
@joesho112358 joesho112358 deleted the remove-broad-exception-raised branch May 10, 2024 20:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: None Do not include in changelog Community PR PRs from contributors that are not 'members' of the Qiskit repo type: qa Issues and PRs that relate to testing and code quality
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

None yet

4 participants