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

chore: free pydantic version #6150

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion extra-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pathspec: standard,devel
filelock: standard,devel
requests: standard,devel
websockets: standard,devel
pydantic<2.0.0: core
pydantic: core
python-multipart: standard,devel
aiofiles: standard,devel
aiohttp: standard,devel
Expand Down
8 changes: 8 additions & 0 deletions jina/_docarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@

docarray_v2 = True

from jina._docarray_legacy import LegacyDocumentJina

Check warning on line 7 in jina/_docarray.py

View check run for this annotation

Codecov / codecov/patch

jina/_docarray.py#L7

Added line #L7 was not covered by tests

except ImportError:
from docarray import Document, DocumentArray

docarray_v2 = False


import pydantic

is_pydantic_v2 = pydantic.__version__.startswith('2.')

49 changes: 49 additions & 0 deletions jina/_docarray_legacy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from __future__ import annotations

Check warning on line 1 in jina/_docarray_legacy.py

View check run for this annotation

Codecov / codecov/patch

jina/_docarray_legacy.py#L1

Added line #L1 was not covered by tests

from docarray import BaseDoc
from docarray import DocList

Check warning on line 4 in jina/_docarray_legacy.py

View check run for this annotation

Codecov / codecov/patch

jina/_docarray_legacy.py#L3-L4

Added lines #L3 - L4 were not covered by tests

docarray_v2 = True

Check warning on line 6 in jina/_docarray_legacy.py

View check run for this annotation

Codecov / codecov/patch

jina/_docarray_legacy.py#L6

Added line #L6 was not covered by tests

from typing import Any, Dict, Optional, List, Union

Check warning on line 8 in jina/_docarray_legacy.py

View check run for this annotation

Codecov / codecov/patch

jina/_docarray_legacy.py#L8

Added line #L8 was not covered by tests

from docarray.typing import AnyEmbedding, AnyTensor

Check warning on line 10 in jina/_docarray_legacy.py

View check run for this annotation

Codecov / codecov/patch

jina/_docarray_legacy.py#L10

Added line #L10 was not covered by tests


class LegacyDocumentJina(BaseDoc):

Check warning on line 13 in jina/_docarray_legacy.py

View check run for this annotation

Codecov / codecov/patch

jina/_docarray_legacy.py#L13

Added line #L13 was not covered by tests
"""
This Document is the LegacyDocumentJina. It follows the same schema as in DocArray <=0.21.
It can be useful to start migrating a codebase from v1 to v2.

Nevertheless, the API is not totally compatible with DocArray <=0.21 `Document`.
Indeed, none of the method associated with `Document` are present. Only the schema
of the data is similar.

```python
from docarray import DocList
from docarray.documents.legacy import LegacyDocument
import numpy as np

doc = LegacyDocument(text='hello')
doc.url = 'http://myimg.png'
doc.tensor = np.zeros((3, 224, 224))
doc.embedding = np.zeros((100, 1))

doc.tags['price'] = 10

doc.chunks = DocList[Document]([Document() for _ in range(10)])

doc.chunks = DocList[Document]([Document() for _ in range(10)])
```

"""

tensor: Optional[AnyTensor] = None
chunks: Optional[Union[DocList[LegacyDocumentJina], List[LegacyDocumentJina]]] = None
matches: Optional[Union[DocList[LegacyDocumentJina], List[LegacyDocumentJina]]] = None
blob: Optional[bytes] = None
text: Optional[str] = None
url: Optional[str] = None
embedding: Optional[AnyEmbedding] = None
tags: Dict[str, Any] = dict()
scores: Optional[Dict[str, Any]] = None

Check warning on line 49 in jina/_docarray_legacy.py

View check run for this annotation

Codecov / codecov/patch

jina/_docarray_legacy.py#L41-L49

Added lines #L41 - L49 were not covered by tests
4 changes: 2 additions & 2 deletions jina/serve/executors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
from jina.serve.instrumentation import MetricsTimer

if docarray_v2:
from docarray.documents.legacy import LegacyDocument
from jina._docarray import LegacyDocumentJina

Check warning on line 55 in jina/serve/executors/__init__.py

View check run for this annotation

Codecov / codecov/patch

jina/serve/executors/__init__.py#L55

Added line #L55 was not covered by tests

if TYPE_CHECKING: # pragma: no cover
from opentelemetry.context.context import Context
Expand Down Expand Up @@ -257,7 +257,7 @@
from docarray import BaseDoc, DocList

