Skip to content

Commit

Permalink
Fix bug whereby __init__ methods with local variables did not have th…
Browse files Browse the repository at this point in the history
…eir values extracted properly
  • Loading branch information
JohnVinyard committed Jun 14, 2018
1 parent 2b3b681 commit a90fab2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
19 changes: 18 additions & 1 deletion zounds/persistence/test_util.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import unittest2
from util import decode_timedelta
from util import decode_timedelta, extract_init_args
from zounds.timeseries import Seconds


Expand All @@ -8,3 +8,20 @@ def test_already_decoded_instance_is_returned(self):
td = Seconds(10)
decoded = decode_timedelta(td)
self.assertEqual(td, decoded)


class ExtractInitArgsTests(unittest2.TestCase):
def test_should_not_include_locals(self):
class Blah(object):
def __init__(self, x, y):
super(Blah, self).__init__()
self.x = x
z = 10
self.y = y + z

b = Blah(20, 30)
args = extract_init_args(b)
self.assertEqual(2, len(args))
self.assertIn(20, args)
self.assertIn(40, args)
self.assertNotIn(10, args)
3 changes: 2 additions & 1 deletion zounds/persistence/util.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import base64
import re
import numpy as np
import inspect

TIMEDELTA_DTYPE_RE = re.compile(r'\[(?P<dtype>[^\]]+)\]')

Expand Down Expand Up @@ -28,5 +29,5 @@ def extract_init_args(instance):
cls = instance.__class__
args = filter(
lambda x: x != 'self',
cls.__init__.im_func.func_code.co_varnames)
inspect.getargspec(cls.__init__).args)
return [instance.__dict__[key] for key in args]

0 comments on commit a90fab2

Please sign in to comment.