Skip to content

Commit b9c8343

Browse files
authored
Warn when slicing twice (elastic#1775)
1 parent cb99fd5 commit b9c8343

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

elasticsearch_dsl/search_base.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import collections.abc
1919
import copy
20+
import warnings
2021

2122
from .aggs import A, AggBase
2223
from .exceptions import IllegalOperation
@@ -348,6 +349,15 @@ def __getitem__(self, n):
348349
"""
349350
s = self._clone()
350351

352+
if "from" in s._extra or "size" in s._extra:
353+
warnings.warn(
354+
"Slicing multiple times currently has no effect but will be supported "
355+
"in a future release. See https://github.com/elastic/elasticsearch-dsl-py/pull/1771 "
356+
"for more details",
357+
DeprecationWarning,
358+
stacklevel=2,
359+
)
360+
351361
if isinstance(n, slice):
352362
# If negative slicing, abort.
353363
if n.start and n.start < 0 or n.stop and n.stop < 0:

tests/_async/test_search.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
from copy import deepcopy
1919

20-
from pytest import raises
20+
from pytest import raises, warns
2121

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

365365

366+
def test_slice_twice():
367+
with warns(DeprecationWarning, match="Slicing multiple times .*"):
368+
s = AsyncSearch()
369+
s[10:20][2:]
370+
371+
366372
def test_index():
367373
s = AsyncSearch()
368374
assert {"from": 3, "size": 1} == s[3].to_dict()

tests/_sync/test_search.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
from copy import deepcopy
1919

20-
from pytest import raises
20+
from pytest import raises, warns
2121

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

365365

366+
def test_slice_twice():
367+
with warns(DeprecationWarning, match="Slicing multiple times .*"):
368+
s = Search()
369+
s[10:20][2:]
370+
371+
366372
def test_index():
367373
s = Search()
368374
assert {"from": 3, "size": 1} == s[3].to_dict()

0 commit comments

Comments
 (0)