diff --git a/av/container/core.pyi b/av/container/core.pyi index 5f25e7030..1bbd82b26 100644 --- a/av/container/core.pyi +++ b/av/container/core.pyi @@ -3,7 +3,18 @@ from enum import Flag, IntEnum from fractions import Fraction from pathlib import Path from types import TracebackType -from typing import Any, ClassVar, Literal, Self, TypedDict, cast, overload +from typing import ( + Any, + ClassVar, + Literal, + Self, + TypedDict, + cast, + overload, + type_check_only, +) + +from typing_extensions import disjoint_base from av.codec.hwaccel import HWAccel from av.format import ContainerFormat @@ -13,7 +24,7 @@ from .input import InputContainer from .output import OutputContainer from .streams import StreamContainer -Real = int | float | Fraction | AVRational +type _Real = int | float | Fraction | AVRational class Flags(Flag): gen_pts = cast(ClassVar[Flags], ...) @@ -69,6 +80,7 @@ class AudioCodec(IntEnum): pcm_u8 = cast(int, ...) pcm_vidc = cast(int, ...) +@type_check_only class Chapter(TypedDict): id: int start: int @@ -76,6 +88,7 @@ class Chapter(TypedDict): time_base: AVRational metadata: dict[str, str] +@disjoint_base class Container: name: str file: Any @@ -87,8 +100,8 @@ class Container: container_options: dict[str, str] streams: StreamContainer metadata: dict[str, str] - open_timeout: Real | None - read_timeout: Real | None + open_timeout: _Real | None + read_timeout: _Real | None flags: int video_codec_id: int def dumps_format(self) -> str: ... @@ -113,7 +126,7 @@ def open( options: dict[str, str] | None = None, container_options: dict[str, str] | None = None, buffer_size: int = 32768, - timeout: Real | None | tuple[Real | None, Real | None] = None, + timeout: _Real | None | tuple[_Real | None, _Real | None] = None, io_open: Callable[..., Any] | None = None, hwaccel: HWAccel | None = None, ) -> InputContainer: ... @@ -125,7 +138,7 @@ def open( options: dict[str, str] | None = None, container_options: dict[str, str] | None = None, buffer_size: int = 32768, - timeout: Real | None | tuple[Real | None, Real | None] = None, + timeout: _Real | None | tuple[_Real | None, _Real | None] = None, io_open: Callable[..., Any] | None = None, hwaccel: HWAccel | None = None, ) -> InputContainer: ... @@ -137,7 +150,7 @@ def open( options: dict[str, str] | None = None, container_options: dict[str, str] | None = None, buffer_size: int = 32768, - timeout: Real | None | tuple[Real | None, Real | None] = None, + timeout: _Real | None | tuple[_Real | None, _Real | None] = None, io_open: Callable[..., Any] | None = None, hwaccel: HWAccel | None = None, ) -> OutputContainer: ... @@ -149,7 +162,7 @@ def open( options: dict[str, str] | None = None, container_options: dict[str, str] | None = None, buffer_size: int = 32768, - timeout: Real | None | tuple[Real | None, Real | None] = None, + timeout: _Real | None | tuple[_Real | None, _Real | None] = None, io_open: Callable[..., Any] | None = None, hwaccel: HWAccel | None = None, ) -> InputContainer | OutputContainer: ... diff --git a/av/container/input.pyi b/av/container/input.pyi index 819defd1b..449b56f10 100644 --- a/av/container/input.pyi +++ b/av/container/input.pyi @@ -1,5 +1,5 @@ from collections.abc import Iterator -from typing import Any, overload +from typing import Any, final, overload from av.audio.frame import AudioFrame from av.audio.stream import AudioStream @@ -12,6 +12,7 @@ from av.video.stream import VideoStream from .core import Container +@final class InputContainer(Container): start_time: int start_time_realtime: int | None diff --git a/av/container/output.pyi b/av/container/output.pyi index 5487fdf77..e74cd6f65 100644 --- a/av/container/output.pyi +++ b/av/container/output.pyi @@ -1,6 +1,6 @@ from collections.abc import Sequence from fractions import Fraction -from typing import TypeVar, overload +from typing import final, overload from av.audio import _AudioCodecName from av.audio.stream import AudioStream @@ -15,8 +15,7 @@ from av.video.stream import VideoStream from .core import Container -_StreamT = TypeVar("_StreamT", bound=Stream) - +@final class OutputContainer(Container): @overload def add_stream( @@ -58,9 +57,9 @@ class OutputContainer(Container): rate: AVRational | Fraction | int | None = None, **kwargs, ) -> Stream: ... - def add_stream_from_template( - self, template: _StreamT, opaque: bool | None = None, **kwargs - ) -> _StreamT: ... + def add_stream_from_template[StreamT: Stream]( + self, template: StreamT, opaque: bool | None = None, **kwargs + ) -> StreamT: ... def add_attachment( self, name: str, mimetype: str, data: bytes ) -> AttachmentStream: ... diff --git a/av/container/streams.pyi b/av/container/streams.pyi index bdab1ea54..f401a1cf3 100644 --- a/av/container/streams.pyi +++ b/av/container/streams.pyi @@ -1,11 +1,12 @@ from collections.abc import Iterator -from typing import Literal, overload +from typing import Literal, final, overload from av.audio.stream import AudioStream from av.stream import AttachmentStream, DataStream, Stream from av.subtitles.stream import SubtitleStream from av.video.stream import VideoStream +@final class StreamContainer: video: tuple[VideoStream, ...] audio: tuple[AudioStream, ...] @@ -17,11 +18,9 @@ class StreamContainer: def __len__(self) -> int: ... def __iter__(self) -> Iterator[Stream]: ... @overload - def __getitem__(self, index: int) -> Stream: ... + def __getitem__(self, index: int, /) -> Stream: ... @overload - def __getitem__(self, index: slice) -> list[Stream]: ... - @overload - def __getitem__(self, index: int | slice) -> Stream | list[Stream]: ... + def __getitem__(self, index: slice, /) -> list[Stream]: ... def get( self, *args: int | Stream | dict[str, int | tuple[int, ...]],