From 0f810722bb0f1e434463b655fd642c73da813461 Mon Sep 17 00:00:00 2001 From: Jeremy Leibs Date: Sat, 14 Dec 2024 15:29:33 +0100 Subject: [PATCH] Fix some assorted lints (#8460) ### What Not sure what changed to cause me to start hitting these locally in my pre-push hook, but they all seem valid. --- .../python/arkit_scenes/arkit_scenes/download_dataset.py | 8 ++++---- rerun_py/rerun_sdk/rerun/_send_columns.py | 4 ++-- rerun_py/rerun_sdk/rerun/archetypes/image_ext.py | 9 ++++++++- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/examples/python/arkit_scenes/arkit_scenes/download_dataset.py b/examples/python/arkit_scenes/arkit_scenes/download_dataset.py index 2ed6ca7797d5..156cf6a1c848 100644 --- a/examples/python/arkit_scenes/arkit_scenes/download_dataset.py +++ b/examples/python/arkit_scenes/arkit_scenes/download_dataset.py @@ -7,7 +7,7 @@ import subprocess import zipfile from pathlib import Path -from typing import Final +from typing import Final, Optional import pandas as pd @@ -196,14 +196,14 @@ def download_laser_scanner_point_clouds(laser_scanner_point_cloud_id: str, visit download_file(file_url, filename, laser_scanner_point_clouds_folder_path) -def get_metadata(dataset: str, download_dir: Path) -> pd.DataFrame: +def get_metadata(dataset: str, download_dir: Path) -> Optional[pd.DataFrame]: filename = "metadata.csv" url = f"{ARkitscense_url}/threedod/{filename}" if "3dod" == dataset else f"{ARkitscense_url}/{dataset}/{filename}" dst_folder = download_dir / dataset dst_file = dst_folder / filename if not download_file(url, filename, dst_folder): - return + return None metadata = pd.read_csv(dst_file) return metadata @@ -235,7 +235,7 @@ def download_data( """ metadata = get_metadata(dataset, download_dir) - if None is metadata: + if metadata is None: print(f"Error retrieving metadata for dataset {dataset}") return diff --git a/rerun_py/rerun_sdk/rerun/_send_columns.py b/rerun_py/rerun_sdk/rerun/_send_columns.py index 888ff21d6648..683d442f6053 100644 --- a/rerun_py/rerun_sdk/rerun/_send_columns.py +++ b/rerun_py/rerun_sdk/rerun/_send_columns.py @@ -1,6 +1,6 @@ from __future__ import annotations -from typing import Iterable, Protocol, TypeVar, Union +from typing import Iterable, Protocol, TypeVar import pyarrow as pa import rerun_bindings as bindings @@ -121,7 +121,7 @@ def as_arrow_array(self) -> pa.Array: def send_columns( entity_path: str, times: Iterable[TimeColumnLike], - components: Iterable[Union[ComponentBatchLike]], + components: Iterable[ComponentBatchLike], recording: RecordingStream | None = None, strict: bool | None = None, ) -> None: diff --git a/rerun_py/rerun_sdk/rerun/archetypes/image_ext.py b/rerun_py/rerun_sdk/rerun/archetypes/image_ext.py index 8e77711a44c7..8c31e41bfe4f 100644 --- a/rerun_py/rerun_sdk/rerun/archetypes/image_ext.py +++ b/rerun_py/rerun_sdk/rerun/archetypes/image_ext.py @@ -18,6 +18,8 @@ from ..error_utils import _send_warning_or_raise, catch_and_log_exceptions if TYPE_CHECKING: + from PIL import Image as PILImage + ImageLike = Union[ npt.NDArray[np.float16], npt.NDArray[np.float32], @@ -30,18 +32,23 @@ npt.NDArray[np.uint32], npt.NDArray[np.uint64], npt.NDArray[np.uint8], + PILImage.Image, ] from . import EncodedImage, Image def _to_numpy(tensor: ImageLike) -> npt.NDArray[Any]: + from PIL import Image as PILImage + # isinstance is 4x faster than catching AttributeError if isinstance(tensor, np.ndarray): return tensor + if isinstance(tensor, PILImage.Image): + return np.array(tensor, copy=False) try: # Make available to the cpu - return tensor.numpy(force=True) # type: ignore[union-attr] + return tensor.numpy(force=True) # type: ignore[union-attr, no-any-return] except AttributeError: return np.array(tensor, copy=False)