Skip to content

Commit

Permalink
Merge pull request #377 from JuanTecedor/376-fix-deprecations-query-f…
Browse files Browse the repository at this point in the history
…ilters

Fix deprecations, add warning for Contains
  • Loading branch information
Kamforka authored Jan 12, 2025
2 parents ce0b37f + 8bec9b7 commit 00a4ceb
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
9 changes: 7 additions & 2 deletions tests/test_query_filters.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pytest

from thehive4py import TheHiveApi
from thehive4py.helpers import now_to_ts
from thehive4py.query.filters import (
Expand All @@ -7,6 +9,7 @@
Eq,
Gt,
Gte,
Has,
Id,
In,
Like,
Expand All @@ -20,11 +23,13 @@


class TestQueryFilters:
def test_between_contains_in(self, thehive: TheHiveApi, test_user: OutputUser):
def test_between_contains_has_in(self, thehive: TheHiveApi, test_user: OutputUser):
assert thehive.user.find(
filters=Between(field="_createdAt", start=0, end=now_to_ts())
)
assert thehive.user.find(filters=Contains(field="login"))
with pytest.deprecated_call():
assert thehive.user.find(filters=Contains(field="login"))
assert thehive.user.find(filters=Has(field="login"))
assert thehive.user.find(
filters=In(field="login", values=["...", "xyz", test_user["login"]])
)
Expand Down
2 changes: 1 addition & 1 deletion thehive4py/query/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from .filters import Between, Contains, EndsWith, Eq
from .filters import FilterExpr as _FilterExpr
from .filters import Gt, Gte, Id, In, Like, Lt, Lte, Match, Ne, StartsWith
from .filters import Gt, Gte, Has, Id, In, Like, Lt, Lte, Match, Ne, StartsWith
from .page import Paginate
from .sort import Asc, Desc
from .sort import SortExpr as _SortExpr
Expand Down
23 changes: 19 additions & 4 deletions thehive4py/query/filters.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from collections import UserDict as _UserDict
from typing import Any as _Any
from typing import Union as _Union
import warnings

FilterExpr = _Union["_FilterBase", dict]

Expand Down Expand Up @@ -34,28 +35,28 @@ class Lt(_FilterBase):
"""Field less than value."""

def __init__(self, field: str, value: _Any):
super().__init__(_lt={field: value})
super().__init__(_lt={"_field": field, "_value": value})


class Gt(_FilterBase):
"""Field greater than value."""

def __init__(self, field: str, value: _Any):
super().__init__(_gt={field: value})
super().__init__(_gt={"_field": field, "_value": value})


class Lte(_FilterBase):
"""Field less than or equal value."""

def __init__(self, field: str, value: _Any):
super().__init__(_lte={field: value})
super().__init__(_lte={"_field": field, "_value": value})


class Gte(_FilterBase):
"""Field less than or equal value."""

def __init__(self, field: str, value: _Any):
super().__init__(_gte={field: value})
super().__init__(_gte={"_field": field, "_value": value})


class Ne(_FilterBase):
Expand Down Expand Up @@ -111,9 +112,23 @@ class Contains(_FilterBase):
"""Object contains the field."""

def __init__(self, field: str):
warnings.warn(
message="The `Contains` filter has been deprecated. "
"Please use the `Has` filter to prevent breaking "
"changes in the future.",
category=DeprecationWarning,
stacklevel=2,
)
super().__init__(_contains=field)


class Has(_FilterBase):
"""Object contains the field."""

def __init__(self, field: str):
super().__init__(_has=field)


class Like(_FilterBase):
"""Field contains the value."""

Expand Down

0 comments on commit 00a4ceb

Please sign in to comment.