Skip to content

Warn when slicing twice #1775

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading