Skip to content

Commit f63bf85

Browse files
committed
Avoid importing collections without guard
1 parent 272bbce commit f63bf85

File tree

6 files changed

+59
-55
lines changed

6 files changed

+59
-55
lines changed

auto_editor/formats/shotcut.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
from __future__ import annotations
22

33
import xml.etree.ElementTree as ET
4-
from typing import TYPE_CHECKING, Any, cast
4+
from typing import TYPE_CHECKING, cast
55

66
from auto_editor.timeline import v3
77
from auto_editor.utils.func import aspect_ratio, to_timecode
88

99
if TYPE_CHECKING:
1010
from collections.abc import Sequence
11+
from typing import Any
1112

1213
from auto_editor.timeline import TlAudio, TlVideo
1314
from auto_editor.utils.log import Log

auto_editor/lib/contracts.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
from __future__ import annotations
22

3-
from collections.abc import Callable
43
from dataclasses import dataclass
54
from fractions import Fraction
6-
from typing import Any
5+
from typing import TYPE_CHECKING
76

87
from numpy import float64
98

109
from .data_structs import Sym, print_str
1110
from .err import MyError
1211

12+
if TYPE_CHECKING:
13+
from collections.abc import Callable
14+
from typing import Any
15+
1316

1417
@dataclass(slots=True)
1518
class Contract:

auto_editor/subcommands/test.py

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
# type: ignore
21
from __future__ import annotations
32

43
import os
54
import shutil
65
import subprocess
76
import sys
8-
from collections.abc import Callable
97
from dataclasses import dataclass, field
108
from fractions import Fraction
119
from time import perf_counter
12-
from typing import Any
10+
from typing import TYPE_CHECKING
1311

1412
import numpy as np
1513

@@ -20,6 +18,12 @@
2018
from auto_editor.utils.log import Log
2119
from auto_editor.vanparse import ArgumentParser
2220

21+
if TYPE_CHECKING:
22+
from collections.abc import Callable
23+
from typing import Any
24+
25+
from auto_editor.vanparse import ArgumentParser
26+
2327

2428
@dataclass(slots=True)
2529
class TestArgs:
@@ -29,7 +33,7 @@ class TestArgs:
2933
category: str = "cli"
3034

3135

32-
def test_options(parser):
36+
def test_options(parser: ArgumentParser) -> ArgumentParser:
3337
parser.add_argument("--only", "-n", nargs="*")
3438
parser.add_argument("--no-fail-fast", flag=True)
3539
parser.add_required(
@@ -47,14 +51,6 @@ def pipe_to_console(cmd: list[str]) -> tuple[int, str, str]:
4751
return process.returncode, stdout.decode("utf-8"), stderr.decode("utf-8")
4852

4953

50-
class Checker:
51-
def __init__(self, log: Log):
52-
self.log = log
53-
54-
def check(self, path: str) -> FileInfo:
55-
return initFileInfo(path, self.log)
56-
57-
5854
class Runner:
5955
def __init__(self) -> None:
6056
self.program = [sys.executable, "-m", "auto_editor"]
@@ -176,7 +172,10 @@ def main(sys_args: list[str] | None = None):
176172
args = test_options(ArgumentParser("test")).parse_args(TestArgs, sys_args)
177173

178174
run = Runner()
179-
checker = Checker(Log())
175+
log = Log()
176+
177+
def fileinfo(path: str) -> FileInfo:
178+
return initFileInfo(path, log)
180179

181180
### Tests ###
182181

@@ -229,7 +228,7 @@ def desc():
229228

230229
def example():
231230
out = run.main(inputs=["example.mp4"], cmd=[])
232-
cn = checker.check(out)
231+
cn = fileinfo(out)
233232
video = cn.videos[0]
234233

235234
assert video.fps == 30
@@ -295,7 +294,7 @@ def gif():
295294
out = run.main(
296295
["resources/only-video/man-on-green-screen.gif"], ["--edit", "none"]
297296
)
298-
assert checker.check(out).videos[0].codec == "gif"
297+
assert fileinfo(out).videos[0].codec == "gif"
299298

300299
return out
301300

@@ -319,11 +318,11 @@ def output_extension():
319318
out = run.main(inputs=["example.mp4"], cmd=[], output="out")
320319

321320
assert out == "out.mp4"
322-
assert checker.check(out).videos[0].codec == "h264"
321+
assert fileinfo(out).videos[0].codec == "h264"
323322

324323
out = run.main(inputs=["resources/testsrc.mkv"], cmd=[], output="out")
325324
assert out == "out.mkv"
326-
assert checker.check(out).videos[0].codec == "h264"
325+
assert fileinfo(out).videos[0].codec == "h264"
327326

328327
return "out.mp4", "out.mkv"
329328

@@ -357,22 +356,22 @@ def premiere_named_export():
357356
run.main(["example.mp4"], ["--export", 'premiere:name="Foo Bar"'])
358357

359358
def resolution_and_scale():
360-
cn = checker.check(run.main(["example.mp4"], ["--scale", "1.5"]))
359+
cn = fileinfo(run.main(["example.mp4"], ["--scale", "1.5"]))
361360

362361
assert cn.videos[0].fps == 30
363362
assert cn.videos[0].width == 1920
364363
assert cn.videos[0].height == 1080
365364
assert cn.audios[0].samplerate == 48000
366365

367-
cn = checker.check(run.main(["example.mp4"], ["--scale", "0.2"]))
366+
cn = fileinfo(run.main(["example.mp4"], ["--scale", "0.2"]))
368367

369368
assert cn.videos[0].fps == 30
370369
assert cn.videos[0].width == 256
371370
assert cn.videos[0].height == 144
372371
assert cn.audios[0].samplerate == 48000
373372

374373
out = run.main(["example.mp4"], ["-res", "700,380", "-b", "darkgreen"])
375-
cn = checker.check(out)
374+
cn = fileinfo(out)
376375

377376
assert cn.videos[0].fps == 30
378377
assert cn.videos[0].width == 700
@@ -416,7 +415,7 @@ def multi_track_edit():
416415
["--edit", "audio:stream=1"],
417416
"out.mov",
418417
)
419-
assert len(checker.check(out).audios) == 1
418+
assert len(fileinfo(out).audios) == 1
420419

421420
return out
422421

@@ -427,7 +426,7 @@ def concat():
427426

428427
def concat_mux_tracks():
429428
out = run.main(["example.mp4", "resources/multi-track.mov"], [], "out.mov")
430-
assert len(checker.check(out).audios) == 1
429+
assert len(fileinfo(out).audios) == 1
431430

432431
return out
433432

@@ -437,30 +436,30 @@ def concat_multiple_tracks():
437436
["--keep-tracks-separate"],
438437
"out.mov",
439438
)
440-
assert len(checker.check(out).audios) == 2
439+
assert len(fileinfo(out).audios) == 2
441440
out = run.main(
442441
["example.mp4", "resources/multi-track.mov"],
443442
["--keep-tracks-separate"],
444443
"out.mov",
445444
)
446-
assert len(checker.check(out).audios) == 2
445+
assert len(fileinfo(out).audios) == 2
447446

448447
return out
449448

450449
def frame_rate():
451-
cn = checker.check(run.main(["example.mp4"], ["-r", "15", "--no-seek"]))
450+
cn = fileinfo(run.main(["example.mp4"], ["-r", "15", "--no-seek"]))
452451
video = cn.videos[0]
453452
assert video.fps == 15
454453
assert video.time_base == Fraction(1, 15)
455454
assert float(video.duration) - 17.33333333333333333333333 < 3
456455

457-
cn = checker.check(run.main(["example.mp4"], ["-r", "20"]))
456+
cn = fileinfo(run.main(["example.mp4"], ["-r", "20"]))
458457
video = cn.videos[0]
459458
assert video.fps == 20
460459
assert video.time_base == Fraction(1, 20)
461460
assert float(video.duration) - 17.33333333333333333333333 < 2
462461

463-
cn = checker.check(out := run.main(["example.mp4"], ["-r", "60"]))
462+
cn = fileinfo(out := run.main(["example.mp4"], ["-r", "60"]))
464463
video = cn.videos[0]
465464

466465
assert video.fps == 60
@@ -471,22 +470,22 @@ def frame_rate():
471470

472471
def embedded_image():
473472
out1 = run.main(["resources/embedded-image/h264-png.mp4"], [])
474-
cn = checker.check(out1)
473+
cn = fileinfo(out1)
475474
assert cn.videos[0].codec == "h264"
476475
assert cn.videos[1].codec == "png"
477476

478477
out2 = run.main(["resources/embedded-image/h264-mjpeg.mp4"], [])
479-
cn = checker.check(out2)
478+
cn = fileinfo(out2)
480479
assert cn.videos[0].codec == "h264"
481480
assert cn.videos[1].codec == "mjpeg"
482481

483482
out3 = run.main(["resources/embedded-image/h264-png.mkv"], [])
484-
cn = checker.check(out3)
483+
cn = fileinfo(out3)
485484
assert cn.videos[0].codec == "h264"
486485
assert cn.videos[1].codec == "png"
487486

488487
out4 = run.main(["resources/embedded-image/h264-mjpeg.mkv"], [])
489-
cn = checker.check(out4)
488+
cn = fileinfo(out4)
490489
assert cn.videos[0].codec == "h264"
491490
assert cn.videos[1].codec == "mjpeg"
492491

@@ -532,7 +531,7 @@ def yuv442p():
532531
# Issue 280
533532
def SAR():
534533
out = run.main(["resources/SAR-2by3.mp4"], [])
535-
assert checker.check(out).videos[0].sar == Fraction(2, 3)
534+
assert fileinfo(out).videos[0].sar == Fraction(2, 3)
536535

537536
return out
538537

auto_editor/timeline.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
from __future__ import annotations
22

3-
from collections.abc import Iterator
43
from dataclasses import dataclass
5-
from fractions import Fraction
6-
from typing import Any
4+
from typing import TYPE_CHECKING
75

8-
from auto_editor.ffwrapper import FileInfo
96
from auto_editor.lib.contracts import *
10-
from auto_editor.utils.chunks import Chunks
117
from auto_editor.utils.cmdkw import Required, pAttr, pAttrs
12-
from auto_editor.utils.types import (
13-
anchor,
14-
color,
15-
natural,
16-
number,
17-
threshold,
18-
)
8+
from auto_editor.utils.types import anchor, color, natural, number, threshold
9+
10+
if TYPE_CHECKING:
11+
from collections.abc import Iterator
12+
from fractions import Fraction
13+
from typing import Any
14+
15+
from auto_editor.ffwrapper import FileInfo
16+
from auto_editor.utils.chunks import Chunks
1917

2018

2119
@dataclass(slots=True)

auto_editor/utils/func.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
from __future__ import annotations
22

3-
from collections.abc import Callable
4-
from fractions import Fraction
3+
from typing import TYPE_CHECKING
54

65
import numpy as np
7-
from numpy.typing import NDArray
86

9-
from auto_editor.utils.log import Log
7+
if TYPE_CHECKING:
8+
from collections.abc import Callable
9+
from fractions import Fraction
1010

11-
BoolList = NDArray[np.bool_]
12-
BoolOperand = Callable[[BoolList, BoolList], BoolList]
11+
from numpy.typing import NDArray
12+
13+
from auto_editor.utils.log import Log
14+
15+
BoolList = NDArray[np.bool_]
16+
BoolOperand = Callable[[BoolList, BoolList], BoolList]
1317

1418

1519
def boolop(a: BoolList, b: BoolList, call: BoolOperand) -> BoolList:

auto_editor/vanparse.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import re
55
import sys
66
import textwrap
7-
from collections.abc import Iterator
87
from dataclasses import dataclass
98
from io import StringIO
109
from shutil import get_terminal_size
@@ -14,7 +13,7 @@
1413
from auto_editor.utils.types import CoerceError
1514

1615
if TYPE_CHECKING:
17-
from collections.abc import Callable
16+
from collections.abc import Callable, Iterator
1817
from typing import Any, Literal, TypeVar
1918

2019
T = TypeVar("T")

0 commit comments

Comments
 (0)