-
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
SymPy numbers now inherit from Python numbers ABCs #16652
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
SymPy numbers now inherit from Python numbers ABCs #16652
Conversation
|
✅ Hi, I am the SymPy bot (v147). 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.5. 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. |
Sc0rpi0n101
left a comment
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 patch fails the build tests.
Not sure why #33122.12 failed
But you can try my suggestions and see if the tests pass
sympy/core/numbers.py
Outdated
|
|
||
| import mpmath | ||
| import mpmath.libmp as mlib | ||
| import numbers |
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.
| import numbers | |
| import numbers as num |
You might want to import it as num since you are working in a module of the same name
sympy/core/numbers.py
Outdated
| pass | ||
|
|
||
| class Number(AtomicExpr): | ||
| class Number(with_metaclass(ManagedABCMetaclass, AtomicExpr, numbers.Number)): |
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.
Try if this passes the tests
| class Number(with_metaclass(ManagedABCMetaclass, AtomicExpr, numbers.Number)): | |
| class Number(with_metaclass(ManagedABCMetaclass, AtomicExpr, num.Number)): |
Maybe the build errors are because python thinks you are referencing sympy.code.number.Numbers inside the definition of the same, so the module cannot have Number as it is not defined yet.
sympy/core/numbers.py
Outdated
|
|
||
|
|
||
| class Float(Number): | ||
| class Float(with_metaclass(ManagedABCMetaclass, Number, numbers.Real)): |
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.
| class Float(with_metaclass(ManagedABCMetaclass, Number, numbers.Real)): | |
| class Float(with_metaclass(ManagedABCMetaclass, Number, num.Real)): |
Same here
sympy/core/numbers.py
Outdated
|
|
||
|
|
||
| class Rational(Number): | ||
| class Rational(with_metaclass(ManagedABCMetaclass, Number, numbers.Rational)): |
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.
| class Rational(with_metaclass(ManagedABCMetaclass, Number, numbers.Rational)): | |
| class Rational(with_metaclass(ManagedABCMetaclass, Number, num.Rational)): |
And here
Codecov Report
@@ Coverage Diff @@
## master #16652 +/- ##
============================================
+ Coverage 73.79% 73.852% +0.061%
============================================
Files 619 619
Lines 159222 159591 +369
Branches 37366 37454 +88
============================================
+ Hits 117491 117862 +371
+ Misses 36297 36272 -25
- Partials 5434 5457 +23 |
>>> import numbers as nums
>>> nums.Number
<class 'numbers.Number'>
>>> class abc(nums.Number):
... print ("hello")
...
helloHow is the build getting an attribute error? well, you can try |
|
Well, I tested the module core and it passed the tests. For testing in sympy, you can check: or you can use pytest |
|
|
tests finished: 8166 passed, 226 skipped, 354 expected to fail, 2 expected to fail but passed, Actually the tests didn't even start for the fails, the build failed before that. |
|
Was Number not in python 2.7? |
|
Yeah, the 2.7 docs have numbers.Number as a base class. |
Sc0rpi0n101
left a comment
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.
Can you try changing these and see if that work.
>>> from sympy import *
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "sympy/__init__.py", line 57, in <module>
from .core import *
File "sympy/core/__init__.py", line 8, in <module>
from .expr import Expr, AtomicExpr, UnevaluatedExpr
File "sympy/core/expr.py", line 3616, in <module>
from .mul import Mul
File "sympy/core/mul.py", line 1805, in <module>
from .numbers import Rational
File "sympy/core/numbers.py", line 492, in <module>
class Number(with_metaclass(ManagedABCMetaclass, AtomicExpr, nums.Number)):
AttributeError: 'module' object has no attribute 'Number'I'm getting this in 2.7 on my machine too, did not get it in 3.7
Might be that using nums.Number in class definition was not supported. Not sure.
You can accept the changes in the review itself if it saves some time,
or we can wait for sylee
sympy/core/numbers.py
Outdated
|
|
||
| import mpmath | ||
| import mpmath.libmp as mlib | ||
| import numbers as nums |
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.
| import numbers as nums | |
| from sympy.external import import_module | |
| nums = import_module('numbers') |
|
I think it would be better to register the classes with the abcs, similar to how numpy does it, rather than directly subclassing. I'm not sure about bitwise operations. It could cause confusion with the logic operations that are defined for those operators. We do currently raise a TypeError when numbers other than 0 or 1 are used with boolean functions, so it isn't strictly an issue. See also #5353. |
|
I think the reason of python 2 failing, is that Line 22 in 47bef27
looking up the module sympy.core.numbers instead of python builtin package.
You can inspect what's going on, if you use Adding from sympy.external import import_module
nums = import_module('numbers')or anything you can use to load correct module. Though I have not much knowledge, about the idea from @asmeurer though. |
Thanks, @sylee957 Still, have to see if it works for Travis |
|
This time one of the build tests failed due to HTTP error. |
|
If we do go with subclassing instead of adding them to the list externally, we should determine if there is any performance impact (see https://github.com/sympy/sympy_benchmarks). |
How can we do that? |
|
Apparently we need instructions in the README for that repo. You should set up |
I'm a little confused about how to use asv to check performance. I'm cloning sympy_benchmark and using that? |
|
Yes. You'll need to edit the json file to change the repo from |
I can't find this commit in our commit history, I'm not sure why |
|
You have to run Also, instead of |
|
Thanks @asmeurer, that worked! I guess it might make more sense to add them to the list externally instead of subclassing. What do you guys think? |
|
I think it makes sense. |
|
@asmeurer we did the register thing like numpy. Does this look good to go? |
|
Can you add tests that the numbers ABCs work (like |
|
I wonder how the new implementation improves the performance. Do you have any benchmark result? |
|
@sylee957 is there anything else we need to do for this PR? |
sympy/core/numbers.py
Outdated
| nums.Rational.register(Rational) | ||
| nums.Rational.register(Integer) | ||
|
|
||
| _register_classes(); |
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.
Remove the semicolon from the end of the line.
sympy/core/numbers.py
Outdated
| from sympy.core.cache import lru_cache | ||
|
|
||
| from sympy.external import import_module | ||
| nums = import_module('numbers') |
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.
Is there a reason this isn't just import numbers?
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.
Yep, it fails on 2.7 due to name collision.
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.
Oh I see. I think it should work if you add absolute_import to the __future__ import at the top of the file.
|
A couple of minor comments. The rest looks good. |
…arkereberry1923/sympy into Issue-12134-Python-Numbers-ABC
|
@asmeurer Done! Thank you for telling me about the import fix. |
|
Thank you. This looks good now. I updated the release notes since we aren't literally subclassing anymore. |







References to other Issues or PRs
Fixes #12134
Brief description of what is fixed or changed
Other comments
Release Notes
isinstancefor Python numeric ABCs now works for the basic SymPy number types.