# Buggy code
def slice(self, start, stop):
    """Computes the "slice" of the :class:`.Query` represented by
    the given indices and returns the resulting :class:`.Query`.

    The start and stop indices behave like the argument to Python's
    built-in :func:`range` function. This method provides an
    alternative to using ``LIMIT``/``OFFSET`` to get a slice of the
    query.

    For example, ::

        session.query(User).order_by(User.id).slice(1, 3)

    renders as

    .. sourcecode:: sql

       SELECT users.id AS users_id,
              users.name AS users_name
       FROM users ORDER BY users.id
       LIMIT ? OFFSET ?
       (2, 1)

    .. seealso::

       :meth:`.Query.limit`

       :meth:`.Query.offset`

    """
    if start is not None and stop is not None:
        self._offset = (self._offset or 0) + start
        self._limit = stop - start
    elif start is None and stop is not None:
        self._limit = stop
    elif start is not None and stop is None:
        self._offset = (self._offset or 0) + start

    if self._offset == 0:
        self._offset = None


# Traceback (TypeError)
/home/artslob/.virtualenvs/sqlalchemy-bug-report/bin/python /home/artslob/projects/python/sqlalchemy-bug-report/main.py
2019-08-11 12:57:29,715 INFO sqlalchemy.engine.base.Engine select version()
2019-08-11 12:57:29,716 INFO sqlalchemy.engine.base.Engine {}
2019-08-11 12:57:29,717 INFO sqlalchemy.engine.base.Engine select current_schema()
2019-08-11 12:57:29,717 INFO sqlalchemy.engine.base.Engine {}
2019-08-11 12:57:29,718 INFO sqlalchemy.engine.base.Engine SELECT CAST('test plain returns' AS VARCHAR(60)) AS anon_1
2019-08-11 12:57:29,718 INFO sqlalchemy.engine.base.Engine {}
2019-08-11 12:57:29,719 INFO sqlalchemy.engine.base.Engine SELECT CAST('test unicode returns' AS VARCHAR(60)) AS anon_1
2019-08-11 12:57:29,719 INFO sqlalchemy.engine.base.Engine {}
2019-08-11 12:57:29,720 INFO sqlalchemy.engine.base.Engine show standard_conforming_strings
2019-08-11 12:57:29,720 INFO sqlalchemy.engine.base.Engine {}
2019-08-11 12:57:29,721 INFO sqlalchemy.engine.base.Engine select relname from pg_class c join pg_namespace n on n.oid=c.relnamespace where pg_catalog.pg_table_is_visible(c.oid) and relname=%(name)s
2019-08-11 12:57:29,721 INFO sqlalchemy.engine.base.Engine {'name': 'users'}
2019-08-11 12:57:29,724 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)
2019-08-11 12:57:29,724 INFO sqlalchemy.engine.base.Engine DELETE FROM users
2019-08-11 12:57:29,724 INFO sqlalchemy.engine.base.Engine {}
2019-08-11 12:57:29,727 INFO sqlalchemy.engine.base.Engine INSERT INTO users (id, name) VALUES (%(id)s, %(name)s)
2019-08-11 12:57:29,727 INFO sqlalchemy.engine.base.Engine ({'id': 1, 'name': 'user 1'}, {'id': 2, 'name': 'user 2'}, {'id': 3, 'name': 'user 3'}, {'id': 4, 'name': 'user 4'}, {'id': 5, 'name': 'user 5'}, {'id': 6, 'name': 'user 6'}, {'id': 7, 'name': 'user 7'}, {'id': 8, 'name': 'user 8'}  ... displaying 10 of 15 total bound parameter sets ...  {'id': 14, 'name': 'user 14'}, {'id': 15, 'name': 'user 15'})
2019-08-11 12:57:29,731 INFO sqlalchemy.engine.base.Engine COMMIT
2019-08-11 12:57:29,736 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)
2019-08-11 12:57:29,737 INFO sqlalchemy.engine.base.Engine SELECT users.id AS users_id, users.name AS users_name
FROM users
LIMIT %(param_1)s OFFSET floor(random() * (SELECT count(users.id) AS count_1
FROM users))
2019-08-11 12:57:29,737 INFO sqlalchemy.engine.base.Engine {'param_1': 1}
[user: (id=15, name='user 15')]
Traceback (most recent call last):
File "/home/artslob/projects/python/sqlalchemy-bug-report/main.py", line 46, in <module>
main()
File "/home/artslob/projects/python/sqlalchemy-bug-report/main.py", line 34, in main
print(bad(query))
File "/home/artslob/projects/python/sqlalchemy-bug-report/main.py", line 42, in bad
return query.first()
File "/home/artslob/.virtualenvs/sqlalchemy-bug-report/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 3228, in first
ret = list(self[0:1])
File "/home/artslob/.virtualenvs/sqlalchemy-bug-report/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 3014, in __getitem__
res = self.slice(start, stop)
File "<string>", line 2, in slice
File "/home/artslob/.virtualenvs/sqlalchemy-bug-report/lib/python3.6/site-packages/sqlalchemy/orm/base.py", line 220, in generate
fn(self, *args[1:], **kw)
File "/home/artslob/.virtualenvs/sqlalchemy-bug-report/lib/python3.6/site-packages/sqlalchemy/orm/query.py", line 3057, in slice
self._offset = (self._offset or 0) + start
File "/home/artslob/.virtualenvs/sqlalchemy-bug-report/lib/python3.6/site-packages/sqlalchemy/sql/elements.py", line 516, in __bool__
raise TypeError("Boolean value of this clause is not defined")
TypeError: Boolean value of this clause is not defined

Process finished with exit code 1

# Fixed code
def slice(self, start, stop):
    """Computes the "slice" of the :class:`.Query` represented by
    the given indices and returns the resulting :class:`.Query`.

    The start and stop indices behave like the argument to Python's
    built-in :func:`range` function. This method provides an
    alternative to using ``LIMIT``/``OFFSET`` to get a slice of the
    query.

    For example, ::

        session.query(User).order_by(User.id).slice(1, 3)

    renders as

    .. sourcecode:: sql

       SELECT users.id AS users_id,
              users.name AS users_name
       FROM users ORDER BY users.id
       LIMIT ? OFFSET ?
       (2, 1)

    .. seealso::

       :meth:`.Query.limit`

       :meth:`.Query.offset`

    """
    if start is not None and stop is not None:
        self._offset = self._offset if self._offset is not None else 0
        if start != 0:
            self._offset += start
        self._limit = stop - start
    elif start is None and stop is not None:
        self._limit = stop
    elif start is not None and stop is None:
        self._offset = self._offset if self._offset is not None else 0
        if start != 0:
            self._offset += start

    if isinstance(self._offset, int) and self._offset == 0:
        self._offset = None