Skip to content

Commit

Permalink
Merge pull request #19346 from rational-kunal/dev
Browse files Browse the repository at this point in the history
Fixed: srepr not printing dict and set properly
  • Loading branch information
oscarbenjamin committed May 19, 2020
2 parents 6348bdd + 2a7b8a4 commit 3e33c30
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
10 changes: 10 additions & 0 deletions sympy/printing/repr.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,16 @@ def _print_EmptySequence(self, expr):
def _print_list(self, expr):
return "[%s]" % self.reprify(expr, ", ")

def _print_dict(self, expr):
sep = ", "
dict_kvs = ["%s: %s" % (self.doprint(key), self.doprint(value)) for key, value in expr.items()]
return "{%s}" % sep.join(dict_kvs)

def _print_set(self, expr):
if not expr:
return "set()"
return "{%s}" % self.reprify(expr, ", ")

def _print_MatrixBase(self, expr):
# special case for some empty matrices
if (expr.rows == 0) ^ (expr.cols == 0):
Expand Down
23 changes: 23 additions & 0 deletions sympy/printing/tests/test_repr.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,3 +318,26 @@ def test_diffgeom():
assert srepr(rect) == "CoordSystem('rect', Patch('P', Manifold('M', 2)), ('rect_0', 'rect_1'))"
b = BaseScalarField(rect, 0)
assert srepr(b) == "BaseScalarField(CoordSystem('rect', Patch('P', Manifold('M', 2)), ('rect_0', 'rect_1')), Integer(0))"

def test_dict():
from sympy import srepr
from sympy.abc import x, y, z
d = {}
assert srepr(d) == "{}"
d = {x: y}
assert srepr(d) == "{Symbol('x'): Symbol('y')}"
d = {x: y, y: z}
assert srepr(d) in (
"{Symbol('x'): Symbol('y'), Symbol('y'): Symbol('z')}",
"{Symbol('y'): Symbol('z'), Symbol('x'): Symbol('y')}",
)
d = {x: {y: z}}
assert srepr(d) == "{Symbol('x'): {Symbol('y'): Symbol('z')}}"

def test_set():
from sympy import srepr
from sympy.abc import x, y
s = set()
assert srepr(s) == "set()"
s = {x, y}
assert srepr(s) in ("{Symbol('x'), Symbol('y')}", "{Symbol('y'), Symbol('x')}")

0 comments on commit 3e33c30

Please sign in to comment.