Skip to content
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
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@ name: CI
on: [push, pull_request]

jobs:
lint-flake-8:
lint-ruff:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2.5.0
- name: Set up Python 3.8
uses: actions/setup-python@v4
with:
python-version: 3.8
- name: Lint with flake8
- name: Lint with ruff
run: |
python -m pip install --upgrade pip
python -m pip install poetry
poetry install

# stop the build if there are Python syntax errors or undefined names
poetry run flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics --exclude .git,__pycache__,docs/source/conf.py,old,build,dist,tests/,docarray/proto/pb2/docarray_pb2.py
poetry run ruff docarray
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
poetry run flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics --exclude .git,__pycache__,docs/source/conf.py,old,build,dist,tests/,docarray/proto/pb2/docarray_pb2.py
poetry run ruff docarray

check-black:
runs-on: ubuntu-latest
Expand Down
14 changes: 5 additions & 9 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
repos:
- repo: https://github.com/pycqa/flake8
rev: 4.0.1
hooks:
- id: flake8
exclude: ^(.git|__pycache__|docs/source/conf.py|old|build|dist|tests|jina/resources/|docarray/proto/pb/docarray_pb2.py|docarray/proto/pb/docarray_pb2_grpc.py|jina/proto/pb2/docarray_pb2.py|jina/proto/pb2/docarray_pb2_grpc.py)
args:
- --max-complexity=10
- --max-line-length=127
- --select=E9,F63,F7,F82
- repo: https://github.com/ambv/black
rev: 22.3.0
hooks:
Expand All @@ -28,3 +19,8 @@ repos:
- id: isort
args: ["--profile", "black"]
exclude: ^(docarray/proto/pb/docarray_pb2.py|docarray/proto/pb/docarray_pb2.py|docs/|docarray/resources/)

- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.116
hooks:
- id: ruff
2 changes: 2 additions & 0 deletions docarray/array/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
from docarray.array.documentarray import DocumentArray

__all__ = ['DocumentArray']
2 changes: 2 additions & 0 deletions docarray/array/mixins/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
from docarray.array.mixins.proto import ProtoArrayMixin

