Skip to content

Handling typing errors in text/numbers.py #4317

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 28 additions & 23 deletions manim/mobject/text/numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

__all__ = ["DecimalNumber", "Integer", "Variable"]

from collections.abc import Sequence
from typing import Any

import numpy as np
from typing_extensions import Self

from manim import config
from manim.constants import *
Expand All @@ -16,10 +16,9 @@
from manim.mobject.text.text_mobject import Text
from manim.mobject.types.vectorized_mobject import VMobject
from manim.mobject.value_tracker import ValueTracker
from manim.typing import Point3DLike

string_to_mob_map = {}

__all__ = ["DecimalNumber", "Integer", "Variable"]
string_to_mob_map: dict[str, VMobject] = {}


class DecimalNumber(VMobject, metaclass=ConvertToOpenGL):
Expand Down Expand Up @@ -86,19 +85,19 @@ def __init__(
self,
number: float = 0,
num_decimal_places: int = 2,
mob_class: VMobject = MathTex,
mob_class: type[SingleStringMathTex] = MathTex,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this type too specific?

include_sign: bool = False,
group_with_commas: bool = True,
digit_buff_per_font_unit: float = 0.001,
show_ellipsis: bool = False,
unit: str | None = None, # Aligned to bottom unless it starts with "^"
unit_buff_per_font_unit: float = 0,
include_background_rectangle: bool = False,
edge_to_fix: Sequence[float] = LEFT,
edge_to_fix: Point3DLike = LEFT,
font_size: float = DEFAULT_FONT_SIZE,
stroke_width: float = 0,
fill_opacity: float = 1.0,
**kwargs,
**kwargs: Any,
):
super().__init__(**kwargs, fill_opacity=fill_opacity, stroke_width=stroke_width)
self.number = number
Expand Down Expand Up @@ -137,12 +136,13 @@ def __init__(
self.init_colors()

@property
def font_size(self):
def font_size(self) -> float:
"""The font size of the tex mobject."""
return self.height / self.initial_height * self._font_size
return_value: float = self.height / self.initial_height * self._font_size
return return_value

@font_size.setter
def font_size(self, font_val):
def font_size(self, font_val: float) -> None:
if font_val <= 0:
raise ValueError("font_size must be greater than 0.")
elif self.height > 0:
Expand All @@ -153,7 +153,7 @@ def font_size(self, font_val):
# font_size does not depend on current size.
self.scale(font_val / self.font_size)

def _set_submobjects_from_number(self, number):
def _set_submobjects_from_number(self, number: float) -> None:
self.number = number
self.submobjects = []

Expand Down Expand Up @@ -197,12 +197,12 @@ def _set_submobjects_from_number(self, number):
self.unit_sign.align_to(self, UP)

# track the initial height to enable scaling via font_size
self.initial_height = self.height
self.initial_height: float = self.height

if self.include_background_rectangle:
self.add_background_rectangle()

def _get_num_string(self, number):
def _get_num_string(self, number: float | complex) -> str:
if isinstance(number, complex):
formatter = self._get_complex_formatter()
else:
Expand All @@ -215,17 +215,22 @@ def _get_num_string(self, number):

return num_string

def _string_to_mob(self, string: str, mob_class: VMobject | None = None, **kwargs):
def _string_to_mob(
self,
string: str,
mob_class: type[SingleStringMathTex] | None = None,
**kwargs: Any,
) -> VMobject:
if mob_class is None:
mob_class = self.mob_class

if string not in string_to_mob_map:
string_to_mob_map[string] = mob_class(string, **kwargs)
mob = string_to_mob_map[string].copy()
mob.font_size = self._font_size
mob.font_size = self._font_size # type: ignore[attr-defined]
return mob

def _get_formatter(self, **kwargs):
def _get_formatter(self, **kwargs: Any) -> str:
"""
Configuration is based first off instance attributes,
but overwritten by any kew word argument. Relevant
Expand Down Expand Up @@ -258,7 +263,7 @@ def _get_formatter(self, **kwargs):
],
)

def _get_complex_formatter(self):
def _get_complex_formatter(self) -> str:
return "".join(
[
self._get_formatter(field_name="0.real"),
Expand All @@ -267,7 +272,7 @@ def _get_complex_formatter(self):
],
)

def set_value(self, number: float):
def set_value(self, number: float) -> Self:
"""Set the value of the :class:`~.DecimalNumber` to a new number.

Parameters
Expand Down Expand Up @@ -304,10 +309,10 @@ def set_value(self, number: float):
self.init_colors()
return self

def get_value(self):
def get_value(self) -> float:
return self.number

def increment_value(self, delta_t=1):
def increment_value(self, delta_t: float = 1) -> None:
self.set_value(self.get_value() + delta_t)


Expand All @@ -333,7 +338,7 @@ def __init__(
) -> None:
super().__init__(number=number, num_decimal_places=num_decimal_places, **kwargs)

def get_value(self):
def get_value(self) -> int:
return int(np.round(super().get_value()))


Expand Down Expand Up @@ -444,9 +449,9 @@ def __init__(
self,
var: float,
label: str | Tex | MathTex | Text | SingleStringMathTex,
var_type: DecimalNumber | Integer = DecimalNumber,
var_type: type[DecimalNumber | Integer] = DecimalNumber,
num_decimal_places: int = 2,
**kwargs,
**kwargs: Any,
):
self.label = MathTex(label) if isinstance(label, str) else label
equals = MathTex("=").next_to(self.label, RIGHT)
Expand Down
6 changes: 3 additions & 3 deletions manim/mobject/text/tex_mobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def __init__(
tex_template: TexTemplate | None = None,
font_size: float = DEFAULT_FONT_SIZE,
color: ParsableManimColor | None = None,
**kwargs,
**kwargs: Any,
):
if color is None:
color = VMobject().color
Expand Down Expand Up @@ -110,12 +110,12 @@ def __repr__(self):
return f"{type(self).__name__}({repr(self.tex_string)})"

@property
def font_size(self):
def font_size(self) -> float:
"""The font size of the tex mobject."""
return self.height / self.initial_height / SCALE_FACTOR_PER_FONT_POINT

@font_size.setter
def font_size(self, font_val):
def font_size(self, font_val: float):
if font_val <= 0:
raise ValueError("font_size must be greater than 0.")
elif self.height > 0:
Expand Down
4 changes: 3 additions & 1 deletion manim/mobject/value_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
__all__ = ["ValueTracker", "ComplexValueTracker"]


from typing import Any

import numpy as np

from manim.mobject.mobject import Mobject
Expand Down Expand Up @@ -69,7 +71,7 @@ def construct(self):

"""

def __init__(self, value=0, **kwargs):
def __init__(self, value: float = 0, **kwargs: Any):
super().__init__(**kwargs)
self.set(points=np.zeros((1, 3)))
self.set_value(value)
Expand Down
3 changes: 0 additions & 3 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,6 @@ ignore_errors = True
[mypy-manim.mobject.table]
ignore_errors = True

[mypy-manim.mobject.text.numbers]
ignore_errors = True

[mypy-manim.mobject.text.tex_mobject]
ignore_errors = True

Expand Down