Replace reproducible_repr() with usage of displayhook#39420
Replace reproducible_repr() with usage of displayhook#39420vbraun merged 7 commits intosagemath:developfrom
Conversation
|
Documentation preview for this PR (built with commit d5a22f1; changes) is ready! 🎉 |
tobiasdiez
left a comment
There was a problem hiding this comment.
and the ordering of elements is reversed for multivariate polynomial ring (in R.<x,y,z> = QQ[], z < y < x)
Would it be desired/easy to change this to the more natural ordering x < y < z?
src/sage/doctest/fixtures.py
Outdated
|
|
||
| .. NOTE:: | ||
|
|
||
| This function is mostly superseded by the automatic sorting |
There was a problem hiding this comment.
Should we then mark it as deprecated?
There was a problem hiding this comment.
Not sure if it's necessary. On the other hand it was never used except for one doctesting case, and letting it stay there might serve as a signpost to the correct handles of reproducible dict/set repr. Thoughts?
There was a problem hiding this comment.
If it's no longer used/needed in the sage codebase, then I think it should be deprecated.
There was a problem hiding this comment.
Actually there's a little caveat — if the repr forms a total order but the values themselves do not form a total order then it might be desirable to use this function (e.g. CIF in #39413 ). But I think it's rare enough that you can add an explicit sorted(…, key=[some reliable key]) anyway.
There was a problem hiding this comment.
Also there's still one place where the function is used below it (in trace_method which is also used in doctest):
print("exit {} -> {}".format(meth, reproducible_repr(res)))
I think this usage is slightly more reasonable (it's not much longer than the alternative); on the other hand it can also be replaced with from IPython.lib.pretty import pretty → pretty(res).
Thoughts?
There was a problem hiding this comment.
In general, if we can remove sage-specific things in the basic infrastructure then this is a big win. There is a considerably higher chance that people know about pretty from IPython than that they go hunting for a reproducible_repr method in sage. So if you think all the existing usage can be covered, then do please do so and deprecate the method.
| sage: print(reproducible_repr(sols)) # needs sage.rings.polynomial.pbori | ||
| [{x: 0, y: 1, z: 0}, {x: 1, y: 1, z: 1}] | ||
| sage: sols # needs sage.rings.polynomial.pbori | ||
| [{z: 0, y: 1, x: 0}, {z: 1, y: 1, x: 1}] |
There was a problem hiding this comment.
Why is this here z < y < x while in the other examples a bit down it is still x < y < z?
There was a problem hiding this comment.
Ugh, must be because I don't have the optional packages.
By the way, how can optional packages be installed in meson again? (sage -i doesn't work if I recalled correctly?) Find the corresponding conda package, but how do you know which conda package corresponds to which sage package?
There was a problem hiding this comment.
Find the corresponding conda package, but how do you know which conda package corresponds to which sage package?
There are conda.txt files in build/pkgs that tell you the mapping from sage pkg to conda pkg. (But they are not actively maintained and thus may be incorrect.)
There was a problem hiding this comment.
End up most of the packages needed seems not available on conda anyway (fes ginv macaulay2 magma msolve pycryptosat) so I just try to fix it by best effort.
Basically it is determined by Singular's monomial comparison ( On the other hand we can flip the order we pass variables to Singular, which is technically possible, but it seems rather unnatural and the last time Singular polynomial comparison was changed a bunch of tests need to be fixed. You can also argue that in
If anything we should fix this upstream as well. |
sagemathgh-39420: Replace reproducible_repr() with usage of displayhook * It is not very well-known that doctest has magic features to ignore the ordering of `dict` and `set` (see for example sagemath#39413 (comment) ), I try to make it more discoverable. * Since the displayhook is in place (since 68b10e1), the `reproducible_repr` (added in e68d6eb) is mostly redundant. * Implement `_repr_pretty_` for `KeyConvertingDict` to make doctest deterministic. (based on the framework of sagemath#39027) There's a slight difference between `reproducible_repr` ordering (which always order by string representation) and this function (which tries first to order by element values, then by string representation), and the ordering of elements is reversed for multivariate polynomial ring (in `R.<x,y,z> = QQ[]`, `z < y < x`). However, it should still be deterministic. ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. - [x] I have linked a relevant issue or discussion. - [x] I have created tests covering the changes. - [x] I have updated the documentation and checked the documentation preview. ### ⌛ Dependencies <!-- List all open PRs that this PR logically depends on. For example, --> <!-- - sagemath#12345: short description why this is a dependency --> <!-- - sagemath#34567: ... --> URL: sagemath#39420 Reported by: user202729 Reviewer(s): Tobias Diez, user202729
sagemathgh-39420: Replace reproducible_repr() with usage of displayhook * It is not very well-known that doctest has magic features to ignore the ordering of `dict` and `set` (see for example sagemath#39413 (comment) ), I try to make it more discoverable. * Since the displayhook is in place (since 68b10e1), the `reproducible_repr` (added in e68d6eb) is mostly redundant. * Implement `_repr_pretty_` for `KeyConvertingDict` to make doctest deterministic. (based on the framework of sagemath#39027) There's a slight difference between `reproducible_repr` ordering (which always order by string representation) and this function (which tries first to order by element values, then by string representation), and the ordering of elements is reversed for multivariate polynomial ring (in `R.<x,y,z> = QQ[]`, `z < y < x`). However, it should still be deterministic. ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. - [x] I have linked a relevant issue or discussion. - [x] I have created tests covering the changes. - [x] I have updated the documentation and checked the documentation preview. ### ⌛ Dependencies <!-- List all open PRs that this PR logically depends on. For example, --> <!-- - sagemath#12345: short description why this is a dependency --> <!-- - sagemath#34567: ... --> URL: sagemath#39420 Reported by: user202729 Reviewer(s): Tobias Diez, user202729
dictandset(see for example Fix compilation and doctests with flintlib 3.2 #39413 (comment) ), I try to make it more discoverable.reproducible_repr(added in e68d6eb) is mostly redundant._repr_pretty_forKeyConvertingDictto make doctest deterministic. (based on the framework of Make Sequence pretty-printed in IPython #39027)There's a slight difference between
reproducible_reprordering (which always order by string representation) and this function (which tries first to order by element values, then by string representation), and the ordering of elements is reversed for multivariate polynomial ring (inR.<x,y,z> = QQ[],z < y < x). However, it should still be deterministic.📝 Checklist
⌛ Dependencies