Skip to content

Commit

Permalink
[typings] Replace deprecated typing with built-in types (#1794)
Browse files Browse the repository at this point in the history
  • Loading branch information
felixdittrich92 authored Nov 25, 2024
1 parent aca6b36 commit 72bed41
Show file tree
Hide file tree
Showing 126 changed files with 856 additions and 880 deletions.
4 changes: 2 additions & 2 deletions .github/verify_pr_labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
with no labeling responsibility, so we don't want to bother them.
"""

from typing import Any, Set, Tuple
from typing import Any

import requests

Expand Down Expand Up @@ -54,7 +54,7 @@ def query_repo(cmd: str, *, accept) -> Any:
return response.json()


def get_pr_merger_and_labels(pr_number: int) -> Tuple[str, Set[str]]:
def get_pr_merger_and_labels(pr_number: int) -> tuple[str, set[str]]:
# See https://docs.github.com/en/rest/reference/pulls#get-a-pull-request
data = query_repo(f"pulls/{pr_number}", accept="application/vnd.github.v3+json")
merger = data.get("merged_by", {}).get("login")
Expand Down
5 changes: 2 additions & 3 deletions api/app/routes/detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
# This program is licensed under the Apache License 2.0.
# See LICENSE or go to <https://opensource.org/licenses/Apache-2.0> for full license details.

from typing import List

from fastapi import APIRouter, Depends, File, HTTPException, UploadFile, status

Expand All @@ -15,8 +14,8 @@
router = APIRouter()


@router.post("/", response_model=List[DetectionOut], status_code=status.HTTP_200_OK, summary="Perform text detection")
async def text_detection(request: DetectionIn = Depends(), files: List[UploadFile] = [File(...)]):
@router.post("/", response_model=list[DetectionOut], status_code=status.HTTP_200_OK, summary="Perform text detection")
async def text_detection(request: DetectionIn = Depends(), files: list[UploadFile] = [File(...)]):
"""Runs docTR text detection model to analyze the input image"""
try:
predictor = init_predictor(request)
Expand Down
5 changes: 2 additions & 3 deletions api/app/routes/kie.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
# This program is licensed under the Apache License 2.0.
# See LICENSE or go to <https://opensource.org/licenses/Apache-2.0> for full license details.

from typing import List

from fastapi import APIRouter, Depends, File, HTTPException, UploadFile, status

Expand All @@ -14,8 +13,8 @@
router = APIRouter()


@router.post("/", response_model=List[KIEOut], status_code=status.HTTP_200_OK, summary="Perform KIE")
async def perform_kie(request: KIEIn = Depends(), files: List[UploadFile] = [File(...)]):
@router.post("/", response_model=list[KIEOut], status_code=status.HTTP_200_OK, summary="Perform KIE")
async def perform_kie(request: KIEIn = Depends(), files: list[UploadFile] = [File(...)]):
"""Runs docTR KIE model to analyze the input image"""
try:
predictor = init_predictor(request)
Expand Down
5 changes: 2 additions & 3 deletions api/app/routes/ocr.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
# This program is licensed under the Apache License 2.0.
# See LICENSE or go to <https://opensource.org/licenses/Apache-2.0> for full license details.

from typing import List

from fastapi import APIRouter, Depends, File, HTTPException, UploadFile, status

Expand All @@ -14,8 +13,8 @@
router = APIRouter()


@router.post("/", response_model=List[OCROut], status_code=status.HTTP_200_OK, summary="Perform OCR")
async def perform_ocr(request: OCRIn = Depends(), files: List[UploadFile] = [File(...)]):
@router.post("/", response_model=list[OCROut], status_code=status.HTTP_200_OK, summary="Perform OCR")
async def perform_ocr(request: OCRIn = Depends(), files: list[UploadFile] = [File(...)]):
"""Runs docTR OCR model to analyze the input image"""
try:
# generator object to list
Expand Down
5 changes: 2 additions & 3 deletions api/app/routes/recognition.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
# This program is licensed under the Apache License 2.0.
# See LICENSE or go to <https://opensource.org/licenses/Apache-2.0> for full license details.

from typing import List

from fastapi import APIRouter, Depends, File, HTTPException, UploadFile, status

Expand All @@ -15,9 +14,9 @@


@router.post(
"/", response_model=List[RecognitionOut], status_code=status.HTTP_200_OK, summary="Perform text recognition"
"/", response_model=list[RecognitionOut], status_code=status.HTTP_200_OK, summary="Perform text recognition"
)
async def text_recognition(request: RecognitionIn = Depends(), files: List[UploadFile] = [File(...)]):
async def text_recognition(request: RecognitionIn = Depends(), files: list[UploadFile] = [File(...)]):
"""Runs docTR text recognition model to analyze the input image"""
try:
predictor = init_predictor(request)
Expand Down
36 changes: 18 additions & 18 deletions api/app/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# This program is licensed under the Apache License 2.0.
# See LICENSE or go to <https://opensource.org/licenses/Apache-2.0> for full license details.

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

from pydantic import BaseModel, Field

Expand Down Expand Up @@ -54,21 +54,21 @@ class RecognitionOut(BaseModel):

class DetectionOut(BaseModel):
name: str = Field(..., examples=["example.jpg"])
geometries: List[List[float]] = Field(..., examples=[[0.0, 0.0, 0.0, 0.0]])
geometries: list[list[float]] = Field(..., examples=[[0.0, 0.0, 0.0, 0.0]])


class OCRWord(BaseModel):
value: str = Field(..., examples=["example"])
geometry: List[float] = Field(..., examples=[[0.0, 0.0, 0.0, 0.0]])
geometry: list[float] = Field(..., examples=[[0.0, 0.0, 0.0, 0.0]])
objectness_score: float = Field(..., examples=[0.99])
confidence: float = Field(..., examples=[0.99])
crop_orientation: Dict[str, Any] = Field(..., examples=[{"value": 0, "confidence": None}])
crop_orientation: dict[str, Any] = Field(..., examples=[{"value": 0, "confidence": None}])


class OCRLine(BaseModel):
geometry: List[float] = Field(..., examples=[[0.0, 0.0, 0.0, 0.0]])
geometry: list[float] = Field(..., examples=[[0.0, 0.0, 0.0, 0.0]])
objectness_score: float = Field(..., examples=[0.99])
words: List[OCRWord] = Field(
words: list[OCRWord] = Field(
...,
examples=[
{
Expand All @@ -83,9 +83,9 @@ class OCRLine(BaseModel):


class OCRBlock(BaseModel):
geometry: List[float] = Field(..., examples=[[0.0, 0.0, 0.0, 0.0]])
geometry: list[float] = Field(..., examples=[[0.0, 0.0, 0.0, 0.0]])
objectness_score: float = Field(..., examples=[0.99])
lines: List[OCRLine] = Field(
lines: list[OCRLine] = Field(
...,
examples=[
{
Expand All @@ -105,7 +105,7 @@ class OCRBlock(BaseModel):


class OCRPage(BaseModel):
blocks: List[OCRBlock] = Field(
blocks: list[OCRBlock] = Field(
...,
examples=[
{
Expand Down Expand Up @@ -133,10 +133,10 @@ class OCRPage(BaseModel):

class OCROut(BaseModel):
name: str = Field(..., examples=["example.jpg"])
orientation: Dict[str, Union[float, None]] = Field(..., examples=[{"value": 0.0, "confidence": 0.99}])
language: Dict[str, Union[str, float, None]] = Field(..., examples=[{"value": "en", "confidence": 0.99}])
dimensions: Tuple[int, int] = Field(..., examples=[(100, 100)])
items: List[OCRPage] = Field(
orientation: dict[str, float | None] = Field(..., examples=[{"value": 0.0, "confidence": 0.99}])
language: dict[str, str | float | None] = Field(..., examples=[{"value": "en", "confidence": 0.99}])
dimensions: tuple[int, int] = Field(..., examples=[(100, 100)])
items: list[OCRPage] = Field(
...,
examples=[
{
Expand Down Expand Up @@ -164,7 +164,7 @@ class OCROut(BaseModel):

class KIEElement(BaseModel):
class_name: str = Field(..., examples=["example"])
items: List[Dict[str, Union[str, List[float], float, Dict[str, Any]]]] = Field(
items: list[dict[str, str | list[float] | float | dict[str, Any]]] = Field(
...,
examples=[
{
Expand All @@ -180,7 +180,7 @@ class KIEElement(BaseModel):

class KIEOut(BaseModel):
name: str = Field(..., examples=["example.jpg"])
orientation: Dict[str, Union[float, None]] = Field(..., examples=[{"value": 0.0, "confidence": 0.99}])
language: Dict[str, Union[str, float, None]] = Field(..., examples=[{"value": "en", "confidence": 0.99}])
dimensions: Tuple[int, int] = Field(..., examples=[(100, 100)])
predictions: List[KIEElement]
orientation: dict[str, float | None] = Field(..., examples=[{"value": 0.0, "confidence": 0.99}])
language: dict[str, str | float | None] = Field(..., examples=[{"value": "en", "confidence": 0.99}])
dimensions: tuple[int, int] = Field(..., examples=[(100, 100)])
predictions: list[KIEElement]
8 changes: 4 additions & 4 deletions api/app/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# See LICENSE or go to <https://opensource.org/licenses/Apache-2.0> for full license details.


from typing import Any, List, Tuple, Union
from typing import Any

import numpy as np
from fastapi import UploadFile
Expand All @@ -14,20 +14,20 @@

def resolve_geometry(
geom: Any,
) -> Union[Tuple[float, float, float, float], Tuple[float, float, float, float, float, float, float, float]]:
) -> tuple[float, float, float, float] | tuple[float, float, float, float, float, float, float, float]:
if len(geom) == 4:
return (*geom[0], *geom[1], *geom[2], *geom[3])
return (*geom[0], *geom[1])


async def get_documents(files: List[UploadFile]) -> Tuple[List[np.ndarray], List[str]]: # pragma: no cover
async def get_documents(files: list[UploadFile]) -> tuple[list[np.ndarray], list[str]]: # pragma: no cover
"""Convert a list of UploadFile objects to lists of numpy arrays and their corresponding filenames
Args:
files: list of UploadFile objects
Returns:
Tuple[List[np.ndarray], List[str]]: list of numpy arrays and their corresponding filenames
tuple[list[np.ndarray], list[str]]: list of numpy arrays and their corresponding filenames
"""
filenames = []
Expand Down
4 changes: 2 additions & 2 deletions api/app/vision.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# See LICENSE or go to <https://opensource.org/licenses/Apache-2.0> for full license details.


from typing import Callable, Union
from collections.abc import Callable

import torch

Expand All @@ -25,7 +25,7 @@ def _move_to_device(predictor: Callable) -> Callable:
return predictor.to(torch.device("cuda" if torch.cuda.is_available() else "cpu"))


def init_predictor(request: Union[KIEIn, OCRIn, RecognitionIn, DetectionIn]) -> Callable:
def init_predictor(request: KIEIn | OCRIn | RecognitionIn | DetectionIn) -> Callable:
"""Initialize the predictor based on the request
Args:
Expand Down
12 changes: 6 additions & 6 deletions doctr/contrib/artefacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# This program is licensed under the Apache License 2.0.
# See LICENSE or go to <https://opensource.org/licenses/Apache-2.0> for full license details.

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

import cv2
import numpy as np
Expand All @@ -14,7 +14,7 @@

__all__ = ["ArtefactDetector"]

default_cfgs: Dict[str, Dict[str, Any]] = {
default_cfgs: dict[str, dict[str, Any]] = {
"yolov8_artefact": {
"input_shape": (3, 1024, 1024),
"labels": ["bar_code", "qr_code", "logo", "photo"],
Expand Down Expand Up @@ -49,9 +49,9 @@ def __init__(
self,
arch: str = "yolov8_artefact",
batch_size: int = 2,
model_path: Optional[str] = None,
labels: Optional[List[str]] = None,
input_shape: Optional[Tuple[int, int, int]] = None,
model_path: str | None = None,
labels: list[str] | None = None,
input_shape: tuple[int, int, int] | None = None,
conf_threshold: float = 0.5,
iou_threshold: float = 0.5,
**kwargs: Any,
Expand All @@ -65,7 +65,7 @@ def __init__(
def preprocess(self, img: np.ndarray) -> np.ndarray:
return np.transpose(cv2.resize(img, (self.input_shape[2], self.input_shape[1])), (2, 0, 1)) / np.array(255.0)

def postprocess(self, output: List[np.ndarray], input_images: List[List[np.ndarray]]) -> List[List[Dict[str, Any]]]:
def postprocess(self, output: list[np.ndarray], input_images: list[list[np.ndarray]]) -> list[list[dict[str, Any]]]:
results = []

for batch in zip(output, input_images):
Expand Down
14 changes: 7 additions & 7 deletions doctr/contrib/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# This program is licensed under the Apache License 2.0.
# See LICENSE or go to <https://opensource.org/licenses/Apache-2.0> for full license details.

from typing import Any, List, Optional
from typing import Any

import numpy as np

Expand All @@ -22,14 +22,14 @@ class _BasePredictor:
**kwargs: additional arguments to be passed to `download_from_url`
"""

def __init__(self, batch_size: int, url: Optional[str] = None, model_path: Optional[str] = None, **kwargs) -> None:
def __init__(self, batch_size: int, url: str | None = None, model_path: str | None = None, **kwargs) -> None:
self.batch_size = batch_size
self.session = self._init_model(url, model_path, **kwargs)

self._inputs: List[np.ndarray] = []
self._results: List[Any] = []
self._inputs: list[np.ndarray] = []
self._results: list[Any] = []

def _init_model(self, url: Optional[str] = None, model_path: Optional[str] = None, **kwargs: Any) -> Any:
def _init_model(self, url: str | None = None, model_path: str | None = None, **kwargs: Any) -> Any:
"""
Download the model from the given url if needed
Expand Down Expand Up @@ -61,7 +61,7 @@ def preprocess(self, img: np.ndarray) -> np.ndarray:
"""
raise NotImplementedError

def postprocess(self, output: List[np.ndarray], input_images: List[List[np.ndarray]]) -> Any:
def postprocess(self, output: list[np.ndarray], input_images: list[list[np.ndarray]]) -> Any:
"""
Postprocess the model output
Expand All @@ -74,7 +74,7 @@ def postprocess(self, output: List[np.ndarray], input_images: List[List[np.ndarr
"""
raise NotImplementedError

def __call__(self, inputs: List[np.ndarray]) -> Any:
def __call__(self, inputs: list[np.ndarray]) -> Any:
"""
Call the model on the given inputs
Expand Down
8 changes: 4 additions & 4 deletions doctr/datasets/cord.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import json
import os
from pathlib import Path
from typing import Any, Dict, List, Tuple, Union
from typing import Any

import numpy as np
from tqdm import tqdm
Expand Down Expand Up @@ -71,9 +71,9 @@ def __init__(
+ "To get the whole dataset with boxes and labels leave both parameters to False."
)

# List images
# list images
tmp_root = os.path.join(self.root, "image")
self.data: List[Tuple[Union[str, np.ndarray], Union[str, Dict[str, Any], np.ndarray]]] = []
self.data: list[tuple[str | np.ndarray, str | dict[str, Any] | np.ndarray]] = []
self.train = train
np_dtype = np.float32
for img_path in tqdm(iterable=os.listdir(tmp_root), desc="Unpacking CORD", total=len(os.listdir(tmp_root))):
Expand All @@ -90,7 +90,7 @@ def __init__(
if len(word["text"]) > 0:
x = word["quad"]["x1"], word["quad"]["x2"], word["quad"]["x3"], word["quad"]["x4"]
y = word["quad"]["y1"], word["quad"]["y2"], word["quad"]["y3"], word["quad"]["y4"]
box: Union[List[float], np.ndarray]
box: list[float] | np.ndarray
if use_polygons:
# (x, y) coordinates of top left, top right, bottom right, bottom left corners
box = np.array(
Expand Down
Loading

0 comments on commit 72bed41

Please sign in to comment.