Skip to content

Commit 34438e0

Browse files
committed
Remove dependence on pytz
1 parent 0cf1f10 commit 34438e0

File tree

5 files changed

+18
-31
lines changed

5 files changed

+18
-31
lines changed

requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ boto3==1.34.35
33
deprecation==2.1.0
44
gemd==2.1.9
55
pyjwt==2.8.0
6-
pytz==2024.1; python_version < "3.9"
76
requests==2.32.2
87
tqdm==4.66.3
98

setup.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
"deprecation>=2.1.0,<3",
3131
"urllib3>=1.26.18,<3",
3232
"tqdm>=4.27.0,<5",
33-
"pytz>=2024.1; python_version<'3.9'",
3433
],
3534
extras_require={
3635
"tests": [

src/citrine/_session.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
import platform
2-
from datetime import datetime, timedelta
3-
try: # Only available starting Python 3.9
4-
from datetime import UTC
5-
except ImportError: # pragma: no cover
6-
from pytz import utc as UTC
2+
from datetime import datetime, timedelta, timezone
73
from json.decoder import JSONDecodeError
84
from logging import getLogger
95
from os import environ
@@ -29,7 +25,7 @@
2925

3026
# Choose a 5-second buffer so that there's no chance of the access token
3127
# expiring during the check for expiration
32-
EXPIRATION_BUFFER_MILLIS: timedelta = timedelta(milliseconds=5000)
28+
EXPIRATION_BUFFER: timedelta = timedelta(seconds=5)
3329
logger = getLogger(__name__)
3430

3531

@@ -57,7 +53,7 @@ def __init__(self,
5753
self.authority = ':'.join(([host] if host else []) + ([port] if port else []))
5854
self.refresh_token: str = refresh_token
5955
self.access_token: Optional[str] = None
60-
self.access_token_expiration: datetime = datetime.now(UTC)
56+
self.access_token_expiration: datetime = datetime.now(timezone.utc)
6157

6258
agent = "{}/{} python-requests/{} citrine-python/{}".format(
6359
platform.python_implementation(),
@@ -110,7 +106,8 @@ def _versioned_base_url(self, version: str = 'v1'):
110106
))
111107

112108
def _is_access_token_expired(self):
113-
return self.access_token_expiration - EXPIRATION_BUFFER_MILLIS <= datetime.now(UTC)
109+
buffered_expire = self.access_token_expiration - EXPIRATION_BUFFER
110+
return datetime.now(timezone.utc) > buffered_expire
114111

115112
def _refresh_access_token(self) -> None:
116113
"""Optionally refresh our access token (if the previous one is about to expire)."""
@@ -128,7 +125,7 @@ def _refresh_access_token(self) -> None:
128125
options={"verify_signature": False},
129126
algorithms=["HS256"]
130127
)['exp'],
131-
UTC
128+
timezone.utc
132129
)
133130

134131
# Explicitly set an updated 'auth', so as to not rely on implicit cookie handling.

tests/test_citrine.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
import platform
2-
from datetime import datetime
3-
try:
4-
from datetime import UTC
5-
except ImportError: # Only available starting Python 3.9
6-
from pytz import utc as UTC
2+
from datetime import datetime, timezone
73

84
import jwt
95
import pytest
@@ -20,7 +16,7 @@ def refresh_token(expiration: datetime = None) -> dict:
2016
return {'access_token': token}
2117

2218

23-
token_refresh_response = refresh_token(datetime(2019, 3, 14, tzinfo=UTC))
19+
token_refresh_response = refresh_token(datetime(2019, 3, 14, tzinfo=timezone.utc))
2420

2521

2622
def test_citrine_creation():

tests/test_session.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,7 @@
88
WorkflowNotReadyException,
99
RetryableException)
1010

11-
from datetime import datetime, timedelta
12-
try:
13-
from datetime import UTC
14-
except ImportError: # Only available starting Python 3.9
15-
from pytz import utc as UTC
11+
from datetime import datetime, timedelta, timezone
1612

1713
import mock
1814
import requests
@@ -32,7 +28,7 @@ def refresh_token(expiration: datetime = None) -> dict:
3228

