Skip to content

Commit

Permalink
Merge f6b74ba into e6a1f8a
Browse files Browse the repository at this point in the history
  • Loading branch information
toslunar committed Aug 22, 2017
2 parents e6a1f8a + f6b74ba commit 759f4d0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
7 changes: 7 additions & 0 deletions chainer/link.py
Expand Up @@ -820,6 +820,13 @@ def __init__(self, *links):
for link in links:
self.add_link(link)

def __setattr__(self, name, value):
if self.within_init_scope and isinstance(value, Link):
raise TypeError(
'cannot register a new link'
' within a "with chainlist.init_scope():" block.')
super(ChainList, self).__setattr__(name, value)

def __getitem__(self, index):
"""Returns the child at given index.
Expand Down
12 changes: 12 additions & 0 deletions tests/chainer_tests/test_link.py
Expand Up @@ -746,6 +746,18 @@ def test_append(self):
self.assertIs(self.c2[1], self.l3)
self.assertEqual(self.l3.name, '1')

def test_assign_param_in_init_scope(self):
p = chainer.Parameter()
with self.c1.init_scope():
self.c1.p = p
self.assertIn(p, self.c1.params())

def test_assign_link_in_init_scope(self):
l = chainer.Link()
with self.c1.init_scope():
with self.assertRaises(TypeError):
self.c1.l = l

def test_iter(self):
links = list(self.c2)
self.assertEqual(2, len(links))
Expand Down

0 comments on commit 759f4d0

Please sign in to comment.