Can't iterate NoneType using offset and count on search method

Hello,

I found this case where I’m building a web application and I write the search method params on the application and then parse them on the server and use all of them in a query like

sales = Sale.search(domain, offset=offset, order=order, limit=limit, count=count)

I’m querying the route that uses this method from a page that requires pagination so I send a first query that returns the count of records of such query, then the same query without the count…

The problem comes when I navigate to the second page where my offset is already calculated and I queried it on the count query, resulting in this:

SELECT COUNT(*) FROM sale_sale WHERE state = 'quotation' AND company = '1' OFFSET 21;
 count 
-------
(0 rows)
Traceback (most recent call last):
  File "/home/hodei/dev/ingsys/client/.venv/lib/python3.10/site-packages/trytond/protocols/wrappers.py", line 237, in wrapper
    response = func(request, *args, **kwargs)
  File "/home/hodei/tryton_app/wrappers.py", line 37, in wrapper
    return func(*args, application, **kwargs)
  File "/home/hodei/tryton_app/routes.py", line 107, in sales
    sales = Sale.search(domain, offset=offset, order=order,
  File "/home/hodei/dev/ingsys/client/.venv/lib/python3.10/site-packages/trytond/model/modelsql.py", line 1322, in search
    return cursor.fetchone()[0]
TypeError: 'NoneType' object is not subscriptable

Without the offset this query returns the correct value

SELECT COUNT(*) FROM sale_sale WHERE state = 'quotation' AND company = '1';
 count 
-------
    33
(1 row)

This is an easy fix for me handling the query either on the client or the server but I wonder if this could be considered as a bug, a feature or just a bad use.

Indeed offset does not have sense for a SELECT COUNT(*).
I guess we could improve the behavior in Issue 10967: Add limit to search count - Tryton issue tracker by supporting offset using a subquery.