Skip to content

Commit

Permalink
Merge pull request #161 from enoch3712/139-change-name-documentloader…
Browse files Browse the repository at this point in the history
…documentai

name convention changed with warning
  • Loading branch information
enoch3712 authored Dec 28, 2024
2 parents 9ac8dab + 4c41f24 commit bb46914
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
6 changes: 5 additions & 1 deletion extract_thinker/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
from .document_loader.document_loader_doc2txt import DocumentLoaderDoc2txt
from .document_loader.document_loader_aws_textract import DocumentLoaderAWSTextract
from .document_loader.document_loader_llm_image import DocumentLoaderLLMImage
from .document_loader.document_loader_google_document_ai import DocumentLoaderDocumentAI
from .document_loader.document_loader_google_document_ai import (
DocumentLoaderGoogleDocumentAI,
DocumentLoaderDocumentAI,
)

__all__ = [
'Extractor',
Expand All @@ -39,6 +42,7 @@
'DocumentLoaderTxt',
'DocumentLoaderDoc2txt',
'DocumentLoaderAWSTextract',
'DocumentLoaderGoogleDocumentAI',
'DocumentLoaderDocumentAI',
'Classification',
'ClassificationResponse',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from cachetools.keys import hashkey
from cachetools import cachedmethod
from operator import attrgetter
import warnings

class Config:
def __init__(
Expand All @@ -21,7 +22,7 @@ def __init__(
self.page_range = page_range


class DocumentLoaderDocumentAI(CachedDocumentLoader):
class DocumentLoaderGoogleDocumentAI(CachedDocumentLoader):
"""Loader for documents using Google Document AI."""

SUPPORTED_FORMATS = [
Expand Down Expand Up @@ -179,3 +180,14 @@ def _get_page_key_value_pairs(page: documentai.Document.Page) -> List[Dict[str,
for kv_pair in page.key_value_pairs
]


# Create an alias with deprecation warning
class DocumentLoaderDocumentAI(DocumentLoaderGoogleDocumentAI):
def __init__(self, *args, **kwargs):
warnings.warn(
"DocumentLoaderDocumentAI is deprecated and will be removed in 0.1.0"
"Use DocumentLoaderGoogleDocumentAI instead.",
DeprecationWarning,
stacklevel=2
)
super().__init__(*args, **kwargs)
23 changes: 21 additions & 2 deletions tests/test_document_loader_google_document_ai.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,40 @@
import os
import pytest
import warnings
from dotenv import load_dotenv
from extract_thinker.document_loader.document_loader_google_document_ai import DocumentLoaderDocumentAI
from extract_thinker.document_loader.document_loader_google_document_ai import (
DocumentLoaderGoogleDocumentAI,
DocumentLoaderDocumentAI
)
from tests.test_document_loader_base import BaseDocumentLoaderTest

load_dotenv()

class TestDocumentLoaderGoogleDocumentAI(BaseDocumentLoaderTest):
@pytest.fixture
def loader(self):
return DocumentLoaderDocumentAI(
return DocumentLoaderGoogleDocumentAI(
project_id=os.getenv("DOCUMENTAI_PROJECT_ID"),
location=os.getenv("DOCUMENTAI_LOCATION"),
processor_id=os.getenv("DOCUMENTAI_PROCESSOR_ID"),
credentials=os.getenv("DOCUMENTAI_GOOGLE_CREDENTIALS")
)

def test_deprecation_warning(self):
"""Test that using old class name raises deprecation warning"""
with pytest.warns(DeprecationWarning) as record:
DocumentLoaderDocumentAI(
project_id=os.getenv("DOCUMENTAI_PROJECT_ID"),
location=os.getenv("DOCUMENTAI_LOCATION"),
processor_id=os.getenv("DOCUMENTAI_PROCESSOR_ID"),
credentials=os.getenv("DOCUMENTAI_GOOGLE_CREDENTIALS")
)

# Verify the warning message
assert len(record) == 1
assert "DocumentLoaderDocumentAI is deprecated" in str(record[0].message)
assert "Use DocumentLoaderGoogleDocumentAI instead" in str(record[0].message)

@pytest.fixture
def test_file_path(self):
current_dir = os.path.dirname(os.path.abspath(__file__))
Expand Down

0 comments on commit bb46914

Please sign in to comment.