__all__ = ['ProtoArrayMixin']
6 changes: 4 additions & 2 deletions docarray/array/mixins/proto.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ def from_protobuf(
def to_protobuf(self) -> 'DocumentArrayProto':
"""Convert DocumentArray into a Protobuf message.

:param ndarray_type: can be ``list`` or ``numpy``, if set it will force all ndarray-like object from all
:param ndarray_type: can be ``list`` or ``numpy``,
if set it will force all ndarray-like object from all
Documents to ``List`` or ``numpy.ndarray``.
:return: the protobuf message
"""
Expand All @@ -28,7 +29,8 @@ def to_protobuf(self) -> 'DocumentArrayProto':
return dap

def _to_nested_item_protobuf(self) -> 'NodeProto':
"""Convert a DocumentArray into a nested item protobuf message. This function should be called when a DocumentArray
"""Convert a DocumentArray into a nested item protobuf message.
This function should be called when a DocumentArray
is nested into another Document that need to be converted into a protobuf

:return: the nested item protobuf message
Expand Down
2 changes: 2 additions & 0 deletions docarray/document/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
from docarray.document.any_document import AnyDocument
from docarray.document.document import BaseDocument

__all__ = ['AnyDocument', 'BaseDocument']
3 changes: 2 additions & 1 deletion docarray/document/any_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ def __init__(self, **kwargs):
@classmethod
def _get_nested_document_class(cls, field: str) -> Type['BaseDocument']:
"""
Accessing the nested python Class define in the schema. Could be useful for reconstruction of Document in
Accessing the nested python Class define in the schema.
Could be useful for reconstruction of Document in
serialization/deserilization
:param field: name of the field
:return:
Expand Down
9 changes: 5 additions & 4 deletions docarray/document/base_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@

class BaseNode(ABC):
"""
A DocumentNode is an object than can be nested inside a Document. A Document itself is a DocumentNode as well
as prebuilt type
A DocumentNode is an object than can be nested inside a Document.
A Document itself is a DocumentNode as well as prebuilt type
"""

@abstractmethod
def _to_nested_item_protobuf(self) -> 'NodeProto':
"""Convert itself into a nested item protobuf message. This function should be called when the self
is nested into another Document that need to be converted into a protobuf
"""Convert itself into a nested item protobuf message. This function should
be called when the self is nested into another Document that need to be
converted into a protobuf

:return: the nested item protobuf message
"""
Expand Down
2 changes: 2 additions & 0 deletions docarray/document/mixins/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
from docarray.document.mixins.proto import ProtoMixin

__all__ = ['ProtoMixin']
19 changes: 11 additions & 8 deletions docarray/document/mixins/proto.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import TYPE_CHECKING, Any, Dict, Type
from typing import Any, Dict, Type

from docarray.proto import DocumentProto, NdArrayProto, NodeProto
from docarray.proto import DocumentProto, NodeProto
from docarray.typing import Tensor

from ..abstract_document import AbstractDocument
Expand All @@ -11,8 +11,8 @@ class ProtoMixin(AbstractDocument, BaseNode):
@classmethod
def _get_nested_document_class(cls, field: str) -> Type['ProtoMixin']:
"""
Accessing the nested python Class define in the schema. Could be useful for reconstruction of Document in
serialization/deserilization
Accessing the nested python Class define in the schema. Could be useful for
reconstruction of Document in serialization/deserilization
:param field: name of the field
:return:
"""
Expand Down Expand Up @@ -78,8 +78,10 @@ def to_protobuf(self) -> 'DocumentProto':
except RecursionError as ex:
if len(ex.args) >= 1:
ex.args = (
f'Field `{field}` contains cyclic reference in memory. '
f'Could it be your Document is referring to itself?',
(
f'Field `{field}` contains cyclic reference in memory. '
'Could it be your Document is referring to itself?'
),
)
raise
except Exception as ex:
Expand All @@ -90,8 +92,9 @@ def to_protobuf(self) -> 'DocumentProto':
return DocumentProto(data=data)

def _to_nested_item_protobuf(self) -> 'NodeProto':
"""Convert Document into a nested item protobuf message. This function should be called when the Document
is nest into another Document that need to be converted into a protobuf
"""Convert Document into a nested item protobuf message. This function should be
called when the Document is nest into another Document that need to be
converted into a protobuf

:return: the nested item protobuf message
"""
Expand Down
9 changes: 8 additions & 1 deletion docarray/proto/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
from docarray.proto.pb2.docarray_pb2 import DocumentArrayProto, DocumentProto, NdArrayProto, NodeProto
from docarray.proto.pb2.docarray_pb2 import (
DocumentArrayProto,
DocumentProto,
NdArrayProto,
NodeProto,
)

__all__ = ['DocumentArrayProto', 'DocumentProto', 'NdArrayProto', 'NodeProto']
10 changes: 3 additions & 7 deletions docarray/typing/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
from typing import TypeVar

from docarray.document.base_node import BaseNode

from .ndarray import Embedding, Tensor
from .url import ImageUrl

T = TypeVar('T')
from docarray.typing.ndarray import Embedding, Tensor
from docarray.typing.url import ImageUrl

__all__ = ['Tensor', 'Embedding', 'BaseNode']
__all__ = ['Tensor', 'Embedding', 'BaseNode', 'ImageUrl']
1 change: 0 additions & 1 deletion docarray/typing/ndarray.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import numpy as np
from .tensor import Tensor

Embedding = Tensor
4 changes: 3 additions & 1 deletion docarray/typing/tensor/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
from .tensor import Tensor
from docarray.typing.tensor.tensor import Tensor

__all__ = ['Tensor']
16 changes: 10 additions & 6 deletions docarray/typing/tensor/tensor.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from typing import Union, TypeVar, Any, TYPE_CHECKING, Type, cast

import numpy as np

if TYPE_CHECKING:
from pydantic.fields import ModelField
from pydantic import BaseConfig, PydanticValueError
from pydantic import BaseConfig

from docarray.document.base_node import BaseNode
from docarray.proto import DocumentProto, NdArrayProto, NodeProto
from docarray.proto import NdArrayProto, NodeProto

T = TypeVar('T', bound='Tensor')

Expand All @@ -20,7 +21,9 @@ def __get_validators__(cls):
yield cls.validate

@classmethod
def validate(cls: Type[T], value: Union[T, Any], field: 'ModelField', config: 'BaseConfig') -> T:
def validate(
cls: Type[T], value: Union[T, Any], field: 'ModelField', config: 'BaseConfig'
) -> T:
if isinstance(value, np.ndarray):
return cls.from_ndarray(value)
elif isinstance(value, Tensor):
Expand All @@ -38,8 +41,9 @@ def from_ndarray(cls: Type[T], value: np.ndarray) -> T:
return value.view(cls)

def _to_nested_item_protobuf(self: T) -> 'NodeProto':
"""Convert Document into a nested item protobuf message. This function should be called when the Document
is nested into another Document that need to be converted into a protobuf
"""Convert Document into a nested item protobuf message. This function should
be called when the Document is nested into another Document that need to be
converted into a protobuf

:return: the nested item protobuf message
"""
Expand Down Expand Up @@ -69,4 +73,4 @@ def flush_ndarray(pb_msg: 'NdArrayProto', value: 'Tensor'):
pb_msg.dense.buffer = value.tobytes()
pb_msg.dense.ClearField('shape')
pb_msg.dense.shape.extend(list(value.shape))
pb_msg.dense.dtype = value.dtype.str
pb_msg.dense.dtype = value.dtype.str
2 changes: 2 additions & 0 deletions docarray/typing/url/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
from .image_url import ImageUrl

__all__ = ['ImageUrl']
5 changes: 3 additions & 2 deletions docarray/typing/url/any_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

class AnyUrl(BaseAnyUrl, BaseNode):
def _to_nested_item_protobuf(self) -> 'NodeProto':
"""Convert Document into a nested item protobuf message. This function should be called when the Document
is nested into another Document that need to be converted into a protobuf
"""Convert Document into a nested item protobuf message. This function should
be called when the Document is nested into another Document that need to
be converted into a protobuf

:return: the nested item protobuf message
"""
Expand Down
52 changes: 10 additions & 42 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading