Skip to content

🐛 Bug Report: TypeError in create_rdf_store() when given None as input #149

@cedricdcc

Description

@cedricdcc

When calling create_rdf_store() with None as one of the inputs, a TypeError is raised. This happens because the function does not properly handle the case where None is passed in combination with other arguments, resulting in a NoneType being unpacked.

Stack Trace

TypeError: sema.commons.store.build.create_rdf_store() argument after * must be an iterable, not NoneType

Code Example

create_rdf_store(None)

Expected Behavior:
None should be safely ignored, and the function should return a MemoryRDFStore in this case.

Actual Behavior:
A TypeError is raised due to unpacking None.


🛠️ Proposed Solution

Modify the code to ensure that None values are properly filtered and handled before unpacking store_info. Specifically, ensure that store_info is always a valid iterable.

Current Code:

def create_rdf_store(*store_info) -> RDFStore:
    store_info = [el for el in store_info if el is not None]
    assert len(store_info) <= 2, "Too many arguments to create store {store_info=}"
    
    if len(store_info) == 0:
        return MemoryRDFStore()
    return URIRDFStore(*store_info)

Proposed Fix:

Update the function to avoid unpacking store_info when it is empty or contains only None. Use a fallback to ensure a valid iterable is passed to URIRDFStore.

Suggested Code Update:

def create_rdf_store(*store_info) -> RDFStore:
    """Creates an rdf_store based on the passed non-None arguments.
    0 of those arguments yields a MemoryRDFStore,
    1-2 will be passed as read_uri and write_uri to URIRDFStore.
    Anything beyond is unacceptable.
    """
    store_info = tuple(el for el in store_info if el is not None)  # Ensure a valid tuple

    assert len(store_info) <= 2, f"Too many arguments to create store {store_info=}"

    if len(store_info) == 0:
        return MemoryRDFStore()

    return URIRDFStore(*store_info)  # Properly unpack only valid elements

🔬 Test Code

Here’s a test to confirm the bug and validate the fix:

def test_create_rdf_store():
    assert isinstance(create_rdf_store(), MemoryRDFStore)  # No input → MemoryRDFStore
    assert isinstance(create_rdf_store(None), MemoryRDFStore)  # None input → MemoryRDFStore
    assert isinstance(create_rdf_store("read_uri"), URIRDFStore)  # Single valid input → URIRDFStore
    assert isinstance(create_rdf_store("read_uri", "write_uri"), URIRDFStore)  # Two valid inputs → URIRDFStore

    # Test for failure if more than two arguments are passed
    try:
        create_rdf_store("read_uri", "write_uri", "extra_uri")
    except AssertionError as e:
        assert "Too many arguments" in str(e)

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions