Repository: sympy/sympy

Issue:
Limit can fail if an expression contains a zero=True symbol
    >>> from sympy import *
    >>> n = Symbol('n')
    >>> p = Symbol('p', zero=True)
    >>> limit(n+p, n, 0)
    Traceback (most recent call last):
      File "<pyshell#3>", line 1, in <module>
        limit(n+p, n, 0)
      File "C:\Users\E\AppData\Local\Programs\Python\Python36\lib\site-packages\sympy\series\limits.py", line 49, in limit
        return Limit(e, z, z0, dir).doit(deep=False)
      File "C:\Users\E\AppData\Local\Programs\Python\Python36\lib\site-packages\sympy\series\limits.py", line 180, in doit
        r = gruntz(e, z, z0, dir)
      File "C:\Users\E\AppData\Local\Programs\Python\Python36\lib\site-packages\sympy\series\gruntz.py", line 658, in gruntz
        r = limitinf(e0, z)
      File "C:\Users\E\AppData\Local\Programs\Python\Python36\lib\site-packages\sympy\core\cache.py", line 93, in wrapper
        retval = cfunc(*args, **kwargs)
      File "C:\Users\E\AppData\Local\Programs\Python\Python36\lib\site-packages\sympy\series\gruntz.py", line 428, in limitinf
        c0, e0 = mrv_leadterm(e, x)
      File "C:\Users\E\AppData\Local\Programs\Python\Python36\lib\site-packages\sympy\core\cache.py", line 93, in wrapper
        retval = cfunc(*args, **kwargs)
      File "C:\Users\E\AppData\Local\Programs\Python\Python36\lib\site-packages\sympy\series\gruntz.py", line 513, in mrv_leadterm
        series = calculate_series(f, w, logx=logw)
      File "C:\Users\E\AppData\Local\Programs\Python\Python36\lib\site-packages\sympy\series\gruntz.py", line 466, in calculate_series
        for t in e.lseries(x, logx=logx):
      File "C:\Users\E\AppData\Local\Programs\Python\Python36\lib\site-packages\sympy\core\expr.py", line 2645, in yield_lseries
        o = Order(si, x)*x
      File "C:\Users\E\AppData\Local\Programs\Python\Python36\lib\site-packages\sympy\core\cache.py", line 93, in wrapper
        retval = cfunc(*args, **kwargs)
      File "C:\Users\E\AppData\Local\Programs\Python\Python36\lib\site-packages\sympy\series\order.py", line 213, in __new__
        lst = expr.extract_leading_order(args)
      File "C:\Users\E\AppData\Local\Programs\Python\Python36\lib\site-packages\sympy\core\cache.py", line 93, in wrapper
        retval = cfunc(*args, **kwargs)
      File "C:\Users\E\AppData\Local\Programs\Python\Python36\lib\site-packages\sympy\core\add.py", line 783, in extract_leading_order
        if o.contains(of) and o != of:
    AttributeError: 'Zero' object has no attribute 'contains'

Discussion:

Patch:
diff --git a/sympy/core/add.py b/sympy/core/add.py
index eca10ae1c6..4c3ca8b1fc 100644
--- a/sympy/core/add.py
+++ b/sympy/core/add.py
@@ -123,6 +123,8 @@ def flatten(cls, seq):
 
             # O(x)
             if o.is_Order:
+                if o.expr.is_zero:
+                    continue
                 for o1 in order_factors:
                     if o1.contains(o):
                         o = None
diff --git a/sympy/series/order.py b/sympy/series/order.py
index f4d9c68cfe..362edd4ade 100644
--- a/sympy/series/order.py
+++ b/sympy/series/order.py
@@ -257,13 +257,10 @@ def __new__(cls, expr, *args, **kwargs):
 
             expr = expr.subs(rs)
 
-        if expr.is_zero:
-            return expr
-
         if expr.is_Order:
             expr = expr.expr
 
-        if not expr.has(*variables):
+        if not expr.has(*variables) and not expr.is_zero:
             expr = S.One
 
         # create Order instance:


Test patch:
diff --git a/sympy/series/tests/test_limits.py b/sympy/series/tests/test_limits.py
index ec6d61f56f..32e44843a6 100644
--- a/sympy/series/tests/test_limits.py
+++ b/sympy/series/tests/test_limits.py
@@ -636,3 +636,8 @@ def test_issue_18508():
     assert limit(sin(x)/sqrt(1-cos(x)), x, 0) == sqrt(2)
     assert limit(sin(x)/sqrt(1-cos(x)), x, 0, dir='+') == sqrt(2)
     assert limit(sin(x)/sqrt(1-cos(x)), x, 0, dir='-') == -sqrt(2)
+
+def test_issue_13715():
+    n = Symbol('n')
+    p = Symbol('p', zero=True)
+    assert limit(n + p, n, 0) == 0
diff --git a/sympy/series/tests/test_order.py b/sympy/series/tests/test_order.py
index 539dbeb2f7..f98247fd51 100644
--- a/sympy/series/tests/test_order.py
+++ b/sympy/series/tests/test_order.py
@@ -1,6 +1,7 @@
 from sympy import (Symbol, Rational, Order, exp, ln, log, nan, oo, O, pi, I,
     S, Integral, sin, cos, sqrt, conjugate, expand, transpose, symbols,
     Function, Add)
+from sympy.core.expr import unchanged
 from sympy.testing.pytest import raises
 from sympy.abc import w, x, y, z
 
@@ -435,3 +436,6 @@ def test_issue_15539():
     assert O(1/x**4 + exp(x), (x, -oo)) == O(1/x**4, (x, -oo))
     assert O(1/x**4 + exp(-x), (x, -oo)) == O(exp(-x), (x, -oo))
     assert O(1/x, (x, oo)).subs(x, -x) == O(-1/x, (x, -oo))
+
+def test_issue_18606():
+    assert unchanged(Order, 0)