Skip to content

Commit

Permalink
Warn when slicing twice (#1775)
Browse files Browse the repository at this point in the history
  • Loading branch information
pquentin committed Apr 15, 2024
1 parent cb99fd5 commit b9c8343
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
10 changes: 10 additions & 0 deletions elasticsearch_dsl/search_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import collections.abc
import copy
import warnings

from .aggs import A, AggBase
from .exceptions import IllegalOperation
Expand Down Expand Up @@ -348,6 +349,15 @@ def __getitem__(self, n):
"""
s = self._clone()

if "from" in s._extra or "size" in s._extra:
warnings.warn(
"Slicing multiple times currently has no effect but will be supported "
"in a future release. See https://github.com/elastic/elasticsearch-dsl-py/pull/1771 "
"for more details",
DeprecationWarning,
stacklevel=2,
)

if isinstance(n, slice):
# If negative slicing, abort.
if n.start and n.start < 0 or n.stop and n.stop < 0:
Expand Down
8 changes: 7 additions & 1 deletion tests/_async/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

from copy import deepcopy

from pytest import raises
from pytest import raises, warns

from elasticsearch_dsl import AsyncSearch, Document, Q, query
from elasticsearch_dsl.exceptions import IllegalOperation
Expand Down Expand Up @@ -363,6 +363,12 @@ def test_slice():
assert {"from": 20, "size": 0} == s[20:0].to_dict()


def test_slice_twice():
with warns(DeprecationWarning, match="Slicing multiple times .*"):
s = AsyncSearch()
s[10:20][2:]


def test_index():
s = AsyncSearch()
assert {"from": 3, "size": 1} == s[3].to_dict()
Expand Down
8 changes: 7 additions & 1 deletion tests/_sync/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

from copy import deepcopy

from pytest import raises
from pytest import raises, warns

from elasticsearch_dsl import Document, Q, Search, query
from elasticsearch_dsl.exceptions import IllegalOperation
Expand Down Expand Up @@ -363,6 +363,12 @@ def test_slice():
assert {"from": 20, "size": 0} == s[20:0].to_dict()


def test_slice_twice():
with warns(DeprecationWarning, match="Slicing multiple times .*"):
s = Search()
s[10:20][2:]


def test_index():
s = Search()
assert {"from": 3, "size": 1} == s[3].to_dict()
Expand Down

0 comments on commit b9c8343

Please sign in to comment.