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
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
repos:
- repo: https://github.com/ambv/black
rev: 24.4.0
rev: 25.9.0
hooks:
- id: black

- repo: https://github.com/pycqa/isort
rev: 5.13.2
rev: 7.0.0
hooks:
- id: isort
args: [ "--profile", "black" , "--split-on-trailing-comma", "true"]
Expand Down Expand Up @@ -39,7 +39,7 @@ repos:
]

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.15.0
rev: v1.18.2
hooks:
- id: mypy
args: []
Expand Down
8 changes: 8 additions & 0 deletions docs/product/ind/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
India
-----

.. toctree::
:maxdepth: 1
:glob:

./*
16 changes: 14 additions & 2 deletions mindee/parsing/v2/field/dynamic_field.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
from enum import Enum
from importlib import import_module
from typing import TYPE_CHECKING, Union

from mindee.error import MindeeApiV2Error
from mindee.parsing.common.string_dict import StringDict

if TYPE_CHECKING:
from mindee.parsing.v2.field.list_field import ListField
from mindee.parsing.v2.field.object_field import ObjectField
from mindee.parsing.v2.field.simple_field import SimpleField


class FieldType(str, Enum):
"""Field types."""
Expand All @@ -13,6 +19,9 @@ class FieldType(str, Enum):
SIMPLE = "SimpleField"


FieldTypeAlias = Union["SimpleField", "ListField", "ObjectField"]


class DynamicField:
"""Field that can be displayed in rst format."""

Expand All @@ -30,7 +39,10 @@ def multi_str(self) -> str:
return str(self)


def get_field_type(raw_response: StringDict, indent_level: int = 0) -> DynamicField:
def get_field_type(
raw_response: StringDict,
indent_level: int = 0,
) -> FieldTypeAlias:
"""Get appropriate field types."""
if isinstance(raw_response, dict):
if "value" in raw_response:
Expand All @@ -43,7 +55,7 @@ def get_field_type(raw_response: StringDict, indent_level: int = 0) -> DynamicFi
field_file = import_module("mindee.parsing.v2.field.object_field")
field_class = getattr(field_file, FieldType.OBJECT.value)
else:
raise MindeeApiV2Error(f"Unrecognized field format in {raw_response}.")
raise MindeeApiV2Error(f"Unrecognized field type in {raw_response}.")
return field_class(raw_response, indent_level)

raise MindeeApiV2Error(f"Unrecognized field format {raw_response}.")
4 changes: 2 additions & 2 deletions mindee/parsing/v2/field/inference_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

from mindee.parsing.common.string_dict import StringDict
from mindee.parsing.v2.field.dynamic_field import (
DynamicField,
FieldType,
FieldTypeAlias,
get_field_type,
)


class InferenceFields(Dict[str, DynamicField]):
class InferenceFields(Dict[str, FieldTypeAlias]):
"""Inference fields dict."""

def __init__(self, raw_response: StringDict, indent_level: int = 0) -> None:
Expand Down
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ test = [
"pytest-cov~=5.0",
]
docs = [
"sphinx~=5.3",
"sphinx_rtd_theme~=1.1",
"sphinx-autodoc-typehints~=1.20",
"sphinx~=7.3",
"sphinx_rtd_theme~=2.0",
"sphinx-autodoc-typehints~=2.2",
]
build = [
"build",
Expand Down
Empty file added tests/input/__init__.py
Empty file.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import operator
import os
from functools import reduce
from pathlib import Path

import pytest
from PIL import Image
Expand Down
File renamed without changes.
File renamed without changes.
1 change: 0 additions & 1 deletion tests/v1/api/test_async_response.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import json
from pathlib import Path

import pytest
import requests
Expand Down
47 changes: 17 additions & 30 deletions tests/v1/input/test_local_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,36 @@


@pytest.fixture
def dummy_secret_key():
return "ogNjY44MhvKPGTtVsI8zG82JqWQa68woYQH"
def file_path() -> Path:
return ASYNC_DIR / "get_completed_empty.json"


@pytest.fixture
def signature():
return "5ed1673e34421217a5dbfcad905ee62261a3dd66c442f3edd19302072bbf70d0"


@pytest.fixture
def file_path():
return Path(ASYNC_DIR / "get_completed_empty.json")
def _assert_local_response(local_response):
fake_hmac_signing = "ogNjY44MhvKPGTtVsI8zG82JqWQa68woYQH"
signature = "5ed1673e34421217a5dbfcad905ee62261a3dd66c442f3edd19302072bbf70d0"
assert local_response._file is not None
assert not local_response.is_valid_hmac_signature(
fake_hmac_signing, "invalid signature"
)
assert signature == local_response.get_hmac_signature(fake_hmac_signing)
assert local_response.is_valid_hmac_signature(fake_hmac_signing, signature)


def test_valid_file_local_response(dummy_secret_key, signature, file_path):
def test_valid_file_local_response(file_path):
with open(file_path, "rb") as file:
local_response = LocalResponse(file)
assert local_response._file is not None
assert not local_response.is_valid_hmac_signature(
dummy_secret_key, "invalid signature"
)
assert signature == local_response.get_hmac_signature(dummy_secret_key)
assert local_response.is_valid_hmac_signature(dummy_secret_key, signature)
_assert_local_response(local_response)


def test_valid_path_local_response(dummy_secret_key, signature, file_path):
def test_valid_path_local_response(file_path):
local_response = LocalResponse(file_path)
assert local_response._file is not None
assert not local_response.is_valid_hmac_signature(
dummy_secret_key, "invalid signature"
)
assert signature == local_response.get_hmac_signature(dummy_secret_key)
assert local_response.is_valid_hmac_signature(dummy_secret_key, signature)
_assert_local_response(local_response)


def test_valid_bytes_local_response(dummy_secret_key, signature, file_path):
def test_valid_bytes_local_response(file_path):
with open(file_path, "r") as f:
str_response = f.read().replace("\r", "").replace("\n", "")
file_bytes = str_response.encode("utf-8")
local_response = LocalResponse(file_bytes)
assert local_response._file is not None
assert not local_response.is_valid_hmac_signature(
dummy_secret_key, "invalid signature"
)
assert signature == local_response.get_hmac_signature(dummy_secret_key)
assert local_response.is_valid_hmac_signature(dummy_secret_key, signature)
_assert_local_response(local_response)
3 changes: 1 addition & 2 deletions tests/v1/mindee_http/test_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
handle_error,
)
from mindee.input.sources.path_input import PathInput
from tests.utils import V1_DATA_DIR, V1_PRODUCT_DATA_DIR, clear_envvars, dummy_envvars
from tests.v1.input.test_inputs import FILE_TYPES_DIR
from tests.utils import FILE_TYPES_DIR, V1_DATA_DIR, clear_envvars, dummy_envvars

V1_ERROR_DATA_DIR = V1_DATA_DIR / "errors"

Expand Down
49 changes: 49 additions & 0 deletions tests/v2/input/test_local_response.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from pathlib import Path

import pytest

from mindee import InferenceResponse
from mindee.input import LocalResponse
from tests.utils import V2_DATA_DIR


@pytest.fixture
def file_path() -> Path:
return V2_DATA_DIR / "inference" / "standard_field_types.json"


def _assert_local_response(local_response):
fake_hmac_signing = "ogNjY44MhvKPGTtVsI8zG82JqWQa68woYQH"
signature = "a1bc9012fa63539d602f163d8980604a0cf2b2ae88e56009cfa1db33382736cf"

assert local_response._file is not None
assert not local_response.is_valid_hmac_signature(
fake_hmac_signing, "invalid signature"
)
assert signature == local_response.get_hmac_signature(fake_hmac_signing)
assert local_response.is_valid_hmac_signature(fake_hmac_signing, signature)
reponse: InferenceResponse = local_response.deserialize_response(InferenceResponse)
assert isinstance(reponse, InferenceResponse)
assert reponse.inference is not None
assert reponse.inference.result is not None
assert reponse.inference.result.fields is not None


def test_valid_file_local_response(file_path):
with open(file_path, "rb") as file:
local_response = LocalResponse(file)
_assert_local_response(local_response)


def test_valid_path_local_response(file_path):
local_response = LocalResponse(file_path)
assert local_response._file is not None
_assert_local_response(local_response)


def test_valid_bytes_local_response(file_path):
with open(file_path, "r") as f:
str_response = f.read().replace("\r", "").replace("\n", "")
file_bytes = str_response.encode("utf-8")
local_response = LocalResponse(file_bytes)
_assert_local_response(local_response)
Empty file added tests/v2/parsing/__init__.py
Empty file.