Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0f17595

Browse files
authored
chore: drop use of 'pytz' in tests / examples (googleapis#534)
Current (pre-)releases of 'google-api-core' and 'google-cloud-core' no longer use or depend on 'pytz'. Closes googleapis#533.
1 parent 6776eae commit 0f17595

File tree

5 files changed

+35
-28
lines changed

5 files changed

+35
-28
lines changed

google/cloud/storage/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1613,10 +1613,10 @@ def generate_signed_post_policy_v4(
16131613
Example:
16141614
Generate signed POST policy and upload a file.
16151615
1616+
>>> import datetime
16161617
>>> from google.cloud import storage
1617-
>>> import pytz
16181618
>>> client = storage.Client()
1619-
>>> tz = pytz.timezone('America/New_York')
1619+
>>> tz = datetime.timezone(datetime.timedelta(hours=1), 'CET')
16201620
>>> policy = client.generate_signed_post_policy_v4(
16211621
"bucket-name",
16221622
"blob-name",

tests/unit/test__signing.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,24 @@ def _utc_seconds(when):
4444
return int(calendar.timegm(when.timetuple()))
4545

4646

47+
def _make_cet_timezone():
48+
try:
49+
from datetime import timezone
50+
51+
except ImportError: # Python 2.7
52+
from google.cloud._helpers import _UTC
53+
54+
class CET(_UTC):
55+
_tzname = "CET"
56+
_utcoffset = datetime.timedelta(hours=1)
57+
58+
return CET()
59+
else:
60+
from datetime import timedelta
61+
62+
return timezone(timedelta(hours=1), name="CET")
63+
64+
4765
class Test_get_expiration_seconds_v2(unittest.TestCase):
4866
@staticmethod
4967
def _call_fut(expiration):
@@ -81,13 +99,7 @@ def test_w_expiration_utc_datetime(self):
8199
self.assertEqual(self._call_fut(expiration_utc), utc_seconds)
82100

83101
def test_w_expiration_other_zone_datetime(self):
84-
from google.cloud._helpers import _UTC
85-
86-
class CET(_UTC):
87-
_tzname = "CET"
88-
_utcoffset = datetime.timedelta(hours=1)
89-
90-
zone = CET()
102+
zone = _make_cet_timezone()
91103
expiration_other = datetime.datetime(2004, 8, 19, 0, 0, 0, 0, zone)
92104
utc_seconds = _utc_seconds(expiration_other)
93105
cet_seconds = utc_seconds - (60 * 60) # CET one hour earlier than UTC
@@ -198,13 +210,8 @@ def test_w_expiration_utc_datetime(self):
198210

199211
def test_w_expiration_other_zone_datetime(self):
200212
from google.cloud._helpers import UTC
201-
from google.cloud._helpers import _UTC
202-
203-
class CET(_UTC):
204-
_tzname = "CET"
205-
_utcoffset = datetime.timedelta(hours=1)
206213

207-
zone = CET()
214+
zone = _make_cet_timezone()
208215
fake_utcnow = datetime.datetime(2004, 8, 19, 0, 0, 0, 0, UTC)
209216
fake_cetnow = fake_utcnow.astimezone(zone)
210217
delta = datetime.timedelta(seconds=10)

tests/unit/test_bucket.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -366,10 +366,10 @@ def test_ctor_defaults(self):
366366

367367
def test_ctor_explicit_ubla(self):
368368
import datetime
369-
import pytz
369+
from google.cloud._helpers import UTC
370370

371371
bucket = self._make_bucket()
372-
now = datetime.datetime.utcnow().replace(tzinfo=pytz.UTC)
372+
now = datetime.datetime.utcnow().replace(tzinfo=UTC)
373373

374374
config = self._make_one(
375375
bucket,
@@ -403,10 +403,10 @@ def test_ctor_explicit_pap(self):
403403

404404
def test_ctor_explicit_bpo(self):
405405
import datetime
406-
import pytz
406+
from google.cloud._helpers import UTC
407407

408408
bucket = self._make_bucket()
409-
now = datetime.datetime.utcnow().replace(tzinfo=pytz.UTC)
409+
now = datetime.datetime.utcnow().replace(tzinfo=UTC)
410410

411411
config = pytest.deprecated_call(
412412
self._make_one,
@@ -433,10 +433,10 @@ def test_ctor_ubla_and_bpo_enabled(self):
433433

434434
def test_ctor_ubla_and_bpo_time(self):
435435
import datetime
436-
import pytz
436+
from google.cloud._helpers import UTC
437437

438438
bucket = self._make_bucket()
439-
now = datetime.datetime.utcnow().replace(tzinfo=pytz.UTC)
439+
now = datetime.datetime.utcnow().replace(tzinfo=UTC)
440440

441441
with self.assertRaises(ValueError):
442442
self._make_one(
@@ -481,12 +481,12 @@ def test_from_api_repr_w_disabled(self):
481481

482482
def test_from_api_repr_w_enabled(self):
483483
import datetime
484-
import pytz
484+
from google.cloud._helpers import UTC
485485
from google.cloud._helpers import _datetime_to_rfc3339
486486

487487
klass = self._get_target_class()
488488
bucket = self._make_bucket()
489-
now = datetime.datetime.utcnow().replace(tzinfo=pytz.UTC)
489+
now = datetime.datetime.utcnow().replace(tzinfo=UTC)
490490
resource = {
491491
"uniformBucketLevelAccess": {
492492
"enabled": True,
@@ -2174,11 +2174,11 @@ def test_iam_configuration_policy_missing(self):
21742174

21752175
def test_iam_configuration_policy_w_entry(self):
21762176
import datetime
2177-
import pytz
2177+
from google.cloud._helpers import UTC
21782178
from google.cloud._helpers import _datetime_to_rfc3339
21792179
from google.cloud.storage.bucket import IAMConfiguration
21802180

2181-
now = datetime.datetime.utcnow().replace(tzinfo=pytz.UTC)
2181+
now = datetime.datetime.utcnow().replace(tzinfo=UTC)
21822182
NAME = "name"
21832183
properties = {
21842184
"iamConfiguration": {

tests/unit/test_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1778,7 +1778,7 @@ def _create_hmac_key_helper(
17781778
self, explicit_project=None, user_project=None, timeout=None, retry=None,
17791779
):
17801780
import datetime
1781-
from pytz import UTC
1781+
from google.cloud._helpers import UTC
17821782
from google.cloud.storage.hmac_key import HMACKeyMetadata
17831783

17841784
project = "PROJECT"

tests/unit/test_hmac_key.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ def test_state_setter_active(self):
173173

174174
def test_time_created_getter(self):
175175
import datetime
176-
from pytz import UTC
176+
from google.cloud._helpers import UTC
177177

178178
metadata = self._make_one()
179179
now = datetime.datetime.utcnow()
@@ -183,7 +183,7 @@ def test_time_created_getter(self):
183183

184184
def test_updated_getter(self):
185185
import datetime
186-
from pytz import UTC
186+
from google.cloud._helpers import UTC
187187

188188
metadata = self._make_one()
189189
now = datetime.datetime.utcnow()

0 commit comments

Comments
 (0)