Repository: sympy/sympy

Issue:
Solve: GeneratorsNeeded with system involving constant equation
I don't think solve should convert all equations to Poly because it can be slow but at least there should be some checking for this case:
```python
>>> from sympy import *
>>> from sympy.abc import x, y
>>> solve([x + y, sqrt(2)], [x, y])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/enojb/current/sympy/sympy/sympy/solvers/solvers.py", line 1099, in solve
    solution = _solve_system(f, symbols, **flags)
  File "/Users/enojb/current/sympy/sympy/sympy/solvers/solvers.py", line 1774, in _solve_system
    subsol = _solve_system(subexpr, subsyms, **flags)
  File "/Users/enojb/current/sympy/sympy/sympy/solvers/solvers.py", line 1805, in _solve_system
    poly = g.as_poly(*symbols, extension=True)
  File "/Users/enojb/current/sympy/sympy/sympy/core/expr.py", line 1109, in as_poly
    poly = Poly(self, *gens, **args)
  File "/Users/enojb/current/sympy/sympy/sympy/polys/polytools.py", line 162, in __new__
    return cls._from_expr(rep, opt)
  File "/Users/enojb/current/sympy/sympy/sympy/polys/polytools.py", line 292, in _from_expr
    return cls._from_dict(rep, opt)
  File "/Users/enojb/current/sympy/sympy/sympy/polys/polytools.py", line 229, in _from_dict
    raise GeneratorsNeeded(
sympy.polys.polyerrors.GeneratorsNeeded: can't initialize from 'dict' without generators
```

Discussion:
sympy-bot: :white_check_mark:

Hi, I am the [SymPy bot](https://github.com/sympy/sympy-bot) (v161). I'm here to help you write a release notes entry. Please read the [guide on how to write release notes](https://github.com/sympy/sympy/wiki/Writing-Release-Notes).



Your release notes are in good order.

Here is what the release notes will look like:
* polys
  * The `poly` function and `as_poly` methods now return `None` instead of raising `GeneratorsNeeded` for constant expressions when no generators are given. ([#20773](https://github.com/sympy/sympy/pull/20773) by [@kc611](https://github.com/kc611))

* solvers
  * Fixed a bug causing `solve` to raise `GeneratorsNeeded` when an equation such as `sqrt(2)` is included in a system. ([#20773](https://github.com/sympy/sympy/pull/20773) by [@kc611](https://github.com/kc611))

This will be added to https://github.com/sympy/sympy/wiki/Release-Notes-for-1.8.

<details><summary>Click here to see the pull request description that was parsed.</summary>

    <!-- 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 #20610 

    #### Brief description of what is fixed or changed

    Continuation from #20661 . Starting from a new branch due to rebasing issues 

    #### 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
        * The `poly` function and `as_poly` methods now return `None` instead of raising `GeneratorsNeeded` for constant expressions when no generators are given.
    * solvers
        * Fixed a bug causing `solve` to raise `GeneratorsNeeded` when an equation such as `sqrt(2)` is included in a system.
    
    <!-- END RELEASE NOTES -->

</details><p>

oscarbenjamin: This still has 5 commits instead of 1.

I think maybe your master branch has merge commits in it and those are showing up because you started the PR branch from your master rather than upstream master.

I will fix this for you in this PR and push to it myself.

Patch:
diff --git a/sympy/core/expr.py b/sympy/core/expr.py
index fb010a5331..388ca0b457 100644
--- a/sympy/core/expr.py
+++ b/sympy/core/expr.py
@@ -1127,7 +1127,7 @@ def as_poly(self, *gens, **args):
         None
 
         """
-        from sympy.polys.polyerrors import PolynomialError
+        from sympy.polys.polyerrors import PolynomialError, GeneratorsNeeded
         from sympy.polys.polytools import Poly
 
         try:
@@ -1137,7 +1137,9 @@ def as_poly(self, *gens, **args):
                 return None
             else:
                 return poly
-        except PolynomialError:
+        except (PolynomialError, GeneratorsNeeded):
+            # PolynomialError is caught for e.g. exp(x).as_poly(x)
+            # GeneratorsNeeded is caught for e.g. S(2).as_poly()
             return None
 
     def as_ordered_terms(self, order=None, data=False):


Test patch:
diff --git a/sympy/core/tests/test_expr.py b/sympy/core/tests/test_expr.py
index bbf171089a..e744a7dfd5 100644
--- a/sympy/core/tests/test_expr.py
+++ b/sympy/core/tests/test_expr.py
@@ -1172,6 +1172,10 @@ def test_as_poly_as_expr():
 
     assert p.as_poly() == p
 
+    # https://github.com/sympy/sympy/issues/20610
+    assert S(2).as_poly() is None
+    assert sqrt(2).as_poly(extension=True) is None
+
     raises(AttributeError, lambda: Tuple(x, x).as_poly(x))
     raises(AttributeError, lambda: Tuple(x ** 2, x, y).as_poly(x))
 
diff --git a/sympy/solvers/tests/test_solvers.py b/sympy/solvers/tests/test_solvers.py
index d32e2eb05a..4aa9da9140 100644
--- a/sympy/solvers/tests/test_solvers.py
+++ b/sympy/solvers/tests/test_solvers.py
@@ -128,6 +128,7 @@ def test_solve_args():
     # no symbol to solve for
     assert solve(42) == solve(42, x) == []
     assert solve([1, 2]) == []
+    assert solve([sqrt(2)],[x]) == []
     # duplicate symbols removed
     assert solve((x - 3, y + 2), x, y, x) == {x: 3, y: -2}
     # unordered symbols