-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
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
Use dont_inherit arg to compile in test runner #15587
Conversation
✅ Hi, I am the SymPy bot (v142). 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.4. 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. |
0fedcf2
to
0ffa793
Compare
0ffa793
to
0b889ec
Compare
Thanks for your attention to these details. This looks good to me. Let's see if there are other comments. |
Looking more closely at this patch I think maybe it disables enhanced asserts. Are they used? |
Ironically the motivation from my perspective for this and the other patches is to make it possible to use pytest with sympy. Enhanced asserts are one of the major features of pytest. Having just tested them pytest's assert rewriting is significantly better than the bin/test version. |
I use them sometimes. I suppose I could just switch to pytest when I need them, though I often find pytest's enhanced asserts annoying. |
Due to pytest-dev/pytest#2256 |
I think it's possible to rewrite this to fix the integer division issue only. I could change it back to using compile/exec but pass The wouldn't detect problems with encoding but those are less of an issue anyway. |
Also pytest's assert rewriting is better in other ways. If I add this to def test_stuff():
eqs_sols = {
'eq1': (Eq(f(x).diff(x), f(x)), Eq(f(x), C0*exp(x))),
'eq1': (Eq(f(x).diff(x), 2*f(x)), Eq(f(x), C0*exp(x)))
}
for eqname, (eq, sol) in eqs_sols.items():
assert dsolve(eq) == sol Now run with $ bin/test -E sympy/solvers/tests/test_ode.py -k test_stuff
...
Traceback (most recent call last):
File "/Users/enojb/current/sympy/sympy/sympy/solvers/tests/test_ode.py", line 33, in test_stuff
assert dsolve(eq) == sol
AssertionError:
Eq(f(x), C1*exp(2*x)) ==
Eq(f(x), C0*exp(x))
... Under $ pytest sympy/solvers/tests/test_ode.py -k test_stuff
...
def test_stuff():
eqs_sols = {
'eq1': (Eq(f(x).diff(x), f(x)), Eq(f(x), C0*exp(x))),
'eq1': (Eq(f(x).diff(x), 2*f(x)), Eq(f(x), C0*exp(x)))
}
for eqname, (eq, sol) in eqs_sols.items():
> assert dsolve(eq) == sol
E assert Eq(f(x), C1*exp(2*x)) == Eq(f(x), C0*exp(x))
E + where Eq(f(x), C1*exp(2*x)) = dsolve(Eq(Derivative(f(x), x), 2*f(x)))
sympy/solvers/tests/test_ode.py:33: AssertionError
... If I could see that kind of output on Travis I might now the problem with a patch without needing to have run the code on my machine. |
206133b
to
a57911d
Compare
I've written a new patch that uses the This means that enhanced asserts still work and the test runner will pick up problems with integer division when running on Python 2.7. This version of the patch won't pick up problems with encoding declarations but that's not such a problem anyway. |
The division issue seems to be due to |
Those are only used for the doctests. For the normal tests |
+1 for the changes relating to unit tests. |
New division bugs are added to sympy continuously. Here's two from two different PRs both merged 2 days ago: https://github.com/sympy/sympy/pull/15646/files#diff-da2e1fbc257d693653530689558aa08eR94 These show up when running the tests under pytest with Python 2.7. I would like to make it so that pytest is usable with the SymPy tests but something needs to be changed to stop new division bugs from arriving. I see two possibilities for this:
My preference is this patch because it seems clear to me that these test fails are showing unintended mistakes. Looking at the two examples above I'm fairly sure that what is intended is e.g. |
It's worth noting that we will drop Python 2 support in about a year https://github.com/sympy/sympy/wiki/Python-version-support-policy. But it would still be nice to have the tests somehow avoid usage of explicit integer division. We could even consider a test that tests that nowhere does explicit integer/integer (require it to always be wrapped as a rational or require one of them to be a float). |
Python 2.7 has a specific command-line option to warn about integer division: $ python -Qwarn
Python 2.7.10 (default, Oct 6 2017, 22:29:07)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 2/3
__main__:1: DeprecationWarning: classic int division
0 It's been removed in Python 3 though. I don't see any way to pick up on integer division in general without hooking into x = 1
y = 2
assert x / y == z |
I guess a big part of the problem is that things like |
I meant S(1)/5 and so on; sorry about being sloppy and Python3-centric. |
a57911d
to
43cf7ce
Compare
I've rebased and pushed another couple of commits to address the new division bugs. I'm happy with this PR now. No need to be sorry @normalhuman: if you look at #15515 you can see that these were ubiquitous in the codebase and coming from all contributors. This mistake is too easy to be made because it is also made by >>> x ** (S(1)/3)
x**(1/3)
>>> repr(x ** (S(1)/3))
'x**(1/3)' So any time someone copies the |
See sympy#15522. Using compile/exec implicitly injects from __future__ import division. The dont_inherit flag can be used to prevent this.
43cf7ce
to
1bd2b3a
Compare
Codecov Report
@@ Coverage Diff @@
## master #15587 +/- ##
===========================================
+ Coverage 61.6% 73.18% +11.57%
===========================================
Files 618 618
Lines 157812 157812
Branches 37127 37127
===========================================
+ Hits 97220 115494 +18274
+ Misses 54706 36789 -17917
+ Partials 5886 5529 -357
Continue to review full report at Codecov.
|
References to other Issues or PRs
Fixes the test runner to avoid the situations seen in #15522 , #15579 , #15515
Brief description of what is fixed or changed
The test runner is changed to useimportlib.import_module
rather thancompile/exec
. This means that each test file is compiled as it would be if directly imported in animport
statement.The previous compile/exec code for this treated all files as utf8 even if no encoding declaration was present. This meant that the tests would pass under PY2 even if the test module was not normally importable.EDIT: I've changed this patch and force-pushed. The new changes are: Pass
flag=0, no_inherit=True
arguments to compile in the test runner.This means that test modules won't inherit the
__future__
flags of thesympy.utilities.runtests
module. The problem with files having no encoding declaration is unsolved by the latest version of this patch but that's not such a big problem anyway.Previously
compile
implicitly injects the__future__
flags from the calling module into the code that is compiled. This means that all tests are implicitly run withfrom __future__ import division
when under the test runner. This also can mean that the result of the test is different when run under the test runner compare to when imported and called directly.I found a number of other integer division bugs in the tests as a result of this. The first commit is the changes to the test runner. The others are fixes to integer division in various test files.
Other comments
Release Notes
__future__
division so that the test behaviour is the same under the runner as when imported normally.