Skip to content

Commit 212bca0

Browse files
Add type hints to _config (#3440)
* Add type hints to `_config` * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix call issues * Fix wrong value being used * Fix test * Fix wrong value being set * lint * Few type fixes --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 94711f7 commit 212bca0

File tree

7 files changed

+624
-457
lines changed

7 files changed

+624
-457
lines changed

manim/_config/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
from __future__ import annotations
44

55
import logging
6-
from contextlib import _GeneratorContextManager, contextmanager
6+
from contextlib import contextmanager
7+
from typing import Any, Generator
78

89
from .cli_colors import parse_cli_ctx
910
from .logger_utils import make_logger
@@ -40,7 +41,7 @@
4041

4142
# This has to go here because it needs access to this module's config
4243
@contextmanager
43-
def tempconfig(temp: ManimConfig | dict) -> _GeneratorContextManager:
44+
def tempconfig(temp: ManimConfig | dict[str, Any]) -> Generator[None, None, None]:
4445
"""Context manager that temporarily modifies the global ``config`` object.
4546
4647
Inside the ``with`` statement, the modified config will be used. After

manim/_config/cli_colors.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
from __future__ import annotations
2+
13
import configparser
24

35
from cloup import Context, HelpFormatter, HelpTheme, Style
46

57

6-
def parse_cli_ctx(parser: configparser.ConfigParser) -> Context:
7-
formatter_settings = {
8+
def parse_cli_ctx(parser: configparser.SectionProxy) -> Context:
9+
formatter_settings: dict[str, str | int] = {
810
"indent_increment": int(parser["indent_increment"]),
911
"width": int(parser["width"]),
1012
"col1_max_width": int(parser["col1_max_width"]),
@@ -30,16 +32,16 @@ def parse_cli_ctx(parser: configparser.ConfigParser) -> Context:
3032
formatter = {}
3133
theme = parser["theme"] if parser["theme"] else None
3234
if theme is None:
33-
formatter = HelpFormatter().settings(
34-
theme=HelpTheme(**theme_settings), **formatter_settings
35+
formatter = HelpFormatter.settings(
36+
theme=HelpTheme(**theme_settings), **formatter_settings # type: ignore[arg-type]
3537
)
3638
elif theme.lower() == "dark":
37-
formatter = HelpFormatter().settings(
38-
theme=HelpTheme.dark().with_(**theme_settings), **formatter_settings
39+
formatter = HelpFormatter.settings(
40+
theme=HelpTheme.dark().with_(**theme_settings), **formatter_settings # type: ignore[arg-type]
3941
)
4042
elif theme.lower() == "light":
41-
formatter = HelpFormatter().settings(
42-
theme=HelpTheme.light().with_(**theme_settings), **formatter_settings
43+
formatter = HelpFormatter.settings(
44+
theme=HelpTheme.light().with_(**theme_settings), **formatter_settings # type: ignore[arg-type]
4345
)
4446

4547
return Context.settings(

manim/_config/default.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,5 +225,5 @@ loglevel = ERROR
225225
ffmpeg_executable = ffmpeg
226226

227227
[jupyter]
228-
media_embed =
228+
media_embed = False
229229
media_width = 60%%

manim/_config/logger_utils.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import copy
1616
import json
1717
import logging
18-
import sys
1918
from typing import TYPE_CHECKING
2019

2120
from rich import color, errors
@@ -50,9 +49,9 @@
5049

5150

5251
def make_logger(
53-
parser: configparser.ConfigParser,
52+
parser: configparser.SectionProxy,
5453
verbosity: str,
55-
) -> tuple[logging.Logger, Console]:
54+
) -> tuple[logging.Logger, Console, Console]:
5655
"""Make the manim logger and console.
5756
5857
Parameters
@@ -84,14 +83,13 @@ def make_logger(
8483
theme = parse_theme(parser)
8584
console = Console(theme=theme)
8685

87-
# With rich 9.5.0+ we could pass stderr=True instead
88-
error_console = Console(theme=theme, file=sys.stderr)
86+
error_console = Console(theme=theme, stderr=True)
8987

9088
# set the rich handler
91-
RichHandler.KEYWORDS = HIGHLIGHTED_KEYWORDS
9289
rich_handler = RichHandler(
9390
console=console,
9491
show_time=parser.getboolean("log_timestamps"),
92+
keywords=HIGHLIGHTED_KEYWORDS,
9593
)
9694

9795
# finally, the logger
@@ -102,7 +100,7 @@ def make_logger(
102100
return logger, console, error_console
103101

104102

105-
def parse_theme(parser: configparser.ConfigParser) -> Theme:
103+
def parse_theme(parser: configparser.SectionProxy) -> Theme:
106104
"""Configure the rich style of logger and console output.
107105
108106
Parameters
@@ -178,7 +176,7 @@ class JSONFormatter(logging.Formatter):
178176
179177
"""
180178

181-
def format(self, record: dict) -> str:
179+
def format(self, record: logging.LogRecord) -> str:
182180
"""Format the record in a custom JSON format."""
183181
record_c = copy.deepcopy(record)
184182
if record_c.args:

0 commit comments

Comments
 (0)