-
Notifications
You must be signed in to change notification settings - Fork 25
Allow override of constructor args and object attrs to return None #1167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
rly
wants to merge
5
commits into
dev
Choose a base branch
from
no_override
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e022b0c
Allow override of constructor args and object attrs to return None
rly 6960201
Update CHANGELOG.md
rly eabb3b6
Merge branch 'dev' into no_override
mavaylon1 bc3f336
Merge branch 'dev' into no_override
mavaylon1 56e13b8
Merge branch 'dev' into no_override
mavaylon1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
177 changes: 177 additions & 0 deletions
177
tests/unit/build_tests/mapper_tests/test_map_override.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
from hdmf.build.builders import DatasetBuilder, GroupBuilder | ||
from hdmf.container import Container | ||
from hdmf.spec.spec import AttributeSpec, DatasetSpec, GroupSpec | ||
from hdmf.utils import docval, getargs | ||
from hdmf.build import ObjectMapper | ||
from uuid import uuid4 | ||
|
||
from ...helpers.utils import create_test_type_map, CORE_NAMESPACE | ||
|
||
|
||
class Bar(Container): | ||
@docval( | ||
{"name": "name", "type": str, "doc": "the name of this Foo"}, | ||
{"name": "my_data", "type": ("array_data", "data"), "doc": "some data"}, | ||
{"name": "attr1", "type": str, "doc": "an attribute", "default": None}, | ||
) | ||
def __init__(self, **kwargs): | ||
name, my_data, attr1 = getargs("name", "my_data", "attr1", kwargs) | ||
super().__init__(name=name) | ||
self.__data = my_data | ||
self.__attr1 = attr1 | ||
|
||
@property | ||
def my_data(self): | ||
return self.__data | ||
|
||
@property | ||
def attr1(self): | ||
return self.__attr1 | ||
|
||
|
||
def test_carg_override(): | ||
"""Test that a constructor argument can be overridden by a custom mapper.""" | ||
|
||
class CustomBarMapper(ObjectMapper): | ||
|
||
@ObjectMapper.constructor_arg("attr1") | ||
def attr1_carg(self, builder, manager): | ||
"""When constructing a Bar object, use "custom" as the value for the "attr1" argument.""" | ||
return "custom" | ||
|
||
bar_spec = GroupSpec( | ||
"A test group specification with a data type", | ||
data_type_def="Bar", | ||
datasets=[ | ||
DatasetSpec( | ||
name="my_data", | ||
doc="an example 1D int dataset", | ||
dtype="int", | ||
shape=[None], | ||
) | ||
], | ||
attributes=[ | ||
AttributeSpec(name="attr1", doc="an example string attribute", dtype="text"), | ||
], | ||
) | ||
specs = [bar_spec] | ||
container_classes = {"Bar": Bar} | ||
mappers = {"Bar": CustomBarMapper} | ||
type_map = create_test_type_map(specs, container_classes, mappers) | ||
|
||
bar_builder = GroupBuilder( | ||
name='my_bar', | ||
datasets=[DatasetBuilder(name='my_data', data=[1, 2, 3])], | ||
attributes={'attr1': 'value1', 'namespace': CORE_NAMESPACE, 'object_id': str(uuid4()), 'data_type': 'Bar'} | ||
) | ||
bar = type_map.construct(bar_builder) | ||
assert bar.attr1 == 'custom' | ||
|
||
|
||
def test_carg_override_none(): | ||
"""Test that the constructor_arg method can return None to indicate that the argument should not be set.""" | ||
|
||
class CustomBarMapper(ObjectMapper): | ||
|
||
@ObjectMapper.constructor_arg("attr1") | ||
def attr1_carg(self, builder, manager): | ||
"""When constructing a Bar object, use None as the value for the "attr1" argument.""" | ||
return None | ||
|
||
bar_spec = GroupSpec( | ||
"A test group specification with a data type", | ||
data_type_def="Bar", | ||
datasets=[ | ||
DatasetSpec( | ||
name="my_data", | ||
doc="an example 1D int dataset", | ||
dtype="int", | ||
shape=[None], | ||
) | ||
], | ||
attributes=[ # attr1 is optional | ||
AttributeSpec(name="attr1", doc="an example string attribute", dtype="text", required=False), | ||
], | ||
) | ||
specs = [bar_spec] | ||
container_classes = {"Bar": Bar} | ||
mappers = {"Bar": CustomBarMapper} | ||
type_map = create_test_type_map(specs, container_classes, mappers) | ||
|
||
bar_builder = GroupBuilder( | ||
name='my_bar', | ||
datasets=[DatasetBuilder(name='my_data', data=[1, 2, 3])], | ||
attributes={'attr1': 'value1', 'namespace': CORE_NAMESPACE, 'object_id': str(uuid4()), 'data_type': 'Bar'} | ||
) | ||
bar = type_map.construct(bar_builder) | ||
assert bar.attr1 is None | ||
|
||
|
||
def test_object_attr_override(): | ||
"""Test that an object attribute can be overridden by a custom mapper.""" | ||
|
||
class CustomBarMapper(ObjectMapper): | ||
|
||
@ObjectMapper.object_attr("attr1") | ||
def attr1_attr(self, container, manager): | ||
"""When building a Bar object, use "custom" as the value for the "attr1" attribute.""" | ||
return "custom" | ||
|
||
bar_spec = GroupSpec( | ||
"A test group specification with a data type", | ||
data_type_def="Bar", | ||
datasets=[ | ||
DatasetSpec( | ||
name="my_data", | ||
doc="an example 1D int dataset", | ||
dtype="int", | ||
shape=[None], | ||
) | ||
], | ||
attributes=[ | ||
AttributeSpec(name="attr1", doc="an example string attribute", dtype="text"), | ||
], | ||
) | ||
specs = [bar_spec] | ||
container_classes = {"Bar": Bar} | ||
mappers = {"Bar": CustomBarMapper} | ||
type_map = create_test_type_map(specs, container_classes, mappers) | ||
|
||
bar = Bar(name='my_bar', my_data=[1, 2, 3], attr1='value1') | ||
bar_builder = type_map.build(bar) | ||
assert bar_builder.attributes['attr1'] == 'custom' | ||
|
||
|
||
def test_object_attr_override_none(): | ||
"""Test that the object_attr method can return None to indicate that the attribute should not be set.""" | ||
|
||
class CustomBarMapper(ObjectMapper): | ||
|
||
@ObjectMapper.object_attr("attr1") | ||
def attr1_attr(self, container, manager): | ||
"""When building a Bar object, use None as the value for the "attr1" attribute.""" | ||
return None | ||
|
||
bar_spec = GroupSpec( | ||
"A test group specification with a data type", | ||
data_type_def="Bar", | ||
datasets=[ | ||
DatasetSpec( | ||
name="my_data", | ||
doc="an example 1D int dataset", | ||
dtype="int", | ||
shape=[None], | ||
) | ||
], | ||
attributes=[ # attr1 is optional | ||
AttributeSpec(name="attr1", doc="an example string attribute", dtype="text", required=False), | ||
], | ||
) | ||
specs = [bar_spec] | ||
container_classes = {"Bar": Bar} | ||
mappers = {"Bar": CustomBarMapper} | ||
type_map = create_test_type_map(specs, container_classes, mappers) | ||
|
||
bar = Bar(name='my_bar', my_data=[1, 2, 3], attr1='value1') | ||
bar_builder = type_map.build(bar) | ||
assert 'attr1' not in bar_builder.attributes |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.