Skip to content

Commit

Permalink
Update the default repr() of symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
arcondello committed Jul 11, 2024
1 parent 1d5a0ce commit c706ae8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
15 changes: 15 additions & 0 deletions dwave/optimization/model.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import numpy as np
from cpython cimport Py_buffer
from cython.operator cimport dereference as deref, preincrement as inc
from cython.operator cimport typeid
from libc.stdint cimport uintptr_t
from libcpp cimport bool
from libcpp.utility cimport move
from libcpp.vector cimport vector
Expand Down Expand Up @@ -1193,6 +1194,20 @@ cdef class Symbol:
# via their subclasses.
raise ValueError("Symbols cannot be constructed directly")

def __repr__(self):
"""Return a representation of the symbol.
The representation refers to the id of the underlying node, rather than
the id of the Python symbol.
"""
cls = type(self)
# We refer to the node_ptr, which is not necessarily the address of the
# C++ node, as it sublasses Node.
# But this is unique to each node, and functions as an id rather than
# as a pointer, so that's OK.
# Otherwise we aim to match Python's default __repr__.
return f"<{cls.__module__}.{cls.__qualname__} at {<uintptr_t>self.node_ptr:#x}>"

cdef void initialize_node(self, Model model, cppNode* node_ptr) noexcept:
self.model = model

Expand Down
5 changes: 5 additions & 0 deletions releasenotes/notes/fix-to_networkx-a78f0f669cc9638c.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
features:
- |
Make ``repr()`` of symbols unique to the underlying node, rather than to the Python symbol.
See `#52 <https://github.com/dwavesystems/dwave-optimization/issues/52>_.
11 changes: 11 additions & 0 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,3 +424,14 @@ def test_abstract(self):
from dwave.optimization.model import Symbol
with self.assertRaisesRegex(ValueError, "Symbols cannot be constructed directly"):
Symbol()

def test_repr(self):
model = Model()
c0 = model.constant(5)
c1, = model.iter_symbols()
c2 = model.constant(5)

# the specific form is an implementation detail, but different symbols
# representing the same underlying node should have the same repr
self.assertEqual(repr(c0), repr(c1))
self.assertNotEqual(repr(c0), repr(c2))

0 comments on commit c706ae8

Please sign in to comment.