Skip to content

Commit ab05f4d

Browse files
committed
Handled more mypy issues in _config/utils.py
1 parent 7c3e8cc commit ab05f4d

File tree

1 file changed

+37
-30
lines changed

1 file changed

+37
-30
lines changed

manim/_config/utils.py

+37-30
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import os
2121
import re
2222
import sys
23-
from collections.abc import Iterable, Iterator, Mapping, MutableMapping
23+
from collections.abc import Iterator, Mapping, MutableMapping
2424
from pathlib import Path
2525
from typing import TYPE_CHECKING, Any, ClassVar, NoReturn
2626

@@ -131,7 +131,7 @@ def make_config_parser(
131131
return parser
132132

133133

134-
def _determine_quality(qual: str) -> str:
134+
def _determine_quality(qual: str | None) -> str:
135135
for quality, values in constants.QUALITIES.items():
136136
if values["flag"] is not None and values["flag"] == qual:
137137
return quality
@@ -334,6 +334,7 @@ def __len__(self) -> int:
334334

335335
def __contains__(self, key: object) -> bool:
336336
try:
337+
assert isinstance(key, str)
337338
self.__getitem__(key)
338339
return True
339340
except AttributeError:
@@ -653,8 +654,8 @@ def digest_parser(self, parser: configparser.ConfigParser) -> Self:
653654

654655
# plugins
655656
plugins = parser["CLI"].get("plugins", fallback="", raw=True)
656-
plugins = [] if plugins == "" else plugins.split(",")
657-
self.plugins = plugins
657+
plugin_list = [] if plugins is None or plugins == "" else plugins.split(",")
658+
self.plugins = plugin_list
658659
# the next two must be set AFTER digesting pixel_width and pixel_height
659660
self["frame_height"] = parser["CLI"].getfloat("frame_height", 8.0)
660661
width = parser["CLI"].getfloat("frame_width", None)
@@ -664,31 +665,31 @@ def digest_parser(self, parser: configparser.ConfigParser) -> Self:
664665
self["frame_width"] = width
665666

666667
# other logic
667-
val = parser["CLI"].get("tex_template_file")
668-
if val:
669-
self.tex_template_file = val
668+
tex_template_file = parser["CLI"].get("tex_template_file")
669+
if tex_template_file:
670+
self.tex_template_file = Path(tex_template_file)
670671

671-
val = parser["CLI"].get("progress_bar")
672-
if val:
673-
self.progress_bar = val
672+
progress_bar = parser["CLI"].get("progress_bar")
673+
if progress_bar:
674+
self.progress_bar = progress_bar
674675

675-
val = parser["ffmpeg"].get("loglevel")
676-
if val:
677-
self.ffmpeg_loglevel = val
676+
ffmpeg_loglevel = parser["ffmpeg"].get("loglevel")
677+
if ffmpeg_loglevel:
678+
self.ffmpeg_loglevel = ffmpeg_loglevel
678679

679680
try:
680-
val = parser["jupyter"].getboolean("media_embed")
681+
media_embed = parser["jupyter"].getboolean("media_embed")
681682
except ValueError:
682-
val = None
683-
self.media_embed = val
683+
media_embed = None
684+
self.media_embed = media_embed
684685

685-
val = parser["jupyter"].get("media_width")
686-
if val:
687-
self.media_width = val
686+
media_width = parser["jupyter"].get("media_width")
687+
if media_width:
688+
self.media_width = media_width
688689

689-
val = parser["CLI"].get("quality", fallback="", raw=True)
690-
if val:
691-
self.quality = _determine_quality(val)
690+
quality = parser["CLI"].get("quality", fallback="", raw=True)
691+
if quality:
692+
self.quality = _determine_quality(quality)
692693

693694
return self
694695

@@ -1036,7 +1037,7 @@ def verbosity(self, val: str) -> None:
10361037
logger.setLevel(val)
10371038

10381039
@property
1039-
def format(self) -> str:
1040+
def format(self) -> str | None:
10401041
"""File format; "png", "gif", "mp4", "webm" or "mov"."""
10411042
return self._d["format"]
10421043

@@ -1068,7 +1069,7 @@ def ffmpeg_loglevel(self, val: str) -> None:
10681069
logging.getLogger("libav").setLevel(self.ffmpeg_loglevel)
10691070

10701071
@property
1071-
def media_embed(self) -> bool:
1072+
def media_embed(self) -> bool | None:
10721073
"""Whether to embed videos in Jupyter notebook."""
10731074
return self._d["media_embed"]
10741075

@@ -1106,6 +1107,8 @@ def pixel_height(self, value: int) -> None:
11061107
@property
11071108
def aspect_ratio(self) -> float:
11081109
"""Aspect ratio (width / height) in pixels (--resolution, -r)."""
1110+
assert isinstance(self._d["pixel_width"], int)
1111+
assert isinstance(self._d["pixel_height"], int)
11091112
return self._d["pixel_width"] / self._d["pixel_height"]
11101113

11111114
@property
@@ -1129,6 +1132,7 @@ def frame_width(self, value: float) -> None:
11291132
@property
11301133
def frame_y_radius(self) -> float:
11311134
"""Half the frame height (no flag)."""
1135+
assert isinstance(self._d["frame_height"], float)
11321136
return self._d["frame_height"] / 2
11331137

11341138
@frame_y_radius.setter
@@ -1140,6 +1144,7 @@ def frame_y_radius(self, value: float) -> None:
11401144
@property
11411145
def frame_x_radius(self) -> float:
11421146
"""Half the frame width (no flag)."""
1147+
assert isinstance(self._d["frame_width"], float)
11431148
return self._d["frame_width"] / 2
11441149

11451150
@frame_x_radius.setter
@@ -1304,6 +1309,7 @@ def quality(self, value: str | None) -> None:
13041309
@property
13051310
def transparent(self) -> bool:
13061311
"""Whether the background opacity is less than 1.0 (-t)."""
1312+
assert isinstance(self._d["background_opacity"], float)
13071313
return self._d["background_opacity"] < 1.0
13081314

13091315
@transparent.setter
@@ -1447,7 +1453,7 @@ def enable_gui(self, value: bool) -> None:
14471453
self._set_boolean("enable_gui", value)
14481454

14491455
@property
1450-
def gui_location(self) -> tuple[Any]:
1456+
def gui_location(self) -> tuple[int]:
14511457
"""Enable GUI interaction."""
14521458
return self._d["gui_location"]
14531459

@@ -1631,6 +1637,7 @@ def get_dir(self, key: str, **kwargs: Any) -> Path:
16311637
all_args["quality"] = f"{self.pixel_height}p{self.frame_rate:g}"
16321638

16331639
path = self._d[key]
1640+
assert isinstance(path, str)
16341641
while "{" in path:
16351642
try:
16361643
path = path.format(**all_args)
@@ -1730,7 +1737,7 @@ def custom_folders(self, value: str | Path) -> None:
17301737
self._set_dir("custom_folders", value)
17311738

17321739
@property
1733-
def input_file(self) -> str:
1740+
def input_file(self) -> str | Path:
17341741
"""Input file name."""
17351742
return self._d["input_file"]
17361743

@@ -1759,7 +1766,7 @@ def scene_names(self, value: list[str]) -> None:
17591766
@property
17601767
def tex_template(self) -> TexTemplate:
17611768
"""Template used when rendering Tex. See :class:`.TexTemplate`."""
1762-
if not hasattr(self, "_tex_template") or not self._tex_template:
1769+
if not hasattr(self, "_tex_template") or not self._tex_template: # type: ignore[has-type]
17631770
fn = self._d["tex_template_file"]
17641771
if fn:
17651772
self._tex_template = TexTemplate.from_file(fn)
@@ -1795,7 +1802,7 @@ def plugins(self) -> list[str]:
17951802
return self._d["plugins"]
17961803

17971804
@plugins.setter
1798-
def plugins(self, value: list[str]):
1805+
def plugins(self, value: list[str]) -> None:
17991806
self._d["plugins"] = value
18001807

18011808

@@ -1842,15 +1849,15 @@ def __init__(self, c: ManimConfig) -> None:
18421849
self.__dict__["_c"] = c
18431850

18441851
# there are required by parent class Mapping to behave like a dict
1845-
def __getitem__(self, key: str | int) -> Any:
1852+
def __getitem__(self, key: str) -> Any:
18461853
if key in self._OPTS:
18471854
return self._c[key]
18481855
elif key in self._CONSTANTS:
18491856
return self._CONSTANTS[key]
18501857
else:
18511858
raise KeyError(key)
18521859

1853-
def __iter__(self) -> Iterable[str]:
1860+
def __iter__(self) -> Iterator[Any]:
18541861
return iter(list(self._OPTS) + list(self._CONSTANTS))
18551862

18561863
def __len__(self) -> int:

0 commit comments

Comments
 (0)