Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# Changelog

## Version 0.3.0 - 0.3.2
## Version 0.3.0 - 0.3.4

- Provide a base `BiocObject` class similar to the `Annotated` class in Bioconductor. The class provides `metadata` slot, accessors and validation functions.
- Renaming code files to follow pep guidelines
- Update Github actions and workflow to the new biocsetup versions
- Changes to improve `NamedList`, `Names` classes
- get name at index
Expand Down
10 changes: 8 additions & 2 deletions src/biocutils/biocobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@ def sanitize_metadata(metadata: Any) -> NamedList:
"""Sanitize metadata input to a NamedList."""
if metadata is None:
return NamedList()

if isinstance(metadata, NamedList):
return metadata

if isinstance(metadata, dict):
return NamedList.from_dict(metadata)

if isinstance(metadata, list):
return NamedList.from_list(metadata)

raise TypeError(f"`metadata` must be a dictionary or NamedList, provided {type(metadata).__name__}.")


Expand All @@ -45,10 +50,11 @@ def __init__(self, metadata: Optional[Union[Dict[str, Any], NamedList]] = None,
_validate:
Whether to validate the input. Defaults to True.
"""
_meta = sanitize_metadata(metadata)
if _validate and metadata is not None:
_validate_metadata(metadata)
_validate_metadata(_meta)

self._metadata = sanitize_metadata(metadata)
self._metadata = _meta

def _define_output(self, in_place: bool = False) -> BiocObject:
"""Internal utility to handle in-place vs copy-on-modify."""
Expand Down
8 changes: 8 additions & 0 deletions tests/test_biocobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ def test_init_with_dict():
assert isinstance(obj.metadata, NamedList)
assert len(obj.metadata) == 2

def test_init_with_list():
"""Test initialization with a list."""
meta = ["jkanche", 1]
obj = BiocObject(metadata=meta)

assert isinstance(obj.metadata, NamedList)
assert len(obj.metadata) == 2

def test_init_validation():
"""Test that invalid metadata raises TypeError."""
with pytest.raises(TypeError, match="must be a dictionary or NamedList"):
Expand Down