default_annotations = (
DocList[LegacyDocument] if is_batch_docs else LegacyDocument
DocList[LegacyDocumentJina] if is_batch_docs else LegacyDocumentJina
)
else:
from jina import Document, DocumentArray
Expand Down
24 changes: 13 additions & 11 deletions jina/serve/runtimes/gateway/graph/topology_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import grpc.aio

from jina._docarray import DocumentArray, docarray_v2
from jina._docarray import DocumentArray, docarray_v2, is_pydantic_v2
from jina.constants import __default_endpoint__
from jina.excepts import InternalNetworkError
from jina.logging.logger import JinaLogger
Expand All @@ -18,11 +18,15 @@

if docarray_v2:
from docarray import DocList
from docarray.documents.legacy import LegacyDocument
from jina._docarray import LegacyDocumentJina

Check warning on line 21 in jina/serve/runtimes/gateway/graph/topology_graph.py

View check run for this annotation

Codecov / codecov/patch

jina/serve/runtimes/gateway/graph/topology_graph.py#L21

Added line #L21 was not covered by tests

from jina.serve.runtimes.helper import _create_pydantic_model_from_schema
if not is_pydantic_v2:
from jina.serve.runtimes.helper import _create_pydantic_model_from_schema as create_base_doc_from_schema

Check warning on line 24 in jina/serve/runtimes/gateway/graph/topology_graph.py

View check run for this annotation

Codecov / codecov/patch

jina/serve/runtimes/gateway/graph/topology_graph.py#L23-L24

Added lines #L23 - L24 were not covered by tests
else:
from docarray.utils.create_dynamic_doc_class import create_base_doc_from_schema

Check warning on line 26 in jina/serve/runtimes/gateway/graph/topology_graph.py

View check run for this annotation

Codecov / codecov/patch

jina/serve/runtimes/gateway/graph/topology_graph.py#L26

Added line #L26 was not covered by tests

legacy_doc_schema = LegacyDocument.schema()

legacy_doc_schema = LegacyDocumentJina.schema()

Check warning on line 29 in jina/serve/runtimes/gateway/graph/topology_graph.py

View check run for this annotation

Codecov / codecov/patch

jina/serve/runtimes/gateway/graph/topology_graph.py#L29

Added line #L29 was not covered by tests


class TopologyGraph:
Expand Down Expand Up @@ -218,8 +222,6 @@
endp, _ = endpoints_proto
self.endpoints = endp.endpoints
if docarray_v2:
from docarray.documents.legacy import LegacyDocument

schemas = json_format.MessageToDict(endp.schemas)
self._pydantic_models_by_endpoint = {}
models_created_by_name = {}
Expand All @@ -236,10 +238,10 @@
else:
if input_model_name not in models_created_by_name:
if input_model_schema == legacy_doc_schema:
input_model = LegacyDocument
input_model = LegacyDocumentJina

Check warning on line 241 in jina/serve/runtimes/gateway/graph/topology_graph.py

View check run for this annotation

Codecov / codecov/patch

jina/serve/runtimes/gateway/graph/topology_graph.py#L241

Added line #L241 was not covered by tests
else:
input_model = (
_create_pydantic_model_from_schema(
create_base_doc_from_schema(
input_model_schema,
input_model_name,
models_created_by_name,
Expand All @@ -266,10 +268,10 @@
else:
if output_model_name not in models_created_by_name:
if output_model_name == legacy_doc_schema:
output_model = LegacyDocument
output_model = LegacyDocumentJina

Check warning on line 271 in jina/serve/runtimes/gateway/graph/topology_graph.py

View check run for this annotation

Codecov / codecov/patch

jina/serve/runtimes/gateway/graph/topology_graph.py#L271

Added line #L271 was not covered by tests
else:
output_model = (
_create_pydantic_model_from_schema(
create_base_doc_from_schema(
output_model_schema,
output_model_name,
models_created_by_name,
Expand Down Expand Up @@ -306,7 +308,7 @@
from pydantic import BaseModel

parameters_model = (
_create_pydantic_model_from_schema(
create_base_doc_from_schema(
parameters_model_schema,
parameters_model_name,
models_created_by_name,
Expand Down
10 changes: 8 additions & 2 deletions jina/serve/runtimes/gateway/http_fastapi_app_docarrayv2.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from jina.logging.logger import JinaLogger
from jina.serve.networking.sse import EventSourceResponse
from jina.types.request.data import DataRequest
from jina._docarray import is_pydantic_v2

Check warning on line 9 in jina/serve/runtimes/gateway/http_fastapi_app_docarrayv2.py

View check run for this annotation

Codecov / codecov/patch

jina/serve/runtimes/gateway/http_fastapi_app_docarrayv2.py#L9

Added line #L9 was not covered by tests

if TYPE_CHECKING: # pragma: no cover
from opentelemetry import trace
Expand Down Expand Up @@ -80,7 +81,9 @@
import os

from pydantic import BaseModel
from pydantic.config import BaseConfig, inherit_config
from pydantic.config import BaseConfig
if not is_pydantic_v2:
from pydantic.config import inherit_config

Check warning on line 86 in jina/serve/runtimes/gateway/http_fastapi_app_docarrayv2.py

View check run for this annotation

Codecov / codecov/patch

jina/serve/runtimes/gateway/http_fastapi_app_docarrayv2.py#L84-L86

Added lines #L84 - L86 were not covered by tests

from jina.proto import jina_pb2
from jina.serve.runtimes.gateway.models import (
Expand Down Expand Up @@ -275,7 +278,10 @@
parameters_model = input_output_map['parameters'] or Optional[Dict]
default_parameters = ... if input_output_map['parameters'] else None

_config = inherit_config(InnerConfig, BaseDoc.__config__)
if not is_pydantic_v2:
_config = inherit_config(InnerConfig, BaseDoc.__config__)

Check warning on line 282 in jina/serve/runtimes/gateway/http_fastapi_app_docarrayv2.py

View check run for this annotation

Codecov / codecov/patch

jina/serve/runtimes/gateway/http_fastapi_app_docarrayv2.py#L281-L282

Added lines #L281 - L282 were not covered by tests
else:
_config = InnerConfig

Check warning on line 284 in jina/serve/runtimes/gateway/http_fastapi_app_docarrayv2.py

View check run for this annotation

Codecov / codecov/patch

jina/serve/runtimes/gateway/http_fastapi_app_docarrayv2.py#L284

Added line #L284 was not covered by tests

endpoint_input_model = pydantic.create_model(
f'{endpoint.strip("/")}_input_model',
Expand Down
22 changes: 13 additions & 9 deletions jina/serve/runtimes/head/request_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,20 @@
from jina.serve.runtimes.monitoring import MonitoringRequestMixin
from jina.serve.runtimes.worker.request_handling import WorkerRequestHandler
from jina.types.request.data import DataRequest, Response
from jina._docarray import docarray_v2
from jina._docarray import docarray_v2, is_pydantic_v2

if docarray_v2:
from jina.serve.runtimes.helper import _create_pydantic_model_from_schema
if not is_pydantic_v2:
from jina.serve.runtimes.helper import _create_pydantic_model_from_schema as create_base_doc_from_schema

Check warning on line 23 in jina/serve/runtimes/head/request_handling.py

View check run for this annotation

Codecov / codecov/patch

jina/serve/runtimes/head/request_handling.py#L22-L23

Added lines #L22 - L23 were not covered by tests
else:
from docarray.utils.create_dynamic_doc_class import create_base_doc_from_schema

Check warning on line 25 in jina/serve/runtimes/head/request_handling.py

View check run for this annotation

Codecov / codecov/patch

jina/serve/runtimes/head/request_handling.py#L25

Added line #L25 was not covered by tests
from docarray import DocList
from docarray.base_doc.any_doc import AnyDoc

from jina._docarray import LegacyDocumentJina

Check warning on line 29 in jina/serve/runtimes/head/request_handling.py

View check run for this annotation

Codecov / codecov/patch

jina/serve/runtimes/head/request_handling.py#L29

Added line #L29 was not covered by tests

legacy_doc_schema = LegacyDocumentJina.schema()

Check warning on line 31 in jina/serve/runtimes/head/request_handling.py

View check run for this annotation

Codecov / codecov/patch

jina/serve/runtimes/head/request_handling.py#L31

Added line #L31 was not covered by tests


if TYPE_CHECKING: # pragma: no cover
from prometheus_client import CollectorRegistry
Expand Down Expand Up @@ -330,9 +337,6 @@
self, connection_pool: GrpcConnectionPool, name: str, retries: int, stop_event
):
from google.protobuf import json_format
from docarray.documents.legacy import LegacyDocument

legacy_doc_schema = LegacyDocument.schema()

async def task():
self.logger.debug(
Expand All @@ -356,20 +360,20 @@

if input_model_schema == legacy_doc_schema:
models_created_by_name[input_model_name] = (
LegacyDocument
LegacyDocumentJina
)
elif input_model_name not in models_created_by_name:
input_model = _create_pydantic_model_from_schema(
input_model = create_base_doc_from_schema(

Check warning on line 366 in jina/serve/runtimes/head/request_handling.py

View check run for this annotation

Codecov / codecov/patch

jina/serve/runtimes/head/request_handling.py#L366

Added line #L366 was not covered by tests
input_model_schema, input_model_name, {}
)
models_created_by_name[input_model_name] = input_model

if output_model_name == legacy_doc_schema:
models_created_by_name[output_model_name] = (
LegacyDocument
LegacyDocumentJina
)
elif output_model_name not in models_created_by_name:
output_model = _create_pydantic_model_from_schema(
output_model = create_base_doc_from_schema(

Check warning on line 376 in jina/serve/runtimes/head/request_handling.py

View check run for this annotation

Codecov / codecov/patch

jina/serve/runtimes/head/request_handling.py#L376

Added line #L376 was not covered by tests
output_model_schema, output_model_name, {}
)
models_created_by_name[output_model_name] = output_model
Expand Down
4 changes: 2 additions & 2 deletions jina/serve/runtimes/helper.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import copy
from typing import Any, Dict, List, Optional, Tuple, Union

from jina._docarray import docarray_v2
from jina._docarray import docarray_v2, is_pydantic_v2

_SPECIFIC_EXECUTOR_SEPARATOR = '__'

Expand Down Expand Up @@ -79,7 +79,7 @@ def _parse_specific_params(parameters: Dict, executor_name: str):
return parsed_params


if docarray_v2:
if docarray_v2 and not is_pydantic_v2:
from docarray import BaseDoc, DocList
from docarray.typing import AnyTensor
from pydantic import create_model
Expand Down
11 changes: 8 additions & 3 deletions jina/serve/runtimes/worker/http_fastapi_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import TYPE_CHECKING, Callable, Dict, List, Optional, Union

from jina import Document, DocumentArray
from jina._docarray import docarray_v2
from jina._docarray import docarray_v2, is_pydantic_v2

Check warning on line 5 in jina/serve/runtimes/worker/http_fastapi_app.py

View check run for this annotation

Codecov / codecov/patch

jina/serve/runtimes/worker/http_fastapi_app.py#L5

Added line #L5 was not covered by tests
from jina.importer import ImportExtensions
from jina.serve.networking.sse import EventSourceResponse
from jina.types.request.data import DataRequest
Expand Down Expand Up @@ -38,7 +38,9 @@
import os

from pydantic import BaseModel, Field
from pydantic.config import BaseConfig, inherit_config
from pydantic.config import BaseConfig
if not is_pydantic_v2:
from pydantic.config import inherit_config

Check warning on line 43 in jina/serve/runtimes/worker/http_fastapi_app.py

View check run for this annotation

Codecov / codecov/patch

jina/serve/runtimes/worker/http_fastapi_app.py#L41-L43

Added lines #L41 - L43 were not covered by tests

from jina.proto import jina_pb2
from jina.serve.runtimes.gateway.models import _to_camel_case
Expand Down Expand Up @@ -168,7 +170,10 @@
)

if docarray_v2:
_config = inherit_config(InnerConfig, BaseDoc.__config__)
if not is_pydantic_v2:
_config = inherit_config(InnerConfig, BaseDoc.__config__)

Check warning on line 174 in jina/serve/runtimes/worker/http_fastapi_app.py

View check run for this annotation

Codecov / codecov/patch

jina/serve/runtimes/worker/http_fastapi_app.py#L173-L174

Added lines #L173 - L174 were not covered by tests
else:
_config = InnerConfig

Check warning on line 176 in jina/serve/runtimes/worker/http_fastapi_app.py

View check run for this annotation

Codecov / codecov/patch

jina/serve/runtimes/worker/http_fastapi_app.py#L176

Added line #L176 was not covered by tests
else:
_config = input_doc_model.__config__

Expand Down
12 changes: 9 additions & 3 deletions jina/serve/runtimes/worker/http_sagemaker_app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import TYPE_CHECKING, Callable, Dict, List, Optional, Union

from jina._docarray import docarray_v2
from jina._docarray import docarray_v2, is_pydantic_v2

Check warning on line 3 in jina/serve/runtimes/worker/http_sagemaker_app.py

View check run for this annotation

Codecov / codecov/patch

jina/serve/runtimes/worker/http_sagemaker_app.py#L3

Added line #L3 was not covered by tests
from jina.importer import ImportExtensions
from jina.types.request.data import DataRequest

Expand Down Expand Up @@ -33,7 +33,9 @@
from fastapi import FastAPI, HTTPException, Request
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel, Field
from pydantic.config import BaseConfig, inherit_config
from pydantic.config import BaseConfig
if not is_pydantic_v2:
from pydantic.config import inherit_config

Check warning on line 38 in jina/serve/runtimes/worker/http_sagemaker_app.py

View check run for this annotation

Codecov / codecov/patch

jina/serve/runtimes/worker/http_sagemaker_app.py#L36-L38

Added lines #L36 - L38 were not covered by tests

import os

Expand Down Expand Up @@ -235,7 +237,11 @@
... if input_output_map['parameters']['model'] else None
)

_config = inherit_config(InnerConfig, BaseDoc.__config__)
if not is_pydantic_v2:
_config = inherit_config(InnerConfig, BaseDoc.__config__)

Check warning on line 241 in jina/serve/runtimes/worker/http_sagemaker_app.py

View check run for this annotation

Codecov / codecov/patch

jina/serve/runtimes/worker/http_sagemaker_app.py#L240-L241

Added lines #L240 - L241 were not covered by tests
else:
_config = InnerConfig

Check warning on line 243 in jina/serve/runtimes/worker/http_sagemaker_app.py

View check run for this annotation

Codecov / codecov/patch

jina/serve/runtimes/worker/http_sagemaker_app.py#L243

Added line #L243 was not covered by tests

endpoint_input_model = pydantic.create_model(
f'{endpoint.strip("/")}_input_model',
data=(Union[List[input_doc_model], input_doc_model], ...),
Expand Down
16 changes: 10 additions & 6 deletions jina/serve/runtimes/worker/request_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

from google.protobuf.struct_pb2 import Struct

from jina._docarray import DocumentArray, docarray_v2
from jina._docarray import DocumentArray, docarray_v2, is_pydantic_v2
from jina.constants import __default_endpoint__
from jina.excepts import BadConfigSource, RuntimeTerminated
from jina.helper import get_full_version
Expand All @@ -33,6 +33,9 @@

if docarray_v2:
from docarray import DocList
from jina._docarray import LegacyDocumentJina
legacy_doc_schema = LegacyDocumentJina.schema()

Check warning on line 37 in jina/serve/runtimes/worker/request_handling.py

View check run for this annotation

Codecov / codecov/patch

jina/serve/runtimes/worker/request_handling.py#L36-L37

Added lines #L36 - L37 were not covered by tests


if TYPE_CHECKING: # pragma: no cover
import grpc
Expand Down Expand Up @@ -1012,23 +1015,24 @@
endpoints_proto.write_endpoints.extend(list(self._executor.write_endpoints))
schemas = self._executor._get_endpoint_models_dict()
if docarray_v2:
from docarray.documents.legacy import LegacyDocument
if not is_pydantic_v2:
from jina.serve.runtimes.helper import _create_aux_model_doc_list_to_list as create_pure_python_type_model

Check warning on line 1019 in jina/serve/runtimes/worker/request_handling.py

View check run for this annotation

Codecov / codecov/patch

jina/serve/runtimes/worker/request_handling.py#L1018-L1019

Added lines #L1018 - L1019 were not covered by tests
else:
from docarray.utils.create_dynamic_doc_class import create_pure_python_type_model

Check warning on line 1021 in jina/serve/runtimes/worker/request_handling.py

View check run for this annotation

Codecov / codecov/patch

jina/serve/runtimes/worker/request_handling.py#L1021

Added line #L1021 was not covered by tests

from jina.serve.runtimes.helper import _create_aux_model_doc_list_to_list

legacy_doc_schema = LegacyDocument.schema()
for endpoint_name, inner_dict in schemas.items():
if inner_dict['input']['model'].schema() == legacy_doc_schema:
inner_dict['input']['model'] = legacy_doc_schema
else:
inner_dict['input']['model'] = _create_aux_model_doc_list_to_list(
inner_dict['input']['model'] = create_pure_python_type_model(

Check warning on line 1028 in jina/serve/runtimes/worker/request_handling.py

View check run for this annotation

Codecov / codecov/patch

jina/serve/runtimes/worker/request_handling.py#L1028

Added line #L1028 was not covered by tests
inner_dict['input']['model']
).schema()

if inner_dict['output']['model'].schema() == legacy_doc_schema:
inner_dict['output']['model'] = legacy_doc_schema
else:
inner_dict['output']['model'] = _create_aux_model_doc_list_to_list(
inner_dict['output']['model'] = create_pure_python_type_model(

Check warning on line 1035 in jina/serve/runtimes/worker/request_handling.py

View check run for this annotation

Codecov / codecov/patch

jina/serve/runtimes/worker/request_handling.py#L1035

Added line #L1035 was not covered by tests
inner_dict['output']['model']
).schema()

Expand Down
Loading
Loading