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
Conversation
✅ Hi, I am the SymPy bot (v158). I'm here to help you write a release notes entry. Please read the guide on how to write release notes. Your release notes are in good order. Here is what the release notes will look like:
This will be added to https://github.com/sympy/sympy/wiki/Release-Notes-for-1.7. Note: This comment will be updated with the latest check if you edit the pull request. You need to reload the page to see it. Click here to see the pull request description that was parsed.
Update The release notes on the wiki have been updated. |
@@ -144,6 +144,14 @@ 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()] |
There was a problem hiding this comment.
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
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()] |
There was a problem hiding this comment.
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): | ||
return "{%s}" % self.reprify(expr, ", ") |
There was a problem hiding this comment.
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
return "{%s}" % self.reprify(expr, ", ") | |
if not expr: | |
return "set()" | |
else: | |
return "{%s}" % self.reprify(expr, ", ") |
Edit: Done, thanks!
sympy/printing/tests/test_repr.py
Outdated
def test_set(): | ||
from sympy import srepr | ||
from sympy.abc import x, y | ||
s = {x} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s = {x} | |
s = {} | |
assert srepr(s) == "set()" | |
s = {x} |
Generally looks good, just a mistake on empty sets |
@eric-wieser, should empty dict be printed differently |
No, empty dict is correct. The concern is that |
sympy/printing/tests/test_repr.py
Outdated
d = {x: y} | ||
assert srepr(d) == "{Symbol('x'): Symbol('y')}" | ||
d = {x: y, y: z} | ||
assert srepr(d) == "{Symbol('x'): Symbol('y'), Symbol('y'): Symbol('z')}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test won't work in Python 3.5, because dicts are not ordered. You need:
assert srepr(d) == "{Symbol('x'): Symbol('y'), Symbol('y'): Symbol('z')}" | |
# dict order is arbitrary until CPython 3.6 or Python 3.7 | |
assert srepr(d) in ( | |
"{Symbol('x'): Symbol('y'), Symbol('y'): Symbol('z')}", | |
"{Symbol('y'): Symbol('z'), Symbol('x'): Symbol('y')}", | |
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rational-kunal, CI will randomly fail unless you also make this change.
sympy/printing/tests/test_repr.py
Outdated
s = {x} | ||
assert srepr(s) == "{Symbol('x')}" | ||
s = {x, y} | ||
assert srepr(s) == "{Symbol('x'), Symbol('y')}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CI is failing from this test, because set
s are not ordered.
assert srepr(s) == "{Symbol('x'), Symbol('y')}" | |
# set order is arbitrary | |
assert srepr(s) in ( | |
"{Symbol('x'), Symbol('y')}", | |
"{Symbol('y'), Symbol('x')}", | |
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't account for that, thanks!
Codecov Report
@@ Coverage Diff @@
## master #19346 +/- ##
=============================================
+ Coverage 75.598% 75.601% +0.002%
=============================================
Files 651 651
Lines 169483 169491 +8
Branches 39988 39990 +2
=============================================
+ Hits 128127 128137 +10
+ Misses 35744 35742 -2
Partials 5612 5612 |
CI failure is an unrelated network issue |
Looks good. Thanks |
References to other Issues or PRs
Fixes #19329
Brief description of what is fixed or changed
Added two methods
_print_dict
and_print_set
toReprPrinter
.Other comments
Release Notes
srepr
function would not print dictionary and set properly