Skip to content
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

Streamline optional dependency sqlmodel #116

Closed
nsheff opened this issue Nov 9, 2023 · 2 comments
Closed

Streamline optional dependency sqlmodel #116

nsheff opened this issue Nov 9, 2023 · 2 comments
Milestone

Comments

@nsheff
Copy link
Contributor

nsheff commented Nov 9, 2023

In db_helpers.py, we're using a try block to wrap the sqlmodel import:

try:
    from sqlmodel import and_, or_, Integer, Float, String, Boolean
except:
    pass

But in db_parsed_schema.py, and dbbackend.py we are not:

from sqlmodel import Field, SQLModel

Will this break things if sqlmodel is not installed?

Also, is there a better way to isolate the sqlmodel optionality into a single place? For example, I'm thinking of something like importing sqlmodel in an __init__.py under backends/db_backend` and then having these show up in everything in that module, so that it doesn't have to be repeatedly imported? (not sure if this will work)

@nsheff nsheff changed the title Optional dependency sqlmodel Streamline optional dependency sqlmodel Nov 9, 2023
@nsheff
Copy link
Contributor Author

nsheff commented Nov 9, 2023

One way I'm doing this in geniml that is working is something like this:

# first, set a const so we know whether the dep is satisfied or not
try:
    import hnswlib
    DEP_HNSWLIB = True
except ImportError:
    DEP_HNSWLIB = False
    _LOGGER.error(
        "HNSWBackend requires hnswlib. Install hnswlib, or ignore this if you don't need HNSWBackend"
    )


if not DEP_HNSWLIB:
    # Dependency not met;  make dummy class so code can instantiate, but it brakes if actually used
    class HNSWBackend(EmSearchBackend):
        pass

else:
    # dependency met, make the real class
    class HNSWBackend(EmSearchBackend):
        """A search backend that uses a local HNSW index to store and search embeddings"""
        ...

it's nice to keep this logic to a single place to make maintenance easier

@donaldcampbelljr donaldcampbelljr added this to the 0.8.0 milestone Jan 17, 2024
@donaldcampbelljr
Copy link
Contributor

This is currently the code for checking dependencies for the DB backend (within pipestat.py).:

try:
from pipestat.backends.db_backend.db_parsed_schema import (
ParsedSchemaDB as ParsedSchema,
)
except ImportError:
from .parsed_schema import ParsedSchema
try:
from pipestat.backends.db_backend.dbbackend import DBBackend
from pipestat.backends.db_backend.db_helpers import construct_db_url
except ImportError:
# We let this pass, but if the user attempts to create DBBackend, check_dependencies raises exception.
pass
_LOGGER = getLogger(PKG_NAME)
def check_dependencies(dependency_list: list = None, msg: str = None):
"""Decorator to check that the dependency list has successfully been imported."""
def wrapper(func):
def inner(*args, **kwargs):
dependencies_satisfied = True
if dependency_list is not None:
for i in dependency_list:
if i not in globals():
_LOGGER.warning(msg=f"Missing dependency: {i}")
dependencies_satisfied = False
if dependencies_satisfied is False:
raise PipestatDependencyError(msg=msg)
return func(*args, **kwargs)
return inner
return wrapper

To answer the above question

But in db_parsed_schema.py, and dbbackend.py we are not:

from sqlmodel import Field, SQLModel

Will this break things if sqlmodel is not installed?

Lines 55-90 will allow for sqlmodel to not be installed.

Also, I do think that the lines from dbhelpers.py do not need to be in a try: except:

try:
from sqlmodel import and_, or_, Integer, Float, String, Boolean
except ImportError as e:
pass

    from sqlmodel import and_, or_, Integer, Float, String, Boolean

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants