Repository: sympy/sympy
Pull request: Correct the -oo case in degree.
<!-- Your title above should be a short description of what
was changed. Do not include the issue number in the title. -->

#### References to other Issues or PRs
<!-- If this pull request fixes an issue, write "Fixes #NNNN" in that exact
format, e.g. "Fixes #1234" (see
https://tinyurl.com/auto-closing for more information). Also, please
write a comment on that issue linking back to this pull request once it is
open. -->

Fixes #20389

#### Brief description of what is fixed or changed

Added a check in `polytools.degree` to see if the polygon degree is infinite, and provide the correct result.

#### Other comments


#### Release Notes

<!-- Write the release notes for this release below. See
https://github.com/sympy/sympy/wiki/Writing-Release-Notes for more information
on how to write release notes. The bot will check your release notes
automatically to see if they are formatted correctly. -->

<!-- BEGIN RELEASE NOTES -->

* polys
    * Solve a bug in polytools.degree.

<!-- END RELEASE NOTES -->

Problem statement:
TypeError: Argument of Integer should be of numeric type, got -oo.
The present expression generates the previous error.

```python
sp.degree(sp.sympify('_R*_X34*(_X35**2*_X26 - _X26*_X35*(_X35 + 1) + 2*_X26*_X35 - _X26*(_X35 + 1) + _X26 - _X29*_X32*_X35 + _X29*_X32*(_X35 + 1) - _X29*_X32)'), sp.Symbol('_R'))
```

The return statement does not take into account the `p.degree(gen)` returns and -oo.

Hints from issue discussion:
A simpler way to reproduce this:
```python
In [32]: degree(x*(x + 1) - x**2 - x, x)                                                                                                                      
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~/current/sympy/sympy/sympy/core/numbers.py in __new__(cls, i)
   2101         try:
-> 2102             ival = int(i)
   2103         except TypeError:

~/current/sympy/sympy/sympy/core/expr.py in __int__(self)
    323         if r in (S.NaN, S.Infinity, S.NegativeInfinity):
--> 324             raise TypeError("can't convert %s to int" % r)
    325         i = int(r)

TypeError: can't convert -oo to int
...
TypeError: Argument of Integer should be of numeric type, got -oo.
```

Commit message: Solve and check the issue #20389

Added a check to see if the polygon degree is infinite, and provide the correct result.

Patch to review:
diff --git a/sympy/polys/polytools.py b/sympy/polys/polytools.py
index 70112de3b65f..c4a9c0b13e42 100644
--- a/sympy/polys/polytools.py
+++ b/sympy/polys/polytools.py
@@ -4480,8 +4480,8 @@ def degree(f, gen=0):
          expression like func = %s, e.g. degree(func, gen = %s) instead of
          degree(func, gen = %s).
         ''' % (f, next(ordered(f.free_symbols)), gen)))
-
-    return Integer(p.degree(gen))
+    result = p.degree(gen)
+    return Integer(result) if result is int else S.NegativeInfinity
 
 
 @public
diff --git a/sympy/polys/tests/test_polytools.py b/sympy/polys/tests/test_polytools.py
index c774fc41e294..48916f4afbd8 100644
--- a/sympy/polys/tests/test_polytools.py
+++ b/sympy/polys/tests/test_polytools.py
@@ -3441,3 +3441,8 @@ def test_deserialized_poly_equals_original():
         "Deserialized polynomial not equal to original.")
     assert poly.gens == deserialized.gens, (
         "Deserialized polynomial has different generators than original.")
+
+
+def test_issue_20389():
+    result = degree(x * (x + 1) - x ** 2 - x, x)
+    assert result == -oo


Code review comments:
diff_hunk: @@ -4480,8 +4480,8 @@ def degree(f, gen=0):
          expression like func = %s, e.g. degree(func, gen = %s) instead of
          degree(func, gen = %s).
         ''' % (f, next(ordered(f.free_symbols)), gen)))
-
-    return Integer(p.degree(gen))
+    result = p.degree(gen)
+    return Integer(result) if result is int else S.NegativeInfinity
path: sympy/polys/polytools.py
text: @user1:
`result is int` doesn't check if result is an integer. This should be
```suggestion
    return Integer(result) if isinstance(result, int) else S.NegativeInfinity
```

Although it would be simpler to just return a SymPy type, like `return sympify(result)`. 

@user1:
Actually it would make more sense for Poly.degree to return an `Integer`. It already returns -oo, so I think it should always return a SymPy type. Then no conversion would be needed in this function. 

@author:
You are right. Corrected the isinstance. I have left the hardcoded conversion to reduce overhead.