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

Python copy does not replicate PyDiGraph.copy() #836

Closed
lukepmccombs opened this issue Mar 17, 2023 · 4 comments · Fixed by #838
Closed

Python copy does not replicate PyDiGraph.copy() #836

lukepmccombs opened this issue Mar 17, 2023 · 4 comments · Fixed by #838
Labels
bug Something isn't working

Comments

@lukepmccombs
Copy link
Contributor

lukepmccombs commented Mar 17, 2023

Information

  • rustworkx version: 0.12.1
  • Python version: 3.10.6
  • Rust version: 1.64.0
  • Operating system: Ubuntu 22.04.2 LTS

What is the current behavior?

check_cycles is not copied when a PyDiGraph is copied using the python native copy.copy.

What is the expected behavior?

copy.copy(PyDiGraph) should produce identical results to PyDiGraph.copy().

Steps to reproduce the problem

import rustworkx as rx
import copy


a = rx.PyDiGraph(check_cycle=True)
print(a.check_cycle)

b = a.copy()
print(b.check_cycle)

c = copy.copy(a)
print(c.check_cycle)

d = copy.deepcopy(a)
print(d.check_cycle)
True
True
False
False

Edit: My original test used an object that copy.deepcopy() does not copy into a new object. Upon further testing, it behaves as expected.

@lukepmccombs lukepmccombs added the bug Something isn't working label Mar 17, 2023
@lukepmccombs
Copy link
Contributor Author

lukepmccombs commented Mar 17, 2023

The check_cycles issue seems to stem from the implementation of __getstate__ in src/digraph.rs, which is missing an entry for it.

@lukepmccombs
Copy link
Contributor Author

lukepmccombs commented Mar 17, 2023

Here is a bandaid fix I made to add support for copy at the very least.

import copyreg
import rustworkx as rx

def __pickle_pydigraph(net: rx.PyDiGraph):
    return rx.PyDiGraph, (net.check_cycle,), net.__getstate__()

copyreg.pickle(rx.PyDiGraph, __pickle_pydigraph)

Edit: Found a better way of doing this.

@lukepmccombs lukepmccombs changed the title Python copy does not replicate PyDiGraph.copy(), deepcopy also maintains same objects. Python copy does not replicate PyDiGraph.copy(). Mar 17, 2023
@lukepmccombs lukepmccombs changed the title Python copy does not replicate PyDiGraph.copy(). Python copy does not replicate PyDiGraph.copy() Mar 17, 2023
@mtreinish
Copy link
Member

Hmm, yeah the deep copy is a oversight and simple bug to fix, we just missed check_cycle in the __getstate__/__setstate__ methods on PyDiGraph (I expect because it was copy and pasted from PyGraph and modified).

As for the shallow copy bug, I'm not sure why it's missing that attribute we don't define anything custom related to the copy.copy() function. But I'm thinking the best path forward might be to just define __copy__ on the classes and have it internally do what the copy() method does and return self.clone().

Would you be interested in pushing a PR to fix these issues?

@lukepmccombs
Copy link
Contributor Author

lukepmccombs commented Mar 17, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants