Skip to content

Commit 6e9698f

Browse files
committed
Add more type stubs for tests
1 parent 339fc48 commit 6e9698f

File tree

6 files changed

+176
-174
lines changed

6 files changed

+176
-174
lines changed

tests/test_codec_context.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ class Options(TypedDict, total=False):
3636

3737
@overload
3838
def iter_raw_frames(
39-
path: str, packet_sizes: list, ctx: VideoCodecContext
39+
path: str, packet_sizes: list[int], ctx: VideoCodecContext
4040
) -> Iterator[VideoFrame]: ...
4141
@overload
4242
def iter_raw_frames(
43-
path: str, packet_sizes: list, ctx: AudioCodecContext
43+
path: str, packet_sizes: list[int], ctx: AudioCodecContext
4444
) -> Iterator[AudioFrame]: ...
4545
def iter_raw_frames(
46-
path: str, packet_sizes: list, ctx: VideoCodecContext | AudioCodecContext
46+
path: str, packet_sizes: list[int], ctx: VideoCodecContext | AudioCodecContext
4747
) -> Iterator[VideoFrame | AudioFrame]:
4848
with open(path, "rb") as f:
4949
for i, size in enumerate(packet_sizes):
@@ -85,14 +85,16 @@ def test_codec_tag(self):
8585
assert ctx.codec_tag == "xvid"
8686

8787
# wrong length
88-
with self.assertRaises(ValueError) as cm:
88+
with pytest.raises(
89+
ValueError, match="Codec tag should be a 4 character string"
90+
):
8991
ctx.codec_tag = "bob"
90-
assert str(cm.exception) == "Codec tag should be a 4 character string."
9192

9293
# wrong type
93-
with self.assertRaises(ValueError) as cm:
94+
with pytest.raises(
95+
ValueError, match="Codec tag should be a 4 character string"
96+
):
9497
ctx.codec_tag = 123
95-
assert str(cm.exception) == "Codec tag should be a 4 character string."
9698

9799
with av.open(fate_suite("h264/interlaced_crop.mp4")) as container:
98100
assert container.streams[0].codec_tag == "avc1"
@@ -175,14 +177,14 @@ def test_bits_per_coded_sample(self):
175177
with pytest.raises(ValueError):
176178
stream.codec_context.bits_per_coded_sample = 32
177179

178-
def test_parse(self):
180+
def test_parse(self) -> None:
179181
# This one parses into a single packet.
180182
self._assert_parse("mpeg4", fate_suite("h264/interlaced_crop.mp4"))
181183

182184
# This one parses into many small packets.
183185
self._assert_parse("mpeg2video", fate_suite("mpeg2/mpeg2_field_encoding.ts"))
184186

185-
def _assert_parse(self, codec_name, path):
187+
def _assert_parse(self, codec_name: str, path: str) -> None:
186188
fh = av.open(path)
187189
packets = []
188190
for packet in fh.demux(video=0):

tests/test_decode.py

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -57,46 +57,45 @@ def test_decode_audio_sample_count(self) -> None:
5757
)
5858
assert sample_count == total_samples
5959

60-
def test_decoded_time_base(self):
60+
def test_decoded_time_base(self) -> None:
6161
container = av.open(fate_suite("h264/interlaced_crop.mp4"))
6262
stream = container.streams.video[0]
6363

6464
assert stream.time_base == Fraction(1, 25)
6565

6666
for packet in container.demux(stream):
6767
for frame in packet.decode():
68+
assert not isinstance(frame, av.subtitles.subtitle.SubtitleSet)
6869
assert packet.time_base == frame.time_base
6970
assert stream.time_base == frame.time_base
7071
return
7172

72-
def test_decoded_motion_vectors(self):
73+
def test_decoded_motion_vectors(self) -> None:
7374
container = av.open(fate_suite("h264/interlaced_crop.mp4"))
7475
stream = container.streams.video[0]
7576
codec_context = stream.codec_context
7677
codec_context.options = {"flags2": "+export_mvs"}
7778

78-
for packet in container.demux(stream):
79-
for frame in packet.decode():
80-
vectors = frame.side_data.get("MOTION_VECTORS")
81-
if frame.key_frame:
82-
# Key frame don't have motion vectors
83-
assert vectors is None
84-
else:
85-
assert len(vectors) > 0
86-
return
87-
88-
def test_decoded_motion_vectors_no_flag(self):
79+
for frame in container.decode(stream):
80+
vectors = frame.side_data.get("MOTION_VECTORS")
81+
if frame.key_frame:
82+
# Key frame don't have motion vectors
83+
assert vectors is None
84+
else:
85+
assert vectors is not None and len(vectors) > 0
86+
return
87+
88+
def test_decoded_motion_vectors_no_flag(self) -> None:
8989
container = av.open(fate_suite("h264/interlaced_crop.mp4"))
9090
stream = container.streams.video[0]
9191

92-
for packet in container.demux(stream):
93-
for frame in packet.decode():
94-
vectors = frame.side_data.get("MOTION_VECTORS")
95-
if not frame.key_frame:
96-
assert vectors is None
97-
return
92+
for frame in container.decode(stream):
93+
vectors = frame.side_data.get("MOTION_VECTORS")
94+
if not frame.key_frame:
95+
assert vectors is None
96+
return
9897

99-
def test_decode_video_corrupt(self):
98+
def test_decode_video_corrupt(self) -> None:
10099
# write an empty file
101100
path = self.sandboxed("empty.h264")
102101
with open(path, "wb"):
@@ -114,7 +113,7 @@ def test_decode_video_corrupt(self):
114113
assert packet_count == 1
115114
assert frame_count == 0
116115

117-
def test_decode_close_then_use(self):
116+
def test_decode_close_then_use(self) -> None:
118117
container = av.open(fate_suite("h264/interlaced_crop.mp4"))
119118
container.close()
120119

tests/test_file_probing.py

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ def test_container_probing(self):
1414
assert self.file.duration == 6165333
1515
assert str(self.file.format) == "<av.ContainerFormat 'mpegts'>"
1616
assert self.file.format.name == "mpegts"
17-
self.assertEqual(
18-
self.file.format.long_name, "MPEG-TS (MPEG-2 Transport Stream)"
19-
)
17+
assert self.file.format.long_name == "MPEG-TS (MPEG-2 Transport Stream)"
2018
assert self.file.metadata == {}
2119
assert self.file.size == 207740
2220
assert self.file.start_time == 1400000
@@ -25,11 +23,8 @@ def test_container_probing(self):
2523
def test_stream_probing(self):
2624
stream = self.file.streams[0]
2725

28-
# check __repr__
29-
self.assertTrue(
30-
str(stream).startswith(
31-
"<av.AudioStream #0 aac_latm at 48000Hz, stereo, fltp at "
32-
)
26+
assert str(stream).startswith(
27+
"<av.AudioStream #0 aac_latm at 48000Hz, stereo, fltp at "
3328
)
3429

3530
# actual stream properties
@@ -65,9 +60,9 @@ def setUp(self):
6560
with open(path, "wb"):
6661
pass
6762

68-
self.file = av.open(path)
63+
self.file = av.open(path, "r")
6964

70-
def test_container_probing(self):
65+
def test_container_probing(self) -> None:
7166
assert self.file.bit_rate == 0
7267
assert self.file.duration is None
7368
assert str(self.file.format) == "<av.ContainerFormat 'flac'>"
@@ -78,14 +73,11 @@ def test_container_probing(self):
7873
assert self.file.start_time is None
7974
assert len(self.file.streams) == 1
8075

81-
def test_stream_probing(self):
76+
def test_stream_probing(self) -> None:
8277
stream = self.file.streams[0]
8378

84-
# ensure __repr__ does not crash
85-
self.assertTrue(
86-
str(stream).startswith(
87-
"<av.AudioStream #0 flac at 0Hz, 0 channels, None at "
88-
)
79+
assert str(stream).startswith(
80+
"<av.AudioStream #0 flac at 0Hz, 0 channels, None at "
8981
)
9082

9183
# actual stream properties
@@ -191,7 +183,6 @@ def test_container_probing(self) -> None:
191183
def test_stream_probing(self) -> None:
192184
stream = self.file.streams[0]
193185

194-
# check __repr__
195186
assert str(stream).startswith("<av.SubtitleStream #0 subtitle/mov_text at ")
196187

197188
# actual stream properties

tests/test_filters.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,14 @@
1212

1313

1414
def generate_audio_frame(
15-
frame_num, input_format="s16", layout="stereo", sample_rate=44100, frame_size=1024
16-
):
15+
frame_num: int,
16+
input_format: str = "s16",
17+
layout: str = "stereo",
18+
sample_rate: int = 44100,
19+
frame_size: int = 1024,
20+
) -> AudioFrame:
1721
"""
1822
Generate audio frame representing part of the sinusoidal wave
19-
:param input_format: default: s16
20-
:param layout: default: stereo
21-
:param sample_rate: default: 44100
22-
:param frame_size: default: 1024
23-
:param frame_num: frame number
24-
:return: audio frame for sinusoidal wave audio signal slice
2523
"""
2624
frame = AudioFrame(format=input_format, layout=layout, samples=frame_size)
2725
frame.sample_rate = sample_rate
@@ -31,7 +29,7 @@ def generate_audio_frame(
3129
data = np.zeros(frame_size, dtype=format_dtypes[input_format])
3230
for j in range(frame_size):
3331
data[j] = np.sin(2 * np.pi * (frame_num + j) * (i + 1) / float(frame_size))
34-
frame.planes[i].update(data)
32+
frame.planes[i].update(data) # type: ignore
3533

3634
return frame
3735

@@ -79,8 +77,8 @@ def test_generator_graph(self):
7977
lutrgb.link_to(sink)
8078

8179
# pads and links
82-
self.assertIs(src.outputs[0].link.output, lutrgb.inputs[0])
83-
self.assertIs(lutrgb.inputs[0].link.input, src.outputs[0])
80+
assert src.outputs[0].link.output is lutrgb.inputs[0]
81+
assert lutrgb.inputs[0].link.input is src.outputs[0]
8482

8583
frame = sink.pull()
8684
assert isinstance(frame, VideoFrame)

0 commit comments

Comments
 (0)