Skip to content

Commit 2e2e5da

Browse files
authored
Merge pull request #460 from Jamim/fix/warnings
Reduce count of warnings
2 parents c3231ac + 614ade1 commit 2e2e5da

File tree

8 files changed

+85
-39
lines changed

8 files changed

+85
-39
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ __pycache__
1414
.coverage
1515
.cache
1616
absolute.json
17+
htmlcov/
1718

1819
# Sphinx documentation
1920
_build

jupyter_client/connect.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
bytes_to_str, cast_bytes, cast_bytes_py2, string_types,
3030
)
3131
from traitlets import (
32-
Bool, Integer, Unicode, CaselessStrEnum, Instance, Type,
32+
Bool, Integer, Unicode, CaselessStrEnum, Instance, Type, observe
3333
)
3434
from jupyter_core.paths import jupyter_data_dir, jupyter_runtime_dir
3535

@@ -323,8 +323,9 @@ def _ip_default(self):
323323
else:
324324
return localhost()
325325

326-
def _ip_changed(self, name, old, new):
327-
if new == '*':
326+
@observe('ip')
327+
def _ip_changed(self, change):
328+
if change['new'] == '*':
328329
self.ip = '0.0.0.0'
329330

330331
# protected traits

jupyter_client/ioloop/manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from __future__ import absolute_import
77

8-
from zmq.eventloop import ioloop
8+
from tornado import ioloop
99
from zmq.eventloop.zmqstream import ZMQStream
1010