3329
@pytest.fixture
3430
def session():
35-
token_refresh_response = refresh_token(datetime(2019, 3, 14, tzinfo=UTC))
31+
token_refresh_response = refresh_token(datetime(2019, 3, 14, tzinfo=timezone.utc))
3632
with requests_mock.Mocker() as m:
3733
m.post('http://citrine-testing.fake/api/v1/tokens/refresh', json=token_refresh_response)
3834
session = Session(
@@ -43,13 +39,13 @@ def session():
4339
# Default behavior is to *not* require a refresh - those tests can clear this out
4440
# As rule of thumb, we should be using freezegun or similar to never rely on the system clock
4541
# for these scenarios, but I thought this is light enough to postpone that for the time being
46-
session.access_token_expiration = datetime.now(UTC) + timedelta(minutes=3)
42+
session.access_token_expiration = datetime.now(timezone.utc) + timedelta(minutes=3)
4743

4844
return session
4945

5046

5147
def test_session_signature(monkeypatch):
52-
token_refresh_response = refresh_token(datetime(2019, 3, 14, tzinfo=UTC))
48+
token_refresh_response = refresh_token(datetime(2019, 3, 14, tzinfo=timezone.utc))
5349
with requests_mock.Mocker() as m:
5450
m.post('ftp://citrine-testing.fake:8080/api/v1/tokens/refresh', json=token_refresh_response)
5551

@@ -77,8 +73,8 @@ def test_session_signature(monkeypatch):
7773

7874

7975
def test_get_refreshes_token(session: Session):
80-
session.access_token_expiration = datetime.now(UTC) - timedelta(minutes=1)
81-
token_refresh_response = refresh_token(datetime(2019, 3, 14, tzinfo=UTC))
76+
session.access_token_expiration = datetime.now(timezone.utc) - timedelta(minutes=1)
77+
token_refresh_response = refresh_token(datetime(2019, 3, 14, tzinfo=timezone.utc))
8278

8379
with requests_mock.Mocker() as m:
8480
m.post('http://citrine-testing.fake/api/v1/tokens/refresh', json=token_refresh_response)
@@ -89,11 +85,11 @@ def test_get_refreshes_token(session: Session):
8985
resp = session.get_resource('/foo')
9086

9187
assert {'foo': 'bar'} == resp
92-
assert datetime(2019, 3, 14, tzinfo=UTC) == session.access_token_expiration
88+
assert datetime(2019, 3, 14, tzinfo=timezone.utc) == session.access_token_expiration
9389

9490

9591
def test_get_refresh_token_failure(session: Session):
96-
session.access_token_expiration = datetime.now(UTC) - timedelta(minutes=1)
92+
session.access_token_expiration = datetime.now(timezone.utc) - timedelta(minutes=1)
9793

9894
with requests_mock.Mocker() as m:
9995
m.post('http://citrine-testing.fake/api/v1/tokens/refresh', status_code=401)
@@ -197,7 +193,7 @@ def test_connection_error(session: Session):
197193

198194

199195
def test_post_refreshes_token_when_denied(session: Session):
200-
token_refresh_response = refresh_token(datetime(2019, 3, 14, tzinfo=UTC))
196+
token_refresh_response = refresh_token(datetime(2019, 3, 14, tzinfo=timezone.utc))
201197

202198
with requests_mock.Mocker() as m:
203199
m.post('http://citrine-testing.fake/api/v1/tokens/refresh', json=token_refresh_response)
@@ -209,7 +205,7 @@ def test_post_refreshes_token_when_denied(session: Session):
209205
resp = session.post_resource('/foo', json={'data': 'hi'})
210206

211207
assert {'foo': 'bar'} == resp
212-
assert datetime(2019, 3, 14, tzinfo=UTC) == session.access_token_expiration
208+
assert datetime(2019, 3, 14, tzinfo=timezone.utc) == session.access_token_expiration
213209

214210

215211
# this test exists to provide 100% coverage for the legacy 401 status on Unauthorized responses

0 commit comments

Comments
 (0)