Skip to content

Commit 1d4d1a5

Browse files
committed
refactor: migrate deprecated apis to use warn_deprecation decorator
- add warn_deprecation decorator to analytics_rule_v1 and analytics_rules_v1 - add warn_deprecation decorator to override and overrides classes - add warn_deprecation decorator to synonym and synonyms classes - remove manual deprecation warning code and global flags - replace manual logger.warning calls with decorator-based warnings
1 parent 3a172ad commit 1d4d1a5

File tree

6 files changed

+34
-28
lines changed

6 files changed

+34
-28
lines changed

src/typesense/analytics_rule_v1.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import typing_extensions as typing
2929

3030
from typesense.api_call import ApiCall
31+
from typesense.logger import warn_deprecation
3132
from typesense.types.analytics_rule_v1 import (
3233
RuleDeleteSchema,
3334
RuleSchemaForCounters,
@@ -47,6 +48,10 @@ class AnalyticsRuleV1:
4748
rule_id (str): The ID of the analytics rule.
4849
"""
4950

51+
@warn_deprecation(
52+
"AnalyticsRuleV1 is deprecated on v30+. Use client.analytics.rules[rule_id] instead.",
53+
flag_name="analytics_rules_v1_deprecation",
54+
)
5055
def __init__(self, api_call: ApiCall, rule_id: str):
5156
"""
5257
Initialize the AnalyticsRuleV1 object.

src/typesense/analytics_rules_v1.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
import sys
2929

30+
from typesense.logger import warn_deprecation
31+
3032
if sys.version_info >= (3, 11):
3133
import typing
3234
else:
@@ -63,6 +65,10 @@ class AnalyticsRulesV1(object):
6365

6466
resource_path: typing.Final[str] = "/analytics/rules"
6567

68+
@warn_deprecation(
69+
"AnalyticsRulesV1 is deprecated on v30+. Use client.analytics instead.",
70+
flag_name="analytics_rules_v1_deprecation",
71+
)
6672
def __init__(self, api_call: ApiCall):
6773
"""
6874
Initialize the AnalyticsRulesV1 object.

src/typesense/override.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"""
2323

2424
from typesense.api_call import ApiCall
25+
from typesense.logger import warn_deprecation
2526
from typesense.types.override import OverrideDeleteSchema, OverrideSchema
2627

2728

@@ -38,6 +39,11 @@ class Override:
3839
override_id (str): The ID of the override.
3940
"""
4041

42+
@warn_deprecation(
43+
"The override API (collections/{collection}/overrides/{override_id}) is deprecated is removed on v30+. "
44+
"Use curation sets (curation_sets) instead.",
45+
flag_name="overrides_deprecation",
46+
)
4147
def __init__(
4248
self,
4349
api_call: ApiCall,

src/typesense/overrides.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import sys
3131

3232
from typesense.api_call import ApiCall
33+
from typesense.logger import warn_deprecation
3334
from typesense.override import Override
3435
from typesense.types.override import (
3536
OverrideCreateSchema,
@@ -59,6 +60,10 @@ class Overrides:
5960

6061
resource_path: typing.Final[str] = "overrides"
6162

63+
@warn_deprecation(
64+
"Overrides is deprecated on v30+. Use client.curation_sets instead.",
65+
flag_name="overrides_deprecation",
66+
)
6267
def __init__(
6368
self,
6469
api_call: ApiCall,

src/typesense/synonym.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,9 @@
2222
"""
2323

2424
from typesense.api_call import ApiCall
25-
from typesense.logger import logger
25+
from typesense.logger import warn_deprecation
2626
from typesense.types.synonym import SynonymDeleteSchema, SynonymSchema
2727

28-
_synonym_deprecation_warned = False
29-
3028

3129
class Synonym:
3230
"""
@@ -41,6 +39,11 @@ class Synonym:
4139
synonym_id (str): The ID of the synonym.
4240
"""
4341

42+
@warn_deprecation(
43+
"The synonym API (collections/{collection}/synonyms/{synonym_id}) is deprecated is removed on v30+. "
44+
"Use synonym sets (synonym_sets) instead.",
45+
flag_name="synonyms_deprecation",
46+
)
4447
def __init__(
4548
self,
4649
api_call: ApiCall,
@@ -66,7 +69,6 @@ def retrieve(self) -> SynonymSchema:
6669
Returns:
6770
SynonymSchema: The schema containing the synonym details.
6871
"""
69-
self._maybe_warn_deprecation()
7072
return self.api_call.get(self._endpoint_path(), entity_type=SynonymSchema)
7173

7274
def delete(self) -> SynonymDeleteSchema:
@@ -76,7 +78,6 @@ def delete(self) -> SynonymDeleteSchema:
7678
Returns:
7779
SynonymDeleteSchema: The schema containing the deletion response.
7880
"""
79-
self._maybe_warn_deprecation()
8081
return self.api_call.delete(
8182
self._endpoint_path(),
8283
entity_type=SynonymDeleteSchema,
@@ -100,12 +101,3 @@ def _endpoint_path(self) -> str:
100101
self.synonym_id,
101102
],
102103
)
103-
104-
def _maybe_warn_deprecation(self) -> None:
105-
global _synonym_deprecation_warned
106-
if not _synonym_deprecation_warned:
107-
logger.warning(
108-
"The synonyms API (collections/{collection}/synonyms) is deprecated and will be "
109-
"removed in a future release. Use synonym sets (synonym_sets) instead."
110-
)
111-
_synonym_deprecation_warned = True

src/typesense/synonyms.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,13 @@
2828
import sys
2929

3030
from typesense.api_call import ApiCall
31+
from typesense.logger import warn_deprecation
3132
from typesense.synonym import Synonym
3233
from typesense.types.synonym import (
3334
SynonymCreateSchema,
3435
SynonymSchema,
3536
SynonymsRetrieveSchema,
3637
)
37-
from typesense.logger import logger
38-
39-
_synonyms_deprecation_warned = False
4038

4139
if sys.version_info >= (3, 11):
4240
import typing
@@ -60,6 +58,11 @@ class Synonyms:
6058

6159
resource_path: typing.Final[str] = "synonyms"
6260

61+
@warn_deprecation(
62+
"The synonyms API (collections/{collection}/synonyms) is deprecated is removed on v30+. "
63+
"Use synonym sets (synonym_sets) instead.",
64+
flag_name="synonyms_deprecation",
65+
)
6366
def __init__(self, api_call: ApiCall, collection_name: str):
6467
"""
6568
Initialize the Synonyms object.
@@ -101,7 +104,6 @@ def upsert(self, synonym_id: str, schema: SynonymCreateSchema) -> SynonymSchema:
101104
Returns:
102105
SynonymSchema: The created or updated synonym.
103106
"""
104-
self._maybe_warn_deprecation()
105107
response = self.api_call.put(
106108
self._endpoint_path(synonym_id),
107109
body=schema,
@@ -116,7 +118,6 @@ def retrieve(self) -> SynonymsRetrieveSchema:
116118
Returns:
117119
SynonymsRetrieveSchema: The schema containing all synonyms.
118120
"""
119-
self._maybe_warn_deprecation()
120121
response = self.api_call.get(
121122
self._endpoint_path(),
122123
entity_type=SynonymsRetrieveSchema,
@@ -144,12 +145,3 @@ def _endpoint_path(self, synonym_id: typing.Union[str, None] = None) -> str:
144145
synonym_id,
145146
],
146147
)
147-
148-
def _maybe_warn_deprecation(self) -> None:
149-
global _synonyms_deprecation_warned
150-
if not _synonyms_deprecation_warned:
151-
logger.warning(
152-
"The synonyms API (collections/{collection}/synonyms) is deprecated and will be "
153-
"removed in a future release. Use synonym sets (synonym_sets) instead."
154-
)
155-
_synonyms_deprecation_warned = True

0 commit comments

Comments
 (0)