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

Fixed: srepr not printing dict and set properly #19346

Merged
merged 4 commits into from
May 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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()]
Copy link
Member

Choose a reason for hiding this comment

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

Most of the other implementations use _print I think

Suggested change
dict_kvs = ["%s: %s" % (self.doprint(key), self.doprint(value)) for key, value in expr.items()]
dict_kvs = ["%s: %s" % (self._print(key), self._print(value)) for key, value in expr.items()]

Copy link
Member

Choose a reason for hiding this comment

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

Although I suppose reprify does not, so this doesn't really matter.

return "{%s}" % sep.join(dict_kvs)

def _print_set(self, expr):
if not expr:
return "set()"
return "{%s}" % self.reprify(expr, ", ")
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 wrong, and prints empty sets as empty dictionaries

Suggested change
return "{%s}" % self.reprify(expr, ", ")
if not expr:
return "set()"
else:
return "{%s}" % self.reprify(expr, ", ")

Edit: Done, thanks!


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')}")