Skip to content

Commit e83a82c

Browse files
authored
Adding decorator for future deprecation (#214)
* Adding decorator for future deprecation * Add test for decorator * mark Audience Intelligence deprecation * __qualname__ is not available for Python 2 - we can later change this back to use __qualname__ once we dropped the Python 2 support
1 parent 3639f28 commit e83a82c

File tree

5 files changed

+50
-2
lines changed

5 files changed

+50
-2
lines changed

tests/test_utils.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
11
import datetime
2-
from random import randint
32

43
from twitter_ads.enum import GRANULARITY
5-
from twitter_ads.utils import to_time
4+
from twitter_ads.utils import to_time, Deprecated
65

76

87
t = datetime.datetime(2006, 3, 21, 0, 0, 0)
98

9+
1010
def test_to_time_based_on_granularity():
1111
for g in [None, GRANULARITY.HOUR, GRANULARITY.TOTAL]:
1212
assert to_time(t, g) == '2006-03-21T00:00:00Z'
1313
assert to_time(t, GRANULARITY.DAY) == '2006-03-21'
14+
15+
16+
def test_decorator_deprecated():
17+
import warnings
18+
19+
class TestClass(object):
20+
@classmethod
21+
@Deprecated('deprecated API')
22+
def test(self):
23+
pass
24+
25+
with warnings.catch_warnings(record=True) as log:
26+
TestClass.test()
27+
assert len(log) == 1
28+
assert issubclass(log[-1].category, DeprecationWarning)
29+
assert "TestClass.test" in str(log[-1].message)
30+
assert "deprecated API" in str(log[-1].message)

twitter_ads/audience.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from twitter_ads.error import BadRequest
99
from twitter_ads.cursor import Cursor
1010
from twitter_ads import API_VERSION
11+
from twitter_ads.utils import Deprecated
1112

1213
import json
1314

@@ -174,6 +175,8 @@ def __get(klass, account, client, params):
174175
resource, headers=klass.HEADERS, body=params)
175176
return Cursor(klass, request, init_with=[account])
176177

178+
@Deprecated('This endpoint has been deprecated and will no longer be available '
179+
'as of 2019-08-28')
177180
def conversations(self):
178181
"""
179182
Get the conversation topics for an input targeting criteria
@@ -185,6 +188,8 @@ def conversations(self):
185188
}
186189
return self.__get(account=self.account, client=self.account.client, params=json.dumps(body))
187190

191+
@Deprecated('This endpoint has been deprecated and will no longer be available '
192+
'as of 2019-08-28')
188193
def demographics(self):
189194
"""
190195
Get the demographic breakdown for an input targeting criteria

twitter_ads/campaign.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from twitter_ads.http import Request
88
from twitter_ads.cursor import Cursor
99
from twitter_ads import API_VERSION
10+
from twitter_ads.utils import Deprecated
1011

1112

1213
class TargetingCriteria(Resource, Persistence, Batch):
@@ -343,6 +344,8 @@ def __init__(self):
343344
'Error! {name} cannot be instantiated.'.format(name=self.__class__.__name__))
344345

345346
@classmethod
347+
@Deprecated('This endpoint has been deprecated and will no longer be available '
348+
'as of 2019-08-20')
346349
def preview(klass, account, **kwargs):
347350
"""
348351
Returns an HTML preview of a tweet, either new or existing.

twitter_ads/creative.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from twitter_ads.enum import TRANSFORM
1010
from twitter_ads.http import Request
1111
from twitter_ads.resource import resource_property, Resource, Persistence, Analytics
12+
from twitter_ads.utils import Deprecated
1213

1314

1415
class PromotedAccount(Resource, Persistence):
@@ -324,6 +325,8 @@ class ScheduledTweet(Resource, Persistence):
324325
RESOURCE = '/' + API_VERSION + '/accounts/{account_id}/scheduled_tweets/{id}'
325326
PREVIEW = '/' + API_VERSION + '/accounts/{account_id}/scheduled_tweets/preview/{id}'
326327

328+
@Deprecated('This endpoint has been deprecated and will no longer be available '
329+
'as of 2019-08-20')
327330
def preview(self):
328331
"""
329332
Returns an HTML preview for a Scheduled Tweet.
@@ -363,6 +366,8 @@ class DraftTweet(Resource, Persistence):
363366
RESOURCE = '/' + API_VERSION + '/accounts/{account_id}/draft_tweets/{id}'
364367
PREVIEW = '/' + API_VERSION + '/accounts/{account_id}/draft_tweets/preview/{id}'
365368

369+
@Deprecated('This endpoint has been deprecated and will no longer be available '
370+
'as of 2019-08-20')
366371
def preview(self, draft_tweet_id=None):
367372
"""
368373
Preview a Draft Tweet on a mobile device.

twitter_ads/utils.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
"""Container for all helpers and utilities used throughout the Ads API SDK."""
55

66
import datetime
7+
import warnings
8+
warnings.simplefilter('default', DeprecationWarning)
79
from email.utils import formatdate
810
from time import mktime
911

@@ -79,3 +81,19 @@ def extract_response_headers(headers):
7981
values['account_rate_limit_reset'] = headers.get('x-account-rate-limit-reset')
8082

8183
return values
84+
85+
86+
class Deprecated(object):
87+
def __init__(self, message):
88+
self._message = message
89+
90+
def __call__(self, decorated, *args, **kwargs):
91+
def wrapper(*args, **kwargs):
92+
method = "{}.{}".format(str(args[0].__name__), str(decorated.__name__))
93+
warnings.warn(
94+
"{} => {}".format(method, self._message),
95+
DeprecationWarning,
96+
stacklevel=2
97+
)
98+
return decorated(*args, **kwargs)
99+
return wrapper

0 commit comments

Comments
 (0)