Skip to content

bugfix/Fix support for file data models #35

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

Merged
merged 1 commit into from
Jan 2, 2025
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.0.16

* **Bugfix for file data deserialization**

## 0.0.15

* **Bugfix for file data serialization**
Expand Down
22 changes: 10 additions & 12 deletions test/api/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,15 @@ def generic_validation(self):
]


@pytest.mark.parametrize(
"file_data", mock_file_data, ids=[type(fd).__name__ for fd in mock_file_data]
)
@pytest.fixture
def file_data() -> FileData:
return FileData(
identifier="mock file data",
connector_type="CON",
source_identifiers=SourceIdentifiers(filename="n", fullpath="n"),
)


def test_async_sample_function(file_data):
from test.assets.async_typed_dict_response import async_sample_function as test_fn

Expand All @@ -56,9 +62,6 @@ def test_async_sample_function(file_data):
assert output == {"response": {"a_out": 1, "b_out": 2}}


@pytest.mark.parametrize(
"file_data", mock_file_data, ids=[type(fd).__name__ for fd in mock_file_data]
)
def test_dataclass_response(file_data):
from test.assets.dataclass_response import sample_function_with_path as test_fn

Expand All @@ -78,12 +81,10 @@ def test_dataclass_response(file_data):
"resolved": str(current_path.resolve()),
"b": "2",
"c": 1,
"p": not isinstance(file_data, BatchFileData),
}


@pytest.mark.parametrize(
"file_data", mock_file_data, ids=[type(fd).__name__ for fd in mock_file_data]
)
def test_empty_input_and_output(file_data):
from test.assets.empty_input_and_response import SampleClass as TestClass

Expand All @@ -98,9 +99,6 @@ def test_empty_input_and_output(file_data):
assert not output


@pytest.mark.parametrize(
"file_data", mock_file_data, ids=[type(fd).__name__ for fd in mock_file_data]
)
def test_filedata_meta(file_data):
from test.assets.filedata_meta import Input
from test.assets.filedata_meta import process_input as test_fn
Expand Down
10 changes: 9 additions & 1 deletion test/assets/dataclass_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from pathlib import Path
from typing import Any, Optional, TypedDict

from unstructured_ingest.v2.interfaces import BatchFileData, FileData


class SampleFunctionResponse(TypedDict):
response: dict[str, Any]
Expand All @@ -20,10 +22,11 @@ class SampleFunctionWithPathResponse:
resolved: str
b: str
c: int
p: bool


def sample_function_with_path(
b: str, c: int, a: Optional[Path] = None
file_data: FileData, b: str, c: int, a: Optional[Path] = None
) -> SampleFunctionWithPathResponse:
s: list[Any] = [type(a).__name__, f"[exists: {a.exists()}]", a.resolve()] if a else []
s.extend([b, c])
Expand All @@ -33,5 +36,10 @@ def sample_function_with_path(
"resolved": a.resolve(),
"b": b,
"c": c,
"p": (
False
if isinstance(file_data, BatchFileData)
else file_data.source_identifiers.relative_path is not None
),
}
return SampleFunctionWithPathResponse(**resp)
2 changes: 1 addition & 1 deletion unstructured_platform_plugins/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.0.15" # pragma: no cover
__version__ = "0.0.16" # pragma: no cover
6 changes: 6 additions & 0 deletions unstructured_platform_plugins/etl_uvicorn/api_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
from pydantic import BaseModel, Field, create_model
from starlette.responses import RedirectResponse
from unstructured_ingest.v2.interfaces.file_data import file_data_from_dict
from uvicorn.config import LOG_LEVELS
from uvicorn.importer import import_from_string

Expand Down Expand Up @@ -200,6 +201,11 @@ async def run_job(request: input_schema_model) -> ResponseType:
log_func_and_body(func=func, body=request.json())
# Create dictionary from pydantic model while preserving underlying types
request_dict = {f: getattr(request, f) for f in request.model_fields}
# Make sure nested classes get instantiated correctly
if "file_data" in request_dict:
request_dict["file_data"] = file_data_from_dict(
request_dict["file_data"].model_dump()
)
map_inputs(func=func, raw_inputs=request_dict)
if logger.level == LOG_LEVELS.get("trace", logging.NOTSET):
logger.log(level=logger.level, msg=f"passing inputs to function: {request_dict}")
Expand Down
Loading