Skip to content

Commit

Permalink
SDK: remove imports that were deprecated in Python 3.9 (#8626)
Browse files Browse the repository at this point in the history
Since we just dropped support for Python 3.8, we don't need to keep
using the old imports anymore.

This patch was produced by `ruff check --select=UP006,UP035
--target-version=py39 --fix --unsafe-fixes` (with a couple of manual
fixes where ruff coudn't do it by itself).

I'd like to apply this across the entire codebase, but since it produces
a large amount of churn, I'd rather do it piecemeal.
In particular, the SDK contains generated code with `typing` imports,
but since I can't fix that automatically, I'm going to delay it until
another PR.
  • Loading branch information
SpecLad authored Nov 1, 2024
1 parent f0a578a commit 5dc5265
Show file tree
Hide file tree
Showing 23 changed files with 102 additions and 109 deletions.
5 changes: 3 additions & 2 deletions cvat-sdk/cvat_sdk/auto_annotation/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
# SPDX-License-Identifier: MIT

import logging
from typing import List, Mapping, Optional, Sequence
from collections.abc import Mapping, Sequence
from typing import Optional

import attrs

Expand Down Expand Up @@ -119,7 +120,7 @@ def __init__(
fun_label, ds_labels_by_name
)

def validate_and_remap(self, shapes: List[models.LabeledShapeRequest], ds_frame: int) -> None:
def validate_and_remap(self, shapes: list[models.LabeledShapeRequest], ds_frame: int) -> None:
new_shapes = []

for shape in shapes:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
# SPDX-License-Identifier: MIT

from functools import cached_property
from typing import List

import PIL.Image
import torchvision.models
Expand All @@ -28,7 +27,7 @@ def spec(self) -> cvataa.DetectionFunctionSpec:
]
)

def detect(self, context, image: PIL.Image.Image) -> List[models.LabeledShapeRequest]:
def detect(self, context, image: PIL.Image.Image) -> list[models.LabeledShapeRequest]:
results = self._model([self._transforms(image)])

return [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
# SPDX-License-Identifier: MIT

from functools import cached_property
from typing import List

import PIL.Image
import torchvision.models
Expand Down Expand Up @@ -36,7 +35,7 @@ def spec(self) -> cvataa.DetectionFunctionSpec:
]
)

def detect(self, context, image: PIL.Image.Image) -> List[models.LabeledShapeRequest]:
def detect(self, context, image: PIL.Image.Image) -> list[models.LabeledShapeRequest]:
results = self._model([self._transforms(image)])

return [
Expand Down
5 changes: 3 additions & 2 deletions cvat-sdk/cvat_sdk/auto_annotation/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
# SPDX-License-Identifier: MIT

import abc
from typing import List, Protocol, Sequence
from collections.abc import Sequence
from typing import Protocol

import attrs
import PIL.Image
Expand Down Expand Up @@ -79,7 +80,7 @@ def spec(self) -> DetectionFunctionSpec:

def detect(
self, context: DetectionFunctionContext, image: PIL.Image.Image
) -> List[models.LabeledShapeRequest]:
) -> list[models.LabeledShapeRequest]:
"""
Detects objects on the supplied image and returns the results.
Expand Down
15 changes: 8 additions & 7 deletions cvat-sdk/cvat_sdk/core/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@

import logging
import urllib.parse
from collections.abc import Generator, Sequence
from contextlib import contextmanager, suppress
from pathlib import Path
from time import sleep
from typing import Any, Dict, Generator, Optional, Sequence, Tuple, TypeVar
from typing import Any, Optional, TypeVar

import attrs
import packaging.specifiers as specifiers
Expand Down Expand Up @@ -95,7 +96,7 @@ def __init__(
if check_server_version:
self.check_server_version()

self._repos: Dict[str, Repo] = {}
self._repos: dict[str, Repo] = {}
"""A cache for created Repository instances"""

_ORG_SLUG_HEADER = "X-Organization"
Expand Down Expand Up @@ -183,7 +184,7 @@ def __exit__(self, exc_type, exc_value, traceback) -> None:
def close(self) -> None:
return self.__exit__(None, None, None)

def login(self, credentials: Tuple[str, str]) -> None:
def login(self, credentials: tuple[str, str]) -> None:
(auth, _) = self.api_client.auth_api.create_login(
models.LoginSerializerExRequest(username=credentials[0], password=credentials[1])
)
Expand Down Expand Up @@ -211,7 +212,7 @@ def wait_for_completion(
rq_id: str,
*,
status_check_period: Optional[int] = None,
) -> Tuple[models.Request, urllib3.HTTPResponse]:
) -> tuple[models.Request, urllib3.HTTPResponse]:
if status_check_period is None:
status_check_period = self.config.status_check_period

Expand Down Expand Up @@ -319,8 +320,8 @@ def make_endpoint_url(
path: str,
*,
psub: Optional[Sequence[Any]] = None,
kwsub: Optional[Dict[str, Any]] = None,
query_params: Optional[Dict[str, Any]] = None,
kwsub: Optional[dict[str, Any]] = None,
query_params: Optional[dict[str, Any]] = None,
) -> str:
url = self.host + path
if psub or kwsub:
Expand All @@ -331,7 +332,7 @@ def make_endpoint_url(


def make_client(
host: str, *, port: Optional[int] = None, credentials: Optional[Tuple[str, str]] = None
host: str, *, port: Optional[int] = None, credentials: Optional[tuple[str, str]] = None
) -> Client:
url = host.rstrip("/")
if port:
Expand Down
10 changes: 5 additions & 5 deletions cvat-sdk/cvat_sdk/core/downloading.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import json
from contextlib import closing
from pathlib import Path
from typing import TYPE_CHECKING, Any, Dict, Optional
from typing import TYPE_CHECKING, Any, Optional

from cvat_sdk.api_client.api_client import Endpoint
from cvat_sdk.core.helpers import expect_status
Expand Down Expand Up @@ -80,8 +80,8 @@ def prepare_file(
self,
endpoint: Endpoint,
*,
url_params: Optional[Dict[str, Any]] = None,
query_params: Optional[Dict[str, Any]] = None,
url_params: Optional[dict[str, Any]] = None,
query_params: Optional[dict[str, Any]] = None,
status_check_period: Optional[int] = None,
):
client = self._client
Expand Down Expand Up @@ -118,8 +118,8 @@ def prepare_and_download_file_from_endpoint(
endpoint: Endpoint,
filename: Path,
*,
url_params: Optional[Dict[str, Any]] = None,
query_params: Optional[Dict[str, Any]] = None,
url_params: Optional[dict[str, Any]] = None,
query_params: Optional[dict[str, Any]] = None,
pbar: Optional[ProgressReporter] = None,
status_check_period: Optional[int] = None,
):
Expand Down
5 changes: 3 additions & 2 deletions cvat-sdk/cvat_sdk/core/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
import io
import json
import warnings
from typing import Any, Dict, Iterable, List, Optional, Union
from collections.abc import Iterable
from typing import Any, Optional, Union

import tqdm
import urllib3
Expand All @@ -19,7 +20,7 @@

def get_paginated_collection(
endpoint: Endpoint, *, return_json: bool = False, **kwargs
) -> Union[List, List[Dict[str, Any]]]:
) -> Union[list, list[dict[str, Any]]]:
"""
Accumulates results from all the pages
"""
Expand Down
3 changes: 2 additions & 1 deletion cvat-sdk/cvat_sdk/core/progress.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
from __future__ import annotations

import contextlib
from typing import Generator, Iterable, Optional, TypeVar
from collections.abc import Generator, Iterable
from typing import Optional, TypeVar

T = TypeVar("T")

Expand Down
3 changes: 2 additions & 1 deletion cvat-sdk/cvat_sdk/core/proxies/annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
# SPDX-License-Identifier: MIT

from abc import ABC
from collections.abc import Sequence
from enum import Enum
from typing import Optional, Sequence
from typing import Optional

from cvat_sdk import models
from cvat_sdk.core.proxies.model_proxy import _EntityT
Expand Down
4 changes: 1 addition & 3 deletions cvat-sdk/cvat_sdk/core/proxies/issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

from __future__ import annotations

from typing import List

from cvat_sdk.api_client import apis, models
from cvat_sdk.core.helpers import get_paginated_collection
from cvat_sdk.core.proxies.model_proxy import (
Expand Down Expand Up @@ -53,7 +51,7 @@ class Issue(
):
_model_partial_update_arg = "patched_issue_write_request"

def get_comments(self) -> List[Comment]:
def get_comments(self) -> list[Comment]:
return [
Comment(self._client, m)
for m in get_paginated_collection(
Expand Down
11 changes: 6 additions & 5 deletions cvat-sdk/cvat_sdk/core/proxies/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

import io
import mimetypes
from collections.abc import Sequence
from pathlib import Path
from typing import TYPE_CHECKING, List, Optional, Sequence
from typing import TYPE_CHECKING, Optional

from PIL import Image

Expand Down Expand Up @@ -93,7 +94,7 @@ def download_frames(
outdir: StrPath = ".",
quality: str = "original",
filename_pattern: str = "frame_{frame_id:06d}{frame_ext}",
) -> Optional[List[Image.Image]]:
) -> Optional[list[Image.Image]]:
"""
Download the requested frame numbers for a job and save images as outdir/filename_pattern
"""
Expand Down Expand Up @@ -125,12 +126,12 @@ def get_meta(self) -> models.IDataMetaRead:
(meta, _) = self.api.retrieve_data_meta(self.id)
return meta

def get_labels(self) -> List[models.ILabel]:
def get_labels(self) -> list[models.ILabel]:
return get_paginated_collection(
self._client.api_client.labels_api.list_endpoint, job_id=self.id
)

def get_frames_info(self) -> List[models.IFrameMeta]:
def get_frames_info(self) -> list[models.IFrameMeta]:
return self.get_meta().frames

def remove_frames_by_ids(self, ids: Sequence[int]) -> None:
Expand All @@ -141,7 +142,7 @@ def remove_frames_by_ids(self, ids: Sequence[int]) -> None:
),
)

def get_issues(self) -> List[Issue]:
def get_issues(self) -> list[Issue]:
return [
Issue(self._client, m)
for m in get_paginated_collection(
Expand Down
24 changes: 10 additions & 14 deletions cvat-sdk/cvat_sdk/core/proxies/model_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,9 @@
TYPE_CHECKING,
Any,
Callable,
Dict,
Generic,
List,
Literal,
Optional,
Tuple,
Type,
TypeVar,
Union,
overload,
Expand Down Expand Up @@ -96,15 +92,15 @@ class Repo(ModelProxy[ModelType, ApiType]):
Implements group and management operations for entities.
"""

_entity_type: Type[Entity[ModelType, ApiType]]
_entity_type: type[Entity[ModelType, ApiType]]


### Utilities


def build_model_bases(
mt: Type[ModelType], at: Type[ApiType], *, api_member_name: Optional[str] = None
) -> Tuple[Type[Entity[ModelType, ApiType]], Type[Repo[ModelType, ApiType]]]:
mt: type[ModelType], at: type[ApiType], *, api_member_name: Optional[str] = None
) -> tuple[type[Entity[ModelType, ApiType]], type[Repo[ModelType, ApiType]]]:
"""
Helps to remove code duplication in declarations of derived classes
"""
Expand All @@ -128,7 +124,7 @@ class _RepoBase(Repo[ModelType, ApiType]):


class ModelCreateMixin(Generic[_EntityT, IModel]):
def create(self: Repo, spec: Union[Dict[str, Any], IModel]) -> _EntityT:
def create(self: Repo, spec: Union[dict[str, Any], IModel]) -> _EntityT:
"""
Creates a new object on the server and returns the corresponding local object
"""
Expand All @@ -149,12 +145,12 @@ def retrieve(self: Repo, obj_id: int) -> _EntityT:

class ModelListMixin(Generic[_EntityT]):
@overload
def list(self: Repo, *, return_json: Literal[False] = False) -> List[_EntityT]: ...
def list(self: Repo, *, return_json: Literal[False] = False) -> list[_EntityT]: ...

@overload
def list(self: Repo, *, return_json: Literal[True] = False) -> List[Any]: ...
def list(self: Repo, *, return_json: Literal[True] = False) -> list[Any]: ...

def list(self: Repo, *, return_json: bool = False) -> List[Union[_EntityT, Any]]:
def list(self: Repo, *, return_json: bool = False) -> list[Union[_EntityT, Any]]:
"""
Retrieves all objects from the server and returns them in basic or JSON format.
"""
Expand All @@ -174,8 +170,8 @@ class ModelUpdateMixin(ABC, Generic[IModel]):
def _model_partial_update_arg(self: Entity) -> str: ...

def _export_update_fields(
self: Entity, overrides: Optional[Union[Dict[str, Any], IModel]] = None
) -> Dict[str, Any]:
self: Entity, overrides: Optional[Union[dict[str, Any], IModel]] = None
) -> dict[str, Any]:
# TODO: support field conversion and assignment updating
# fields = to_json(self._model)

Expand All @@ -194,7 +190,7 @@ def fetch(self: Entity) -> Self:
(self._model, _) = self.api.retrieve(id=getattr(self, self._model_id_field))
return self

def update(self: Entity, values: Union[Dict[str, Any], IModel]) -> Self:
def update(self: Entity, values: Union[dict[str, Any], IModel]) -> Self:
"""
Commits model changes to the server
Expand Down
6 changes: 3 additions & 3 deletions cvat-sdk/cvat_sdk/core/proxies/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import io
import json
from pathlib import Path
from typing import TYPE_CHECKING, List, Optional
from typing import TYPE_CHECKING, Optional

from cvat_sdk.api_client import apis, models
from cvat_sdk.core.helpers import get_paginated_collection
Expand Down Expand Up @@ -72,15 +72,15 @@ def get_annotations(self) -> models.ILabeledData:
(annotations, _) = self.api.retrieve_annotations(self.id)
return annotations

def get_tasks(self) -> List[Task]:
def get_tasks(self) -> list[Task]:
return [
Task(self._client, m)
for m in get_paginated_collection(
self._client.api_client.tasks_api.list_endpoint, project_id=self.id
)
]

def get_labels(self) -> List[models.ILabel]:
def get_labels(self) -> list[models.ILabel]:
return get_paginated_collection(
self._client.api_client.labels_api.list_endpoint, project_id=self.id
)
Expand Down
Loading

0 comments on commit 5dc5265

Please sign in to comment.