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

Fix panic error at shortest paths #1134

Merged
merged 4 commits into from
Mar 8, 2024
Merged

Conversation

JPena-code
Copy link
Contributor

In #1117 is presented the presence of an uncaught pyo3.PanicException when the index of the source node is no out of bound in the graph. With this naive approach i try fix this issue raising a more understandable IndexError.

  • I ran rustfmt locally
  • I have added the tests to cover my changes.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.

@coveralls
Copy link

coveralls commented Mar 7, 2024

Pull Request Test Coverage Report for Build 8207084623

Details

  • 57 of 57 (100.0%) changed or added relevant lines in 1 file are covered.
  • 6 unchanged lines in 1 file lost coverage.
  • Overall coverage decreased (-0.02%) to 96.446%

Files with Coverage Reduction New Missed Lines %
src/shortest_path/all_pairs_bellman_ford.rs 6 95.53%
Totals Coverage Status
Change from base Build 8147160812: -0.02%
Covered Lines: 17151
Relevant Lines: 17783

💛 - Coveralls

Copy link
Collaborator

@IvanIsCoding IvanIsCoding 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 contributing this! The tests LGTM and so do the error messages. I did propose a simpler method to check if a node is present, though.

Also, you would mind expanding the fix to the Bellman-Ford and A* implementations? They are conceptually similar to Dijkstra's and were not included in the bug report. But they also panic now I believe

Comment on lines 80 to 84
if !graph
.node_indices()
.nodes
.iter()
.any(|&node| node == source)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we can simplify this to:

Suggested change
if !graph
.node_indices()
.nodes
.iter()
.any(|&node| node == source)
if !graph.graph.contains_node(start)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Note that you will need to move the if statement after the variable is defined

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, i really appreciate it the feedback, i gonna make the changes

if !graph
.node_indices()
.nodes
.iter()
.any(|&index| index == node)
{
return Err(PyIndexError::new_err(format!(
"Node source index \"{node}\" out of graph bound"
)));
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

Same

Comment on lines 475 to 480
if !graph
.node_indices()
.nodes
.iter()
.any(|&index| index == node)
{
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same

Comment on lines 230 to 234
if !graph
.node_indices()
.nodes
.iter()
.any(|&node| node == source)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same

@JPena-code JPena-code changed the title Fix panic error at dijkstra shortest paths Fix panic error at shortest paths Mar 8, 2024
Copy link
Collaborator

@IvanIsCoding IvanIsCoding left a comment

Choose a reason for hiding this comment

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

Awesome. This is good to

@IvanIsCoding IvanIsCoding added the stable-backport-potential This PR or issue is potentially worth backporting for inclusion in a stable branch label Mar 8, 2024
@IvanIsCoding IvanIsCoding merged commit 058b0c7 into Qiskit:main Mar 8, 2024
28 checks passed
mergify bot pushed a commit that referenced this pull request Mar 8, 2024
* lint suggestion UP032

* Test of dijkstra raising IndexError

* Test Bellman Ford and A*

* fix: shortest path

(cherry picked from commit 058b0c7)
@IvanIsCoding IvanIsCoding mentioned this pull request Mar 8, 2024
mergify bot added a commit that referenced this pull request Mar 8, 2024
* lint suggestion UP032

* Test of dijkstra raising IndexError

* Test Bellman Ford and A*

* fix: shortest path

(cherry picked from commit 058b0c7)

Co-authored-by: Julio Quintero <jequinterope@unal.edu.co>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stable-backport-potential This PR or issue is potentially worth backporting for inclusion in a stable branch
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants