Skip to content

Commit

Permalink
Raise error message when DuckDB backend not found, closes #464
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmezzetti committed Apr 21, 2023
1 parent 7e9864c commit 80d4f34
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
14 changes: 13 additions & 1 deletion src/python/txtai/database/duckdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@

from tempfile import TemporaryDirectory

import duckdb
# Conditional import
try:
import duckdb

DUCKDB = True
except ImportError:
DUCKDB = False

from .filedb import FileDB

Expand All @@ -16,6 +22,12 @@ class DuckDB(FileDB):
Database instance backed by DuckDB.
"""

def __init__(self, config):
super().__init__(config)

if not DUCKDB:
raise ImportError('DuckDB is not available - install "database" extra to enable')

def connect(self, path=":memory:"):
# Create connection and start a transaction
# pylint: disable=I1101
Expand Down
4 changes: 2 additions & 2 deletions src/python/txtai/database/encoder/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

from io import BytesIO

from .base import Encoder

# Conditional import
try:
from PIL import Image
Expand All @@ -14,6 +12,8 @@
except ImportError:
PIL = False

from .base import Encoder


class ImageEncoder(Encoder):
"""
Expand Down
5 changes: 5 additions & 0 deletions test/python/testoptional.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def setUpClass(cls):
modules = [
"annoy",
"croniter",
"duckdb",
"fastapi",
"fasttext",
"hnswlib",
Expand Down Expand Up @@ -106,8 +107,12 @@ def testDatabase(self):
Test missing database dependencies
"""

from txtai.database import DuckDB
from txtai.database import ImageEncoder

with self.assertRaises(ImportError):
DuckDB({})

with self.assertRaises(ImportError):
ImageEncoder()

Expand Down

0 comments on commit 80d4f34

Please sign in to comment.