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

Fix bad library uses of var() #18084

Closed
jdemeyer opened this issue Mar 29, 2015 · 22 comments
Closed

Fix bad library uses of var() #18084

jdemeyer opened this issue Mar 29, 2015 · 22 comments

Comments

@jdemeyer
Copy link
Contributor

Sage library code should not use var()!

Depends on #18110

Component: symbolics

Author: Jeroen Demeyer

Branch/Commit: 52c15f5

Reviewer: Ralf Stephan, Karl-Dieter Crisman

Issue created by migration from https://trac.sagemath.org/ticket/18084

@jdemeyer jdemeyer added this to the sage-6.6 milestone Mar 29, 2015
@nbruin
Copy link
Contributor

nbruin commented Mar 29, 2015

comment:1

I assume you mean sage.calculus.calculus.var.var. Searching the source for calculus.var shows that the only files at risk for doing so are:

calculus/calculus.py:1532:        sage: sage.calculus.calculus.var_cmp(x,x)
calculus/calculus.py:1534:        sage: sage.calculus.calculus.var_cmp(x,var('z'))
calculus/calculus.py:1536:        sage: sage.calculus.calculus.var_cmp(x,var('a'))
calculus/desolvers.py:1162:        from sage.calculus.var import var
misc/sageinspect.py:188:       'sage/calculus/var.pyx'
crypto/lwe.py:99:from sage.calculus.var import var
symbolic/random_tests.py:275:    vars = [(1.0, sage.calculus.calculus.var('v%d' % (n+1))) for n in range(nvars)]
matroids/catalog.py:46:from sage.calculus.var import var
geometry/riemannian_manifolds/parametrized_surface3d.py:1508:        from sage.calculus.var import var
geometry/riemannian_manifolds/parametrized_surface3d.py:1621:        from sage.calculus.var import var
combinat/finite_state_machine.py:820:from sage.calculus.var import var
combinat/sf/jack.py:38:from sage.calculus.var import var
combinat/sf/hall_littlewood.py:28:from sage.calculus.var import var
combinat/sf/llt.py:33:from sage.calculus.var import var
combinat/sf/macdonald.py:49:from sage.calculus.var import var

unless someone has gone out of their way to obfuscate the incantation.

@jdemeyer
Copy link
Contributor Author

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Mar 30, 2015

Branch pushed to git repo; I updated commit sha1. New commits:

f680be3Use SR.var()

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Mar 30, 2015

Commit: f680be3

@jdemeyer jdemeyer changed the title Fix bad library uses if var() Fix bad library uses of var() Mar 30, 2015
@rwst
Copy link
Contributor

rwst commented Mar 30, 2015

Reviewer: Ralf Stephan

@rwst
Copy link
Contributor

rwst commented Mar 30, 2015

comment:5

Looks good and passes tests in specific main categories.

@kcrisman
Copy link
Member

comment:6
             sage: Sym = SymmetricFunctions(FractionField(QQ['t'])).macdonald()
             Traceback (most recent call last):
             ...
-            ValueError: parameter q must be in the base ring
+            TypeError: unable to convert string

Is this a regression in terms of the error message, an improvement, or just a change? To me it seems more confusing but maybe that's an appropriate message for users of this material.

@jdemeyer
Copy link
Contributor Author

comment:7

Replying to @kcrisman:

             sage: Sym = SymmetricFunctions(FractionField(QQ['t'])).macdonald()
             Traceback (most recent call last):
             ...
-            ValueError: parameter q must be in the base ring
+            TypeError: unable to convert string

Is this a regression in terms of the error message, an improvement, or just a change?

Probably all three. In any case, the code is more clear, the exception type (TypeError instead of ValueError) is surely better. The error message is less clear, but that's something which can be fixed independent of this ticket.

(As a personal pet peeve, I really dislike excessive exception raising code. Moreover, I prefer a good traceback over a good error message.)

@jdemeyer
Copy link
Contributor Author

comment:8

@kcrisman: I can improve the "unable to convert string" error message on a new ticket, but only if somebody (you?) commits to reviewing it.

@kcrisman
Copy link
Member

comment:9

Until the end of the semester I can commit to nearly nothing, unfortunately, and in any case I don't know that code at all so I'd be reluctant to say what a "good" message would be :-(

@jdemeyer
Copy link
Contributor Author

comment:10

Replying to @kcrisman:

Until the end of the semester I can commit to nearly nothing, unfortunately, and in any case I don't know that code at all so I'd be reluctant to say what a "good" message would be :-(

Would you be willing to just accept the change on this ticket then?

@kcrisman
Copy link
Member

comment:11

Moreover, I prefer a good traceback over a good error message.

Well, most people who don't know programming would probably prefer the opposite.

Actually, upon reading the code I think that this is definitely a regression. The only reason it complains about a string is because one doesn't do var('q'); surely there is a way to check whether the string 'q' will be a variable in the given ring. I agree that the way the original code was written is not optimal with such a default, but given that the documentation does directly refer to a parameter q (and t) this change is not good. Surely there is some way to at least use

self.q = Sym.base_ring()(q)

or something like it to try to convert the string and then give a sensible error if it's not possible, since this is clearly how it's designed. The new error message makes it sound like you aren't supposed to use a string, which is manifestly not the case - especially since the string is the default!

That said, I am happy with anything that raises an error that the user can actually understand, so I'm not suggesting one has to go back to the previous. Perhaps

try:
    self.q, self.t do their thing
except TypeError:
    do something with SR.var

and then all other errors are as they should be, instead of raising the message error as previously.

@kcrisman
Copy link
Member

Changed reviewer from Ralf Stephan to Ralf Stephan, Karl-Dieter Crisman

@jdemeyer
Copy link
Contributor Author

comment:12

Replying to @kcrisman:

Moreover, I prefer a good traceback over a good error message.

Well, most people who don't know programming would probably prefer the opposite.

I get your point. In Python 2, this is difficult to get right. In Python 3, there is https://www.python.org/dev/peps/pep-3134/.

@jdemeyer
Copy link
Contributor Author

jdemeyer commented Apr 2, 2015

Dependencies: #18110

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Apr 2, 2015

Changed commit from f680be3 to 52c15f5

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Apr 2, 2015

Branch pushed to git repo; I updated commit sha1. New commits:

abc5a70Improve "unable to convert string" error message
52c15f5Merge branch 't/18110/improve__unable_to_convert_string__error_message' into t/18084/fix_bad_library_uses_if_var__

@jdemeyer
Copy link
Contributor Author

jdemeyer commented Apr 2, 2015

comment:16

The error message is now

TypeError: unable to evaluate 'q' in Fraction Field of Univariate Polynomial Ring in t over Rational Field

@kcrisman
Copy link
Member

kcrisman commented Apr 2, 2015

comment:17

The change message here lgtm, thanks for updating that. I'll look briefly at #18110 as well.

@kcrisman
Copy link
Member

kcrisman commented Apr 2, 2015

comment:18

I guess that was all I needed to review - if it still passes tests I have no further objections.

@jdemeyer
Copy link
Contributor Author

jdemeyer commented Apr 2, 2015

comment:19

OK, so positive_review modulo #18110.

@vbraun
Copy link
Member

vbraun commented Apr 14, 2015

Changed branch from u/jdemeyer/fix_bad_library_uses_if_var__ to 52c15f5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants