Repository: dwavesystems/dwave-cloud-client

Issue:
Fix docs for order_by
In [`get_solver()`](https://github.com/dwavesystems/dwave-cloud-client/blob/a178c2f40f3d107c5b7e2a7ae43602fb24046016/dwave/cloud/client.py#L1055).
Unexpected behavior in order_by
```
>>> from dwave.cloud import Client
>>> client = Client.from_config(solver=dict(qpu=True))
>>> client.get_solver()
StructuredSolver(id='DW_2000Q_6')
>>> client.get_solver(order_by='-num_active_qubits')
UnstructuredSolver(id='hybrid_v1')
>>> client.get_solver(order_by='-num_active_qubits', qpu=True)
StructuredSolver(id='DW_2000Q_6')
```
I would have expected to see `StructuredSolver(id='DW_2000Q_6')` in all three examples, given `client = Client.from_config(qpu=True)`

Discussion:

Patch:
diff --git a/dwave/cloud/client.py b/dwave/cloud/client.py
index 05bd128..1c2df0d 100644
--- a/dwave/cloud/client.py
+++ b/dwave/cloud/client.py
@@ -697,10 +697,10 @@ class Client(object):
                 Force refresh of cached list of solvers/properties.
 
             order_by (callable/str/None, default='avg_load'):
-                Solver sorting key function (or :class:`Solver` attribute/item
-                dot-separated path). By default, solvers are sorted by average
-                load. To explicitly not sort the solvers (and use the API-returned
-                order), set ``order_by=None``.
+                Solver sorting key function (or :class:`~dwave.cloud.solver.Solver`
+                attribute/item dot-separated path). By default, solvers are sorted
+                by average load. To explicitly not sort the solvers (and use the
+                API-returned order), set ``order_by=None``.
 
                 Signature of the `key` `callable` is::
 
@@ -1035,7 +1035,7 @@ class Client(object):
         warnings.warn("'solvers' is deprecated in favor of 'get_solvers'.", DeprecationWarning)
         return self.get_solvers(refresh=refresh, **filters)
 
-    def get_solver(self, name=None, refresh=False, **filters):
+    def get_solver(self, name=None, refresh=False, order_by='avg_load', **filters):
         """Load the configuration for a single solver.
 
         Makes a blocking web call to `{endpoint}/solvers/remote/{solver_name}/`, where `{endpoint}`
@@ -1052,13 +1052,15 @@ class Client(object):
                 Dictionary of filters over features this solver has to have. For a list of
                 feature names and values, see: :meth:`~dwave.cloud.client.Client.get_solvers`.
 
-            order_by (callable/str, default='id'):
-                Solver sorting key function (or :class:`Solver` attribute name).
-                By default, solvers are sorted by ID/name.
+            order_by (callable/str/None, default='avg_load'):
+                Solver sorting key function (or :class:`~dwave.cloud.solver.Solver`
+                attribute/item dot-separated path). By default, solvers are sorted by average
+                load. For details, see :meth:`~dwave.cloud.client.Client.get_solvers`.
 
             refresh (bool):
-                Return solver from cache (if cached with ``get_solvers()``),
-                unless set to ``True``.
+                Return solver from cache (if cached with
+                :meth:`~dwave.cloud.client.Client.get_solvers`), unless set to
+                ``True``.
 
         Returns:
             :class:`.Solver`
@@ -1096,7 +1098,7 @@ class Client(object):
         # get the first solver that satisfies all filters
         try:
             logger.debug("Fetching solvers according to filters=%r", filters)
-            return self.get_solvers(refresh=refresh, **filters)[0]
+            return self.get_solvers(refresh=refresh, order_by=order_by, **filters)[0]
         except IndexError:
             raise SolverNotFoundError("Solver with the requested features not available")
 


Test patch:
diff --git a/tests/test_client.py b/tests/test_client.py
index 12f1726..e5536aa 100644
--- a/tests/test_client.py
+++ b/tests/test_client.py
@@ -717,6 +717,19 @@ class FeatureBasedSolverSelection(unittest.TestCase):
         with self.assertRaises(TypeError):
             self.client.get_solvers(order_by=list)
 
+    def test_order_by_respects_default_solver(self):
+        """order_by used in isolation should not affect default_solver filters (issue #401)"""
+
+        with Client('endpoint', 'token', solver=dict(name='qpu2')) as client:
+            # mock the network call to fetch all solvers
+            client._fetch_solvers = lambda **kw: self.solvers
+
+            # the default solver is set on client
+            self.assertEqual(client.get_solver(), self.qpu2)
+
+            # the default solver should not change when we add order_by
+            self.assertEqual(client.get_solver(order_by='id'), self.qpu2)
+
     def test_order_by_string(self):
         # sort by Solver inferred properties
         self.assertEqual(self.client.get_solvers(order_by='id'), [self.hybrid, self.qpu1, self.qpu2, self.software])