1111
from traitlets import (

jupyter_client/manager.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
from ipython_genutils.importstring import import_item
1818
from .localinterfaces import is_local_ip, local_ips
1919
from traitlets import (
20-
Any, Float, Instance, Unicode, List, Bool, Type, DottedObjectName, Dict
20+
Any, Float, Instance, Unicode, List, Bool, Type, DottedObjectName, Dict,
21+
observe
2122
)
2223
from jupyter_client import (
2324
launch_kernel,
@@ -46,8 +47,9 @@ def _context_default(self):
4647
def _client_factory_default(self):
4748
return import_item(self.client_class)
4849

49-
def _client_class_changed(self, name, old, new):
50-
self.client_factory = import_item(str(new))
50+
@observe('client_class')
51+
def _client_class_changed(self, change):
52+
self.client_factory = import_item(str(change['new']))
5153

5254
# The kernel process with which the KernelManager is communicating.
5355
# generally a Popen instance
@@ -68,9 +70,10 @@ def _kernel_spec_manager_changed(self):
6870

6971
kernel_name = Unicode(kernelspec.NATIVE_KERNEL_NAME)
7072

71-
def _kernel_name_changed(self, name, old, new):
73+
@observe('kernel_name')
74+
def _kernel_name_changed(self, change):
7275
self._kernel_spec = None
73-
if new == 'python':
76+
if change['new'] == 'python':
7477
self.kernel_name = kernelspec.NATIVE_KERNEL_NAME
7578

7679
_kernel_spec = None

jupyter_client/multikernelmanager.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from traitlets.config.configurable import LoggingConfigurable
1414
from ipython_genutils.importstring import import_item
1515
from traitlets import (
16-
Instance, Dict, List, Unicode, Any, DottedObjectName
16+
Instance, Dict, Unicode, Any, DottedObjectName, observe
1717
)
1818
from ipython_genutils.py3compat import unicode_type
1919

@@ -54,8 +54,10 @@ class MultiKernelManager(LoggingConfigurable):
5454
subclassing of the KernelManager for customized behavior.
5555
"""
5656
)
57-
def _kernel_manager_class_changed(self, name, old, new):
58-
self.kernel_manager_factory = import_item(new)
57+
58+
@observe('kernel_manager_class')
59+
def _kernel_manager_class_changed(self, change):
60+
self.kernel_manager_factory = import_item(change['new'])
5961

6062
kernel_manager_factory = Any(help="this is kernel_manager_class after import")
6163
def _kernel_manager_factory_default(self):

jupyter_client/session.py

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ def compare_digest(a,b): return a == b
6161
from jupyter_client.jsonutil import extract_dates, squash_dates, date_default
6262
from ipython_genutils.py3compat import (str_to_bytes, str_to_unicode, unicode_type,
6363
iteritems)
64-
from traitlets import (CBytes, Unicode, Bool, Any, Instance, Set,
65-
DottedObjectName, CUnicode, Dict, Integer,
66-
TraitError,
64+
from traitlets import (
65+
CBytes, Unicode, Bool, Any, Instance, Set, DottedObjectName, CUnicode,
66+
Dict, Integer, TraitError, observe
6767
)
6868
from jupyter_client import protocol_version
6969
from jupyter_client.adapter import adapt
@@ -180,8 +180,10 @@ class SessionFactory(LoggingConfigurable):
180180
"""
181181

182182
logname = Unicode('')
183-
def _logname_changed(self, name, old, new):
184-
self.log = logging.getLogger(new)
183+
184+
@observe('logname')
185+
def _logname_changed(self, change):
186+
self.log = logging.getLogger(change['new'])
185187

186188
# not configurable:
187189
context = Instance('zmq.Context')
@@ -311,7 +313,10 @@ class Session(Configurable):
311313
help="""The name of the packer for serializing messages.
312314
Should be one of 'json', 'pickle', or an import name
313315
for a custom callable serializer.""")
314-
def _packer_changed(self, name, old, new):
316+
317+
@observe('packer')
318+
def _packer_changed(self, change):
319+
new = change['new']
315320
if new.lower() == 'json':
316321
self.pack = json_packer
317322
self.unpack = json_unpacker
@@ -326,7 +331,10 @@ def _packer_changed(self, name, old, new):
326331
unpacker = DottedObjectName('json', config=True,
327332
help="""The name of the unpacker for unserializing messages.
328333
Only used with custom functions for `packer`.""")
329-
def _unpacker_changed(self, name, old, new):
334+
335+
@observe('unpacker')
336+
def _unpacker_changed(self, change):
337+
new = change['new']
330338
if new.lower() == 'json':
331339
self.pack = json_packer
332340
self.unpack = json_unpacker
@@ -345,7 +353,8 @@ def _session_default(self):
345353
self.bsession = u.encode('ascii')
346354
return u
347355

348-
def _session_changed(self, name, old, new):
356+
@observe('session')
357+
def _session_changed(self, change):
349358
self.bsession = self.session.encode('ascii')
350359

351360
# bsession is the session as bytes
@@ -368,13 +377,17 @@ def _session_changed(self, name, old, new):
368377
def _key_default(self):
369378
return new_id_bytes()
370379

371-
def _key_changed(self):
380+
@observe('key')
381+
def _key_changed(self, change):
372382
self._new_auth()
373383

374384
signature_scheme = Unicode('hmac-sha256', config=True,
375385
help="""The digest scheme used to construct the message signatures.
376386
Must have the form 'hmac-HASH'.""")
377-
def _signature_scheme_changed(self, name, old, new):
387+
388+
@observe('signature_scheme')
389+
def _signature_scheme_changed(self, change):
390+
new = change['new']
378391
if not new.startswith('hmac-'):
379392
raise TraitError("signature_scheme must start with 'hmac-', got %r" % new)
380393
hash_name = new.split('-', 1)[1]
@@ -406,8 +419,10 @@ def _new_auth(self):
406419

407420
keyfile = Unicode('', config=True,
408421
help="""path to file containing execution key.""")
409-
def _keyfile_changed(self, name, old, new):
410-
with open(new, 'rb') as f:
422+
423+
@observe('keyfile')
424+
def _keyfile_changed(self, change):
425+
with open(change['new'], 'rb') as f:
411426
self.key = f.read().strip()
412427

413428
# for protecting against sends from forks
@@ -416,13 +431,19 @@ def _keyfile_changed(self, name, old, new):
416431
# serialization traits:
417432

418433
pack = Any(default_packer) # the actual packer function
419-
def _pack_changed(self, name, old, new):
434+
435+
@observe('pack')
436+
def _pack_changed(self, change):
437+
new = change['new']
420438
if not callable(new):
421439
raise TypeError("packer must be callable, not %s"%type(new))
422440

423441
unpack = Any(default_unpacker) # the actual packer function
424-
def _unpack_changed(self, name, old, new):
442+
443+
@observe('unpack')
444+
def _unpack_changed(self, change):
425445
# unpacker is not checked - it is assumed to be
446+
new = change['new']
426447
if not callable(new):
427448
raise TypeError("unpacker must be callable, not %s"%type(new))
428449

jupyter_client/tests/test_jsonutil.py

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,50 @@
1414
# py2
1515
import mock
1616

17+
import pytest
1718
from dateutil.tz import tzlocal, tzoffset
1819
from jupyter_client import jsonutil
1920
from jupyter_client.session import utcnow
2021

2122

23+
REFERENCE_DATETIME = datetime.datetime(
24+
2013, 7, 3, 16, 34, 52, 249482, tzlocal()
25+
)
26+
27+
28+
def test_extract_date_from_naive():
29+
ref = REFERENCE_DATETIME
30+
timestamp = '2013-07-03T16:34:52.249482'
31+
32+
with pytest.deprecated_call(match='Interpreting naive datetime as local'):
33+
extracted = jsonutil.extract_dates(timestamp)
34+
35+
assert isinstance(extracted, datetime.datetime)
36+
assert extracted.tzinfo is not None
37+
assert extracted.tzinfo.utcoffset(ref) == tzlocal().utcoffset(ref)
38+
assert extracted == ref
39+
40+
2241
def test_extract_dates():
42+
ref = REFERENCE_DATETIME
2343
timestamps = [
24-
'2013-07-03T16:34:52.249482',
2544
'2013-07-03T16:34:52.249482Z',
2645
'2013-07-03T16:34:52.249482-0800',
2746
'2013-07-03T16:34:52.249482+0800',
2847
'2013-07-03T16:34:52.249482-08:00',
2948
'2013-07-03T16:34:52.249482+08:00',
3049
]
3150
extracted = jsonutil.extract_dates(timestamps)
32-
ref = extracted[0]
3351
for dt in extracted:
3452
assert isinstance(dt, datetime.datetime)
35-
assert dt.tzinfo != None
53+
assert dt.tzinfo is not None
54+
55+
assert extracted[0].tzinfo.utcoffset(ref) == timedelta(0)
56+
assert extracted[1].tzinfo.utcoffset(ref) == timedelta(hours=-8)
57+
assert extracted[2].tzinfo.utcoffset(ref) == timedelta(hours=8)
58+
assert extracted[3].tzinfo.utcoffset(ref) == timedelta(hours=-8)
59+
assert extracted[4].tzinfo.utcoffset(ref) == timedelta(hours=8)
3660

37-
assert extracted[0].tzinfo.utcoffset(ref) == tzlocal().utcoffset(ref)
38-
assert extracted[1].tzinfo.utcoffset(ref) == timedelta(0)
39-
assert extracted[2].tzinfo.utcoffset(ref) == timedelta(hours=-8)
40-
assert extracted[3].tzinfo.utcoffset(ref) == timedelta(hours=8)
41-
assert extracted[4].tzinfo.utcoffset(ref) == timedelta(hours=-8)
42-
assert extracted[5].tzinfo.utcoffset(ref) == timedelta(hours=8)
4361

4462
def test_parse_ms_precision():
4563
base = '2013-07-03T16:34:52'
@@ -56,14 +74,14 @@ def test_parse_ms_precision():
5674
assert isinstance(parsed, str)
5775

5876

59-
6077
def test_date_default():
6178
naive = datetime.datetime.now()
6279
local = tzoffset('Local', -8 * 3600)
6380
other = tzoffset('Other', 2 * 3600)
6481
data = dict(naive=naive, utc=utcnow(), withtz=naive.replace(tzinfo=other))
6582
with mock.patch.object(jsonutil, 'tzlocal', lambda : local):
66-
jsondata = json.dumps(data, default=jsonutil.date_default)
83+
with pytest.deprecated_call(match='Please add timezone info'):
84+
jsondata = json.dumps(data, default=jsonutil.date_default)
6785
assert "Z" in jsondata
6886
assert jsondata.count("Z") == 1
6987
extracted = jsonutil.extract_dates(json.loads(jsondata))

jupyter_client/tests/test_kernelmanager.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def test_start_new_kernel(self):
131131
self.assertTrue(km.is_alive())
132132
self.assertTrue(kc.is_alive())
133133

134-
@pytest.mark.parallel
134+
135135
class TestParallel:
136136

137137
@pytest.fixture(autouse=True)
@@ -244,4 +244,4 @@ def execute(cmd):
244244
assert km.is_alive()
245245
execute('check')
246246

247-
km.shutdown_kernel()
247+
km.shutdown_kernel()

0 commit comments

Comments
 (0)