Skip to content

Commit

Permalink
Add type hints to _config (ManimCommunity#3440)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
2 people authored and smu160 committed Nov 21, 2023
1 parent b9f6c13 commit ea08ceb
Show file tree
Hide file tree
Showing 7 changed files with 624 additions and 457 deletions.
5 changes: 3 additions & 2 deletions manim/_config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
from __future__ import annotations

import logging
from contextlib import _GeneratorContextManager, contextmanager
from contextlib import contextmanager
from typing import Any, Generator

from .cli_colors import parse_cli_ctx
from .logger_utils import make_logger
Expand Down Expand Up @@ -40,7 +41,7 @@

# This has to go here because it needs access to this module's config
@contextmanager
def tempconfig(temp: ManimConfig | dict) -> _GeneratorContextManager:
def tempconfig(temp: ManimConfig | dict[str, Any]) -> Generator[None, None, None]:
"""Context manager that temporarily modifies the global ``config`` object.
Inside the ``with`` statement, the modified config will be used. After
Expand Down
18 changes: 10 additions & 8 deletions manim/_config/cli_colors.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from __future__ import annotations

import configparser

from cloup import Context, HelpFormatter, HelpTheme, Style


def parse_cli_ctx(parser: configparser.ConfigParser) -> Context:
formatter_settings = {
def parse_cli_ctx(parser: configparser.SectionProxy) -> Context:
formatter_settings: dict[str, str | int] = {
"indent_increment": int(parser["indent_increment"]),
"width": int(parser["width"]),
"col1_max_width": int(parser["col1_max_width"]),
Expand All @@ -30,16 +32,16 @@ def parse_cli_ctx(parser: configparser.ConfigParser) -> Context:
formatter = {}
theme = parser["theme"] if parser["theme"] else None
if theme is None:
formatter = HelpFormatter().settings(
theme=HelpTheme(**theme_settings), **formatter_settings
formatter = HelpFormatter.settings(
theme=HelpTheme(**theme_settings), **formatter_settings # type: ignore[arg-type]
)
elif theme.lower() == "dark":
formatter = HelpFormatter().settings(
theme=HelpTheme.dark().with_(**theme_settings), **formatter_settings
formatter = HelpFormatter.settings(
theme=HelpTheme.dark().with_(**theme_settings), **formatter_settings # type: ignore[arg-type]
)
elif theme.lower() == "light":
formatter = HelpFormatter().settings(
theme=HelpTheme.light().with_(**theme_settings), **formatter_settings
formatter = HelpFormatter.settings(
theme=HelpTheme.light().with_(**theme_settings), **formatter_settings # type: ignore[arg-type]
)

return Context.settings(
Expand Down
2 changes: 1 addition & 1 deletion manim/_config/default.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -225,5 +225,5 @@ loglevel = ERROR
ffmpeg_executable = ffmpeg

[jupyter]
media_embed =
media_embed = False
media_width = 60%%
14 changes: 6 additions & 8 deletions manim/_config/logger_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import copy
import json
import logging
import sys
from typing import TYPE_CHECKING

from rich import color, errors
Expand Down Expand Up @@ -50,9 +49,9 @@


def make_logger(
parser: configparser.ConfigParser,
parser: configparser.SectionProxy,
verbosity: str,
) -> tuple[logging.Logger, Console]:
) -> tuple[logging.Logger, Console, Console]:
"""Make the manim logger and console.
Parameters
Expand Down Expand Up @@ -84,14 +83,13 @@ def make_logger(
theme = parse_theme(parser)
console = Console(theme=theme)

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

# set the rich handler
RichHandler.KEYWORDS = HIGHLIGHTED_KEYWORDS
rich_handler = RichHandler(
console=console,
show_time=parser.getboolean("log_timestamps"),
keywords=HIGHLIGHTED_KEYWORDS,
)

# finally, the logger
Expand All @@ -102,7 +100,7 @@ def make_logger(
return logger, console, error_console


def parse_theme(parser: configparser.ConfigParser) -> Theme:
def parse_theme(parser: configparser.SectionProxy) -> Theme:
"""Configure the rich style of logger and console output.
Parameters
Expand Down Expand Up @@ -178,7 +176,7 @@ class JSONFormatter(logging.Formatter):
"""

def format(self, record: dict) -> str:
def format(self, record: logging.LogRecord) -> str:
"""Format the record in a custom JSON format."""
record_c = copy.deepcopy(record)
if record_c.args:
Expand Down
Loading

0 comments on commit ea08ceb

Please sign in to comment.