Skip to content

Commit

Permalink
Fix issue with generating class with link and name (#1006)
Browse files Browse the repository at this point in the history
  • Loading branch information
rly authored Dec 6, 2023
1 parent 434433e commit b2cc149
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 20 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
- Improve HTML rendering of tables. @bendichter [#998](https://github.com/hdmf-dev/hdmf/pull/998)
- Improved issue and PR templates. @rly [#1004](https://github.com/hdmf-dev/hdmf/pull/1004)

### Bug fixes
- Fixed issue with custom class generation when a spec has a `name`. @rly [#1006](https://github.com/hdmf-dev/hdmf/pull/1006)

## HDMF 3.11.0 (October 30, 2023)

### Enhancements
Expand Down
4 changes: 2 additions & 2 deletions src/hdmf/build/classgenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,8 @@ def process_field_spec(cls, classdict, docval_args, parent_cls, attr_name, not_i
fixed_value = getattr(field_spec, 'value', None)
if fixed_value is not None:
fields_conf['settable'] = False
if isinstance(field_spec, (BaseStorageSpec, LinkSpec)) and field_spec.data_type is not None:
# subgroups, datasets, and links with data types can have fixed names
if isinstance(field_spec, BaseStorageSpec) and field_spec.data_type is not None:
# subgroups and datasets with data types can have fixed names
fixed_name = getattr(field_spec, 'name', None)
if fixed_name is not None:
fields_conf['required_name'] = fixed_name
Expand Down
5 changes: 0 additions & 5 deletions src/hdmf/spec/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -816,11 +816,6 @@ def data_type_inc(self):
''' The data type of target specification '''
return self.get(_target_type_key)

@property
def data_type(self):
''' The data type of target specification '''
return self.get(_target_type_key)

def is_many(self):
return self.quantity not in (1, ZERO_OR_ONE)

Expand Down
18 changes: 5 additions & 13 deletions tests/unit/build_tests/test_classgenerator.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ def setUp(self):
],
links=[
LinkSpec(
doc='A composition inside with a fixed name',
doc='A composition inside without a fixed name',
name="my_baz1_link",
target_type='Baz1'
),
Expand All @@ -517,7 +517,7 @@ def test_gen_parent_class(self):
{'name': 'name', 'type': str, 'doc': 'the name of this container'},
{'name': 'my_baz1', 'doc': 'A composition inside with a fixed name', 'type': baz1_cls},
{'name': 'my_baz2', 'doc': 'A composition inside with a fixed name', 'type': baz2_cls},
{'name': 'my_baz1_link', 'doc': 'A composition inside with a fixed name', 'type': baz1_cls},
{'name': 'my_baz1_link', 'doc': 'A composition inside without a fixed name', 'type': baz1_cls},
))

def test_init_fields(self):
Expand All @@ -537,8 +537,7 @@ def test_init_fields(self):
},
{
'name': 'my_baz1_link',
'doc': 'A composition inside with a fixed name',
'required_name': 'my_baz1_link'
'doc': 'A composition inside without a fixed name',
},
))

Expand All @@ -548,7 +547,7 @@ def test_set_field(self):
baz3_cls = self.type_map.get_dt_container_cls('Baz3', CORE_NAMESPACE)
baz1 = baz1_cls(name="my_baz1")
baz2 = baz2_cls(name="my_baz2")
baz1_link = baz1_cls(name="my_baz1_link")
baz1_link = baz1_cls(name="any_name")
baz3 = baz3_cls(name="test", my_baz1=baz1, my_baz2=baz2, my_baz1_link=baz1_link)
self.assertEqual(baz3.my_baz1, baz1)
self.assertEqual(baz3.my_baz2, baz2)
Expand All @@ -573,13 +572,6 @@ def test_set_field_bad(self):
with self.assertRaisesWith(ValueError, msg):
baz3_cls(name="test", my_baz1=baz1, my_baz2=baz2, my_baz1_link=baz1_link)

baz1 = baz1_cls(name="my_baz1")
baz2 = baz2_cls(name="my_baz2")
baz1_link = baz1_cls(name="test")
msg = "Field 'my_baz1_link' on Baz3 must be named 'my_baz1_link'."
with self.assertRaisesWith(ValueError, msg):
baz3_cls(name="test", my_baz1=baz1, my_baz2=baz2, my_baz1_link=baz1_link)


class TestGetClassSeparateNamespace(TestCase):

Expand Down Expand Up @@ -1049,7 +1041,7 @@ def test_process_field_spec_link(self):
spec=GroupSpec('dummy', 'doc')
)

expected = {'__fields__': [{'name': 'attr3', 'doc': 'a link', 'required_name': 'attr3'}]}
expected = {'__fields__': [{'name': 'attr3', 'doc': 'a link'}]}
self.assertDictEqual(classdict, expected)

def test_post_process_fixed_name(self):
Expand Down

0 comments on commit b2cc149

Please sign in to comment.