From bdf657c5f322e2413839eb7416873ecdeeea149d Mon Sep 17 00:00:00 2001 From: WyattBlue Date: Sat, 24 Oct 2026 21:15:47 -0400 Subject: [PATCH] Type `Packet.stream` as `StreamT | None` Packets that weren't demuxed or encoded by a stream, such as `Packet()` or those from `CodecContext.encode` and `parse`, have no stream. The setter still requires a stream; assigning `None` raises `TypeError`. --- av/packet.pyi | 5 ++++- tests/test_streams.py | 8 ++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/av/packet.pyi b/av/packet.pyi index 7f8892e0f..0d8116d65 100644 --- a/av/packet.pyi +++ b/av/packet.pyi @@ -82,7 +82,10 @@ StreamT = TypeVar("StreamT", bound=Stream) @final class Packet(Buffer, Generic[StreamT]): - stream: StreamT + @property + def stream(self) -> StreamT | None: ... + @stream.setter + def stream(self, value: StreamT) -> None: ... stream_index: int @property def time_base(self) -> AVRational: ... diff --git a/tests/test_streams.py b/tests/test_streams.py index 85aba3e45..3200eb032 100644 --- a/tests/test_streams.py +++ b/tests/test_streams.py @@ -169,7 +169,11 @@ def audio_packets(discard: Discard | None) -> int: if discard is not None: c.streams.audio[0].discard = discard count = sum( - 1 for p in c.demux() if p.dts is not None and p.stream.type == "audio" + 1 + for p in c.demux() + if p.dts is not None + and p.stream is not None + and p.stream.type == "audio" ) c.close() return count @@ -389,7 +393,7 @@ def test_attachment_stream(self) -> None: for packet in ic.demux(ic.streams.video): if packet.size == 0: continue - updated_stream = stream_map.get(packet.stream.index) + updated_stream = stream_map.get(packet.stream_index) if isinstance(updated_stream, av.video.stream.VideoStream): packet.stream = updated_stream else: