Skip to content

Commit 3141bdc

Browse files
committed
refactor: move 'locking.now' to 'util.now'
Rework to avoid warning spew for 'datetime.datetime.utcnow'. Suppress other warning spew (chameleon / AST, pkg_resources, etc.)
1 parent 603c788 commit 3141bdc

File tree

9 files changed

+66
-49
lines changed

9 files changed

+66
-49
lines changed

CHANGES.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
1.0 (unreleased)
2+
================
3+
4+
- Move 'substanced.locking.now' to 'substanced.util.now', and rework to avoid
5+
warning spew for 'datetime.datetime.utcnow'.
6+
7+
- Suppress other warning spew from upstreams in newer Python versions
8+
('chameleon' / AST, 'pkg_resources', etc.)
9+
10+
111
1.0b1 (2024-11-27)
212
==================
313

demos/blog/blog/views/retail.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
from pyramid.decorator import reify
88
from pyramid.httpexceptions import HTTPFound
99
from pyramid.url import resource_url
10-
from substanced.util import find_catalog
10+
from substanced.util import (
11+
find_catalog,
12+
now,
13+
)
1114
from pyramid.view import (
1215
view_config,
1316
view_defaults,
@@ -122,11 +125,6 @@ def __init__(self, context, request):
122125
self.context = context
123126
self.request = request
124127

125-
def _nowtz(self):
126-
now = datetime.datetime.utcnow() # naive
127-
y, mo, d, h, mi, s = now.timetuple()[:6]
128-
return datetime.datetime(y, mo, d, h, mi, s, tzinfo=pytz.utc)
129-
130128
def _get_feed_info(self):
131129
context = self.context
132130
request = self.request
@@ -160,7 +158,7 @@ def _add_updated_strings(updated, info):
160158

161159
blogentries.sort(key=lambda x: x[0].isoformat())
162160
blogentries = [entry[1] for entry in reversed(blogentries)][:15]
163-
updated = blogentries and blogentries[0]['pubdate'] or self._nowtz()
161+
updated = blogentries and blogentries[0]['pubdate'] or now()
164162
_add_updated_strings(updated, feed)
165163

166164
return feed, blogentries

pytest.ini

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,9 @@ addopts = -l --strict
33
norecursedirs = lib include .tox .git
44
python_files = test_*.py tests.py
55
filterwarnings =
6+
ignore::DeprecationWarning:chameleon
67
ignore::DeprecationWarning:pkg_resources
8+
ignore:.*pkg_resources:DeprecationWarning
9+
ignore:.*Attribute . is deprecated:DeprecationWarning
10+
ignore::DeprecationWarning:webob
711
ignore::DeprecationWarning:zodburi

substanced/audit/tests/test_views.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import datetime
12
import unittest
3+
24
from pyramid import testing
35

46
class Test_AuditLogEventStreamView(unittest.TestCase):
@@ -93,11 +95,10 @@ def test_auditstream_sse_with_last_event_id_and_oids(self):
9395
self.assertEqual(list(auditlog.oids), [3])
9496

9597
def test_auditing(self):
96-
import pytz
9798
context = testing.DummyResource()
9899
request = testing.DummyRequest()
99100
request.user = testing.DummyResource()
100-
request.user.timezone = pytz.timezone('UTC')
101+
request.user.timezone = datetime.timezone.utc
101102
inst = self._makeOne(context, request)
102103
inst.get_auditlog = lambda c: DummyAuditLog()
103104
result = inst.auditing()

substanced/content/test_it.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ def test_add_with_meta(self):
2424
def test_create(self):
2525
registry = DummyRegistry()
2626
inst = self._makeOne(registry)
27-
inst._utcnow = lambda *a: 1
2827
content = testing.DummyResource()
2928
inst.content_types['dummy'] = lambda a: content
3029
inst.meta['dummy'] = {}
@@ -34,7 +33,6 @@ def test_create(self):
3433
def test_create_with_oid(self):
3534
registry = DummyRegistry()
3635
inst = self._makeOne(registry)
37-
inst._utcnow = lambda *a: 1
3836
content = testing.DummyResource()
3937
inst.content_types['dummy'] = lambda a: content
4038
inst.meta['dummy'] = {}

substanced/locking/__init__.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
import datetime
99
import uuid
10-
import pytz
1110

1211
from zope.interface import implementer
1312

@@ -43,6 +42,7 @@
4342
find_objectmap,
4443
find_service,
4544
get_oid,
45+
now,
4646
)
4747
from substanced.schema import Schema
4848

@@ -69,9 +69,6 @@ class UnlockError(LockingError):
6969
or ``None`` if there was no lock to unlock.
7070
"""
7171

72-
def now():
73-
return datetime.datetime.utcnow().replace(tzinfo=pytz.UTC)
74-
7572
class LockOwnerSchema(colander.SchemaNode):
7673
title = 'Owner'
7774
schema_type = colander.Int

substanced/locking/tests/test_init.py

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,6 @@ def test_ctor(self):
2020
inst = self._makeOne('lock')
2121
self.assertEqual(inst.lock, 'lock')
2222

23-
class Test_now(unittest.TestCase):
24-
def _callFUT(self):
25-
from .. import now
26-
return now()
27-
28-
def test_it(self):
29-
from pytz import UTC
30-
result = self._callFUT()
31-
self.assertEqual(result.tzinfo, UTC)
32-
3323
class TestLockOwnerSchema(unittest.TestCase):
3424
def _makeOne(self):
3525
from .. import LockOwnerSchema
@@ -210,18 +200,18 @@ def test_ctor(self):
210200
self.assertEqual(inst.comment, 'comment')
211201

212202
def test_refresh(self):
213-
import datetime
203+
from .. import now
214204
inst = self._makeOne()
215-
now = datetime.datetime.utcnow()
216-
inst.refresh(when=now)
217-
self.assertEqual(inst.last_refresh, now)
205+
NOW = now()
206+
inst.refresh(when=NOW)
207+
self.assertEqual(inst.last_refresh, NOW)
218208

219209
def test_refresh_with_timeout(self):
220-
import datetime
210+
from .. import now
221211
inst = self._makeOne()
222-
now = datetime.datetime.utcnow()
223-
inst.refresh(timeout=30, when=now)
224-
self.assertEqual(inst.last_refresh, now)
212+
NOW = now()
213+
inst.refresh(timeout=30, when=NOW)
214+
self.assertEqual(inst.last_refresh, NOW)
225215
self.assertEqual(inst.timeout, 30)
226216

227217
def test_expires_timeout_is_None(self):
@@ -231,11 +221,12 @@ def test_expires_timeout_is_None(self):
231221

232222
def test_expires_timeout_is_int(self):
233223
import datetime
224+
from .. import now
234225
inst = self._makeOne()
235226
inst.timeout = 30
236-
now = datetime.datetime.utcnow()
237-
inst.last_refresh = now
238-
self.assertEqual(inst.expires(), now + datetime.timedelta(seconds=30))
227+
NOW = now()
228+
inst.last_refresh = NOW
229+
self.assertEqual(inst.expires(), NOW + datetime.timedelta(seconds=30))
239230

240231
def test_is_valid_expires_timeout_is_None(self):
241232
inst = self._makeOne()
@@ -244,31 +235,34 @@ def test_is_valid_expires_timeout_is_None(self):
244235

245236
def test_is_valid_expires_timeout_is_int(self):
246237
import datetime
238+
from .. import now
247239
inst = self._makeOne()
248240
inst.timeout = 30
249-
now = datetime.datetime.utcnow()
250-
future = now + datetime.timedelta(seconds=60)
251-
inst.last_refresh = now
252-
self.assertTrue(inst.is_valid(now))
253-
self.assertFalse(inst.is_valid(future))
241+
NOW = now()
242+
FUTURE = NOW + datetime.timedelta(seconds=60)
243+
inst.last_refresh = NOW
244+
self.assertTrue(inst.is_valid(NOW))
245+
self.assertFalse(inst.is_valid(FUTURE))
254246

255247
def test_is_valid_expires_resource_id_exists(self):
256248
import datetime
249+
from .. import now
257250
inst = self._makeOne()
258251
inst.timeout = 30
259-
now = datetime.datetime.utcnow()
260-
inst.last_refresh = now
252+
NOW = now()
253+
inst.last_refresh = NOW
261254
inst.__objectmap__ = DummyObjectMap([1])
262-
self.assertTrue(inst.is_valid(now))
255+
self.assertTrue(inst.is_valid(NOW))
263256

264257
def test_is_valid_expires_resource_id_notexist(self):
265258
import datetime
259+
from .. import now
266260
inst = self._makeOne()
267261
inst.timeout = 30
268-
now = datetime.datetime.utcnow()
269-
inst.last_refresh = now
262+
NOW = now()
263+
inst.last_refresh = NOW
270264
inst.__objectmap__ = DummyObjectMap([])
271-
self.assertFalse(inst.is_valid(now))
265+
self.assertFalse(inst.is_valid(NOW))
272266

273267
def test_depth_wo_infinite(self):
274268
inst = self._makeOne(infinite=False)

substanced/util/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import cProfile as _profile
44
except ImportError: # pragma: no cover (pypy)
55
import profile as _profile
6+
import datetime
67
import itertools
78
import json
89
import math
@@ -30,6 +31,9 @@
3031

3132
_marker = object()
3233

34+
def now():
35+
return datetime.datetime.now(datetime.timezone.utc)
36+
3337
class JsonDict(dict):
3438
def __str__(self):
3539
return json.dumps(self)

substanced/util/test_it.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@
44

55
from . import _marker
66

7+
class Test_now(unittest.TestCase):
8+
def _callFUT(self):
9+
from . import now
10+
return now()
11+
12+
def test_it(self):
13+
import datetime
14+
15+
result = self._callFUT()
16+
self.assertIs(result.tzinfo, datetime.timezone.utc)
17+
718
class Test__postorder(unittest.TestCase):
819
def setUp(self):
920
testing.setUp()
@@ -391,8 +402,8 @@ def _callFUT(self, d):
391402

392403
def test_it(self):
393404
import calendar
394-
import datetime
395-
d = datetime.datetime.utcnow()
405+
from . import now
406+
d = now()
396407
result = self._callFUT(d)
397408
timetime = calendar.timegm(d.timetuple())
398409
val = int(timetime) // 100

0 commit comments

Comments
 (0)