diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 84e404a3e..775c100d0 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -14,6 +14,14 @@ repos: - id: check-added-large-files args: ['--maxkb=1000'] + - repo: https://github.com/asottile/pyupgrade + rev: v3.21.2 + hooks: + - id: pyupgrade + args: [--py310-plus] + files: ^(test|src)/ + exclude: ^examples/ + - repo: https://github.com/omnilib/ufmt rev: v2.6.0 hooks: diff --git a/src/torchcodec/_frame.py b/src/torchcodec/_frame.py index 87e758ae5..2ceb890b7 100644 --- a/src/torchcodec/_frame.py +++ b/src/torchcodec/_frame.py @@ -6,8 +6,8 @@ import dataclasses +from collections.abc import Iterable, Iterator from dataclasses import dataclass -from typing import Iterable, Iterator from torch import Tensor diff --git a/src/torchcodec/decoders/_decoder_utils.py b/src/torchcodec/decoders/_decoder_utils.py index 27b745ea1..6ca53a330 100644 --- a/src/torchcodec/decoders/_decoder_utils.py +++ b/src/torchcodec/decoders/_decoder_utils.py @@ -7,11 +7,11 @@ import contextvars import io + +from collections.abc import Generator from contextlib import contextmanager from pathlib import Path -from typing import Generator - from torch import Tensor from torchcodec import _core as core diff --git a/src/torchcodec/decoders/_video_decoder.py b/src/torchcodec/decoders/_video_decoder.py index 45d6dfabb..03f0d539a 100644 --- a/src/torchcodec/decoders/_video_decoder.py +++ b/src/torchcodec/decoders/_video_decoder.py @@ -8,8 +8,9 @@ import io import json import numbers +from collections.abc import Sequence from pathlib import Path -from typing import Literal, Sequence +from typing import Literal import torch from torch import device as torch_device, nn, Tensor diff --git a/src/torchcodec/samplers/_common.py b/src/torchcodec/samplers/_common.py index 3a0bd94e4..0739c5538 100644 --- a/src/torchcodec/samplers/_common.py +++ b/src/torchcodec/samplers/_common.py @@ -1,4 +1,4 @@ -from typing import Callable +from collections.abc import Callable from torchcodec import FrameBatch diff --git a/src/torchcodec/transforms/_decoder_transforms.py b/src/torchcodec/transforms/_decoder_transforms.py index fbb98b667..378e5235d 100644 --- a/src/torchcodec/transforms/_decoder_transforms.py +++ b/src/torchcodec/transforms/_decoder_transforms.py @@ -6,8 +6,8 @@ from abc import ABC, abstractmethod +from collections.abc import Sequence from types import ModuleType -from typing import Sequence import torch from torch import nn diff --git a/test/test_decoders.py b/test/test_decoders.py index efa2d11c8..d688ffbf0 100644 --- a/test/test_decoders.py +++ b/test/test_decoders.py @@ -127,7 +127,7 @@ def test_create_fails(self, Decoder): # user mistakenly forgets to specify binary reading when creating a file # like object from open() with pytest.raises(TypeError, match="binary reading?"): - Decoder(open(NASA_VIDEO.path, "r")) + Decoder(open(NASA_VIDEO.path)) class TestVideoDecoder: @@ -1334,7 +1334,7 @@ def test_custom_frame_mappings_json_and_bytes( # Optionally open the custom frame mappings file if it is a file path # or use a null context if it is a string. with ( - open(custom_frame_mappings, "r") + open(custom_frame_mappings) if hasattr(custom_frame_mappings, "read") else contextlib.nullcontext() ) as custom_frame_mappings: @@ -1406,7 +1406,7 @@ def test_custom_frame_mappings_init_fails_invalid_json(self, tmp_path, device): f.write("invalid input") # Test both file object and string - with open(invalid_json_path, "r") as file_obj: + with open(invalid_json_path) as file_obj: for custom_frame_mappings in [ file_obj, file_obj.read(), diff --git a/test/test_ops.py b/test/test_ops.py index 607fcdc91..0ec45155d 100644 --- a/test/test_ops.py +++ b/test/test_ops.py @@ -1005,7 +1005,7 @@ def seek(self, offset: int, whence: int) -> int: class SeekMethodMissing: def read(self, size: int) -> bytes: - return bytes() + return b"" with pytest.raises(RuntimeError, match="must implement a seek method"): create_from_file_like(SeekMethodMissing(), "approximate") @@ -1016,7 +1016,7 @@ def __init__(self, file: io.RawIOBase): # io.RawIOBase says we should accept a single int; wrong signature on purpose def read(self) -> bytes: - return bytes() + return b"" def seek(self, offset: int, whence: int) -> int: return self._file.seeK(offset, whence) diff --git a/test/utils.py b/test/utils.py index 39fa96597..6f4c77b76 100644 --- a/test/utils.py +++ b/test/utils.py @@ -262,7 +262,7 @@ def __post_init__(self): "You need to submit this file, or specify the `frames` field manually." ) - with open(frames_info_path, "r") as f: + with open(frames_info_path) as f: frames_info = json.loads(f.read()) self.frames[stream_index] = { frame_index: TestFrameInfo(