From 6610b984ea65f999cb5c98cf3e8c59280f6be474 Mon Sep 17 00:00:00 2001 From: WyattBlue Date: Sat, 15 Aug 2026 13:57:21 -0400 Subject: [PATCH] Use subscripted list/set/tuple annotations for Cython --- av/audio/codeccontext.py | 4 +++- av/audio/fifo.py | 2 +- av/audio/layout.py | 2 +- av/audio/resampler.pxd | 2 +- av/audio/resampler.py | 4 ++-- av/bitstream.py | 6 +++--- av/codec/codec.pxd | 4 +++- av/codec/codec.py | 4 ++-- av/codec/context.pxd | 2 +- av/codec/context.py | 14 +++++++------- av/codec/hwaccel.py | 2 +- av/container/core.py | 2 +- av/container/output.pxd | 2 +- av/container/output.py | 8 ++++---- av/container/streams.pxd | 2 +- av/device.py | 8 +++++--- av/filter/filter.py | 4 ++-- av/filter/link.pxd | 2 +- av/filter/link.py | 4 ++-- av/format.py | 10 +++++----- av/sidedata/sidedata.pxd | 2 +- av/sidedata/sidedata.py | 2 +- av/video/codeccontext.py | 2 +- 23 files changed, 50 insertions(+), 44 deletions(-) diff --git a/av/audio/codeccontext.py b/av/audio/codeccontext.py index 86ac107cf..1e018e3b8 100644 --- a/av/audio/codeccontext.py +++ b/av/audio/codeccontext.py @@ -11,7 +11,9 @@ @cython.cclass class AudioCodecContext(CodecContext): @cython.cfunc - def _prepare_frames_for_encode(self, input_frame: Frame | None) -> list: + def _prepare_frames_for_encode( + self, input_frame: Frame | None + ) -> list[Frame | None]: frame: AudioFrame | None = input_frame allow_var_frame_size: cython.bint = ( self.ptr.codec.capabilities & lib.AV_CODEC_CAP_VARIABLE_FRAME_SIZE diff --git a/av/audio/fifo.py b/av/audio/fifo.py index 00db125b7..a000711ab 100644 --- a/av/audio/fifo.py +++ b/av/audio/fifo.py @@ -180,7 +180,7 @@ def read_many(self, samples: cython.int, partial: cython.bint = False): """ frame: AudioFrame - frames: list = [] + frames: list[AudioFrame] = [] while True: frame = self.read(samples, partial=partial) if frame is not None: diff --git a/av/audio/layout.py b/av/audio/layout.py index f3adb3514..e68f5f1bd 100644 --- a/av/audio/layout.py +++ b/av/audio/layout.py @@ -71,7 +71,7 @@ def channels(self): buf: cython.char[16] buf2: cython.char[128] - results: list = [] + results: list[AudioChannel] = [] for index in range(self.layout.nb_channels): size = lib.av_channel_name( buf, diff --git a/av/audio/resampler.pxd b/av/audio/resampler.pxd index 2846bdfb6..6cb6c5c1d 100644 --- a/av/audio/resampler.pxd +++ b/av/audio/resampler.pxd @@ -16,4 +16,4 @@ cdef class AudioResampler: cdef readonly dict options cdef Graph graph - cpdef list resample(self, AudioFrame) + cpdef list[AudioFrame] resample(self, AudioFrame) diff --git a/av/audio/resampler.py b/av/audio/resampler.py index 785e522b9..5f614146b 100644 --- a/av/audio/resampler.py +++ b/av/audio/resampler.py @@ -40,7 +40,7 @@ def __cinit__( self.graph = None @cython.ccall - def resample(self, frame: AudioFrame | None) -> list: + def resample(self, frame: AudioFrame | None) -> list[AudioFrame]: """resample(frame) Convert the ``sample_rate``, ``channel_layout`` and/or ``format`` of @@ -126,7 +126,7 @@ def resample(self, frame: AudioFrame | None) -> list: self.graph.push(frame) - output: list = [] + output: list[AudioFrame] = [] while True: try: output.append(self.graph.pull()) diff --git a/av/bitstream.py b/av/bitstream.py index a3bc0088b..c5dbc43d0 100644 --- a/av/bitstream.py +++ b/av/bitstream.py @@ -89,7 +89,7 @@ def filter(self, packet: Packet | None = None): ) err_check(res) - output: list = [] + output: list[Packet] = [] while True: new_packet = Packet() with cython.nogil: @@ -115,8 +115,8 @@ def flush(self): @cython.cfunc -def get_filter_names() -> set: - names: set = set() +def get_filter_names() -> set[str]: + names: set[str] = set() ptr: cython.pointer[cython.const[lib.AVBitStreamFilter]] opaque: cython.p_void = cython.NULL while True: diff --git a/av/codec/codec.pxd b/av/codec/codec.pxd index 576c659b4..7739f6a57 100644 --- a/av/codec/codec.pxd +++ b/av/codec/codec.pxd @@ -1,5 +1,7 @@ cimport libav as lib +from av.codec.hwaccel cimport HWConfig + cdef class Codec: @@ -7,7 +9,7 @@ cdef class Codec: cdef const lib.AVCodecDescriptor *desc cdef readonly bint is_encoder - cdef tuple _hardware_configs + cdef tuple[HWConfig, ...] _hardware_configs cdef _init(self, name=?) diff --git a/av/codec/codec.py b/av/codec/codec.py index 0e8b958ec..1c5a6f956 100644 --- a/av/codec/codec.py +++ b/av/codec/codec.py @@ -3,7 +3,7 @@ import cython from cython.cimports import libav as lib from cython.cimports.av.audio.format import get_audio_format -from cython.cimports.av.codec.hwaccel import wrap_hwconfig +from cython.cimports.av.codec.hwaccel import HWConfig, wrap_hwconfig from cython.cimports.av.rational import from_avrational from cython.cimports.av.utils import avrational_to_fraction from cython.cimports.av.video.format import VideoFormat, get_pix_fmt, get_video_format @@ -268,7 +268,7 @@ def audio_formats(self): def hardware_configs(self): if self._hardware_configs: return self._hardware_configs - ret: list = [] + ret: list[HWConfig] = [] i: cython.int = 0 ptr: cython.pointer[cython.const[lib.AVCodecHWConfig]] while True: diff --git a/av/codec/context.pxd b/av/codec/context.pxd index a9cb702a7..0b89f45a9 100644 --- a/av/codec/context.pxd +++ b/av/codec/context.pxd @@ -44,7 +44,7 @@ cdef class CodecContext: # are bogus). It should take all info it needs from the context and/or stream. cdef _prepare_and_time_rebase_frames_for_encode(self, Frame frame) cdef void _setup_encode_hwframes(self) - cdef list _prepare_frames_for_encode(self, Frame frame) + cdef list[Frame | None] _prepare_frames_for_encode(self, Frame frame) cdef _setup_encoded_packet(self, Packet) cdef _setup_decoded_frame(self, Frame, Packet) diff --git a/av/codec/context.py b/av/codec/context.py index 7b3fb7cca..0e32e82a1 100644 --- a/av/codec/context.py +++ b/av/codec/context.py @@ -175,14 +175,14 @@ def _get_option_default( @cython.cfunc def _get_supported_options(obj: cython.p_void): - options: list = [] + options: list[CodecOption] = [] ptr: cython.pointer[cython.const[lib.AVOption]] = lib.av_opt_next(obj, cython.NULL) choice_ptr: cython.pointer[cython.const[lib.AVOption]] option_type: object while ptr != cython.NULL: if ptr.type != lib.AV_OPT_TYPE_CONST: - choices: list = [] + choices: list[CodecOptionChoice] = [] if ptr.unit != cython.NULL: choice_ptr = lib.av_opt_next(obj, cython.NULL) while choice_ptr != cython.NULL: @@ -257,7 +257,7 @@ def supported_options(self): self.codec.ptr ) child: cython.p_void - private: list = [] + private: list[CodecOption] = [] if ctx == cython.NULL: raise MemoryError("Cannot allocate codec context") try: @@ -471,7 +471,7 @@ def parse(self, raw_input=None): out_size: cython.int consumed: cython.int packet: Packet = None - packets: list = [] + packets: list[Packet] = [] while True: with cython.nogil: @@ -603,7 +603,7 @@ def _setup_encode_hwframes(self) -> cython.void: self.ptr.pix_fmt = hw_format @cython.cfunc - def _prepare_frames_for_encode(self, frame: Frame | None) -> list: + def _prepare_frames_for_encode(self, frame: Frame | None) -> list[Frame | None]: return [frame] @cython.cfunc @@ -738,7 +738,7 @@ def _decode(self, packet: Packet | None): ) err_check(res, "avcodec_send_packet()") - out: list = [] + out: list[Frame] = [] while True: try: frame = self._recv_frame() @@ -789,7 +789,7 @@ def profiles(self): :type: list[str] """ - ret: list = [] + ret: list[str] = [] if not self.ptr.codec or not self.codec.desc or not self.codec.desc.profiles: return ret diff --git a/av/codec/hwaccel.py b/av/codec/hwaccel.py index 93e7accec..efb408849 100644 --- a/av/codec/hwaccel.py +++ b/av/codec/hwaccel.py @@ -96,7 +96,7 @@ def is_supported(self): def hwdevices_available(): """Return the names of the hardware device types FFmpeg was built with, e.g. ``["cuda", "videotoolbox"]``.""" - result: list = [] + result: list[str] = [] x: lib.AVHWDeviceType = lib.AV_HWDEVICE_TYPE_NONE while True: x = lib.av_hwdevice_iterate_types(x) diff --git a/av/container/core.py b/av/container/core.py index bd82e86a4..79ec3656f 100755 --- a/av/container/core.py +++ b/av/container/core.py @@ -424,7 +424,7 @@ def metadata(self) -> dict: def chapters(self): self._assert_open() - result: list = [] + result: list[dict] = [] i: cython.Py_ssize_t for i in range(self.ptr.nb_chapters): ch = self.ptr.chapters[i] diff --git a/av/container/output.pxd b/av/container/output.pxd index 2ef93206f..cd1a780d6 100644 --- a/av/container/output.pxd +++ b/av/container/output.pxd @@ -8,7 +8,7 @@ from av.stream cimport Stream cdef class OutputContainer(Container): cdef lib.AVPacket *packet_ptr cdef dict _extradata_bsfs - cdef list _buffered_packets + cdef list[Packet] _buffered_packets cdef _mux_one(self, Packet packet) cdef _buffer_for_extradata(self, Packet packet) cdef _try_extract_extradata(self, Packet packet) diff --git a/av/container/output.py b/av/container/output.py index a33dadd61..2a9c9642f 100644 --- a/av/container/output.py +++ b/av/container/output.py @@ -39,7 +39,7 @@ def _set_codecpar_extradata( @cython.cfunc def close_output(self: OutputContainer): if self.packet_ptr != cython.NULL and self._buffered_packets: - buffered: list = self._buffered_packets + buffered: list[Packet] = self._buffered_packets self._buffered_packets = [] packet: Packet for packet in buffered: @@ -492,7 +492,7 @@ def start_encoding(self): # TODO: This does NOT handle options coming from 3 sources. # This is only a rough approximation of what would be cool to do. - used_options: set = set() + used_options: set[str] = set() stream: Stream # Finalize and open all streams. @@ -554,7 +554,7 @@ def supported_codecs(self): """ Returns a set of all codecs this format supports. """ - result: set = set() + result: set[str] = set() codec: cython.pointer[cython.const[lib.AVCodec]] = cython.NULL opaque: cython.p_void = cython.NULL @@ -665,7 +665,7 @@ def _buffer_for_extradata(self, packet: Packet): return True # Still waiting on some stream's extradata. # All extradata is resolved: write the header and flush buffered packets. - buffered: list = self._buffered_packets + buffered: list[Packet] = self._buffered_packets self._buffered_packets = [] buffered_packet: Packet for buffered_packet in buffered: diff --git a/av/container/streams.pxd b/av/container/streams.pxd index d62174992..69ada830a 100644 --- a/av/container/streams.pxd +++ b/av/container/streams.pxd @@ -4,5 +4,5 @@ from av.stream cimport Stream cdef class StreamContainer: - cdef list _streams + cdef list[Stream] _streams cdef void add_stream(self, Stream stream) diff --git a/av/device.py b/av/device.py index b5d5d110c..6ef9d13ba 100644 --- a/av/device.py +++ b/av/device.py @@ -39,8 +39,10 @@ def __repr__(self) -> str: @cython.cfunc -def _build_device_list(device_list: cython.pointer[lib.AVDeviceInfoList]) -> list: - devices: list = [] +def _build_device_list( + device_list: cython.pointer[lib.AVDeviceInfoList], +) -> list[DeviceInfo]: + devices: list[DeviceInfo] = [] i: cython.int j: cython.int device_info: cython.pointer[lib.AVDeviceInfo] @@ -50,7 +52,7 @@ def _build_device_list(device_list: cython.pointer[lib.AVDeviceInfoList]) -> lis for i in range(device_list.nb_devices): device_info = device_list.devices[i] - media_types: list = [] + media_types: list[str] = [] for j in range(device_info.nb_media_types): mt = device_info.media_types[j] s = lib.av_get_media_type_string(mt) diff --git a/av/filter/filter.py b/av/filter/filter.py index acbbc111a..c6a2138cc 100644 --- a/av/filter/filter.py +++ b/av/filter/filter.py @@ -51,8 +51,8 @@ def outputs(self): @cython.cfunc -def get_filter_names() -> set: - names: set = set() +def get_filter_names() -> set[str]: + names: set[str] = set() ptr: cython.pointer[cython.const[lib.AVFilter]] opaque: cython.p_void = cython.NULL while True: diff --git a/av/filter/link.pxd b/av/filter/link.pxd index 7f6aad2aa..3a66b2879 100644 --- a/av/filter/link.pxd +++ b/av/filter/link.pxd @@ -29,4 +29,4 @@ cdef class FilterContextPad(FilterPad): cdef FilterLink _link -cdef tuple alloc_filter_pads(Filter, const lib.AVFilterPad *ptr, bint is_input, FilterContext context=?) +cdef tuple[FilterPad, ...] alloc_filter_pads(Filter, const lib.AVFilterPad *ptr, bint is_input, FilterContext context=?) diff --git a/av/filter/link.py b/av/filter/link.py index 2b687e194..d9a9bddf5 100644 --- a/av/filter/link.py +++ b/av/filter/link.py @@ -126,11 +126,11 @@ def alloc_filter_pads( ptr: cython.pointer[cython.const[lib.AVFilterPad]], is_input: cython.bint, context: FilterContext | None = None, -) -> tuple: +) -> tuple[FilterPad, ...]: if not ptr: return () - pads: list = [] + pads: list[FilterPad] = [] # We need to be careful and check our bounds if we know what they are, # since the arrays on a AVFilterContext are not NULL terminated. diff --git a/av/format.py b/av/format.py index fec69f7a0..86eff2c73 100644 --- a/av/format.py +++ b/av/format.py @@ -113,7 +113,7 @@ def long_name(self): @property def extensions(self): - exts: set = set() + exts: set[str] = set() if self.iptr and self.iptr.extensions: exts.update(self.iptr.extensions.split(",")) if self.optr and self.optr.extensions: @@ -137,8 +137,8 @@ def no_file(self): @cython.cfunc -def get_output_format_names() -> set: - names: set = set() +def get_output_format_names() -> set[str]: + names: set[str] = set() ptr: cython.pointer[cython.const[lib.AVOutputFormat]] opaque: cython.p_void = cython.NULL while True: @@ -151,8 +151,8 @@ def get_output_format_names() -> set: @cython.cfunc -def get_input_format_names() -> set: - names: set = set() +def get_input_format_names() -> set[str]: + names: set[str] = set() ptr: cython.pointer[cython.const[lib.AVInputFormat]] opaque: cython.p_void = cython.NULL while True: diff --git a/av/sidedata/sidedata.pxd b/av/sidedata/sidedata.pxd index d97903c16..87c7d73d7 100644 --- a/av/sidedata/sidedata.pxd +++ b/av/sidedata/sidedata.pxd @@ -15,5 +15,5 @@ cdef int get_display_rotation(Frame frame) cdef class _SideDataContainer: cdef Frame frame - cdef list _by_index + cdef list[SideData] _by_index cdef dict _by_type diff --git a/av/sidedata/sidedata.py b/av/sidedata/sidedata.py index d0fa7b0d1..fcda121e1 100644 --- a/av/sidedata/sidedata.py +++ b/av/sidedata/sidedata.py @@ -106,7 +106,7 @@ def type(self): class _SideDataContainer: def __init__(self, frame: Frame): self.frame = frame - self._by_index: list = [] + self._by_index: list[SideData] = [] self._by_type: dict = {} i: cython.int diff --git a/av/video/codeccontext.py b/av/video/codeccontext.py index 27dbaacb9..27beca8d4 100644 --- a/av/video/codeccontext.py +++ b/av/video/codeccontext.py @@ -124,7 +124,7 @@ def _encode_upload_frame(self, vframe: VideoFrame) -> VideoFrame: return hwframe @cython.cfunc - def _prepare_frames_for_encode(self, input: Frame | None) -> list: + def _prepare_frames_for_encode(self, input: Frame | None) -> list[Frame | None]: if input is None or not input: return [None]