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

Fixed: srepr not printing dict and set properly #19346

merged 4 commits into from May 19, 2020

Conversation

rational-kunal
Copy link
Contributor

@rational-kunal rational-kunal commented May 17, 2020

References to other Issues or PRs

Fixes #19329

Brief description of what is fixed or changed

Added two methods _print_dict and _print_set to ReprPrinter.

Other comments

Release Notes

  • printing
    • fixed a bug where srepr function would not print dictionary and set properly

@sympy-bot
Copy link

sympy-bot commented May 17, 2020

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:

  • printing
    • fixed a bug where srepr function would not print dictionary and set properly (#19346 by @rational-kunal)

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.

<!-- Your title above should be a short description of what
was changed. Do not include the issue number in the title. -->

#### References to other Issues or PRs
<!-- If this pull request fixes an issue, write "Fixes #NNNN" in that exact
format, e.g. "Fixes #1234" (see
https://tinyurl.com/auto-closing for more information). Also, please
write a comment on that issue linking back to this pull request once it is
open. -->
Fixes #19329


#### Brief description of what is fixed or changed
Added two methods `_print_dict` and `_print_set` to `ReprPrinter`.

#### Other comments


#### Release Notes

<!-- Write the release notes for this release below. See
https://github.com/sympy/sympy/wiki/Writing-Release-Notes for more information
on how to write release notes. The bot will check your release notes
automatically to see if they are formatted correctly. -->
<!-- BEGIN RELEASE NOTES -->
* printing
  * fixed a bug where `srepr` function would not print dictionary and set properly
<!-- END RELEASE NOTES -->

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()]
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):
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 test_set():
from sympy import srepr
from sympy.abc import x, y
s = {x}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
s = {x}
s = {}
assert srepr(s) == "set()"
s = {x}

@eric-wieser
Copy link
Member

Generally looks good, just a mistake on empty sets

@rational-kunal
Copy link
Contributor Author

rational-kunal commented May 17, 2020

@eric-wieser, should empty dict be printed differently

@eric-wieser
Copy link
Member

eric-wieser commented May 17, 2020

No, empty dict is correct. The concern is that {} in python is an empty dict, not an empty set.

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

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:

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

Copy link
Member

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.

s = {x}
assert srepr(s) == "{Symbol('x')}"
s = {x, y}
assert srepr(s) == "{Symbol('x'), Symbol('y')}"
Copy link
Member

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 sets are not ordered.

Suggested change
assert srepr(s) == "{Symbol('x'), Symbol('y')}"
# set order is arbitrary
assert srepr(s) in (
"{Symbol('x'), Symbol('y')}",
"{Symbol('y'), Symbol('x')}",
)

Copy link
Contributor Author

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
Copy link

codecov bot commented May 17, 2020

Codecov Report

Merging #19346 into master will increase coverage by 0.002%.
The diff coverage is 100.000%.

@@              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               

@eric-wieser
Copy link
Member

CI failure is an unrelated network issue

@oscarbenjamin
Copy link
Contributor

Looks good. Thanks

@oscarbenjamin oscarbenjamin merged commit 3e33c30 into sympy:master May 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

srepr not printing dict and set properly
5 participants