Merge branch 'main' into guide

This commit is contained in:
Jason Grace 2023-12-28 14:44:23 -05:00 committed by GitHub
commit 9596c7169f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 57 additions and 13 deletions

View file

@ -40,6 +40,9 @@ Typing guidelines
pending shape support, using the correct type aliases will help users understand
which shape should be used.
* For typings of generic collections, check out `this <https://docs.python.org/3/library/collections.abc.html#collections-abstract-base-classes>`_
link.
* Always use a type hint of ``None`` for functions that does not return
a value (this also applies to ``__init__``), e.g.:
@ -57,6 +60,8 @@ Typing guidelines
* Following `PEP 484 <https://peps.python.org/pep-0484/#the-numeric-tower>`_,
use ``float`` instead of ``int | float``.
* Use ``x | y`` instead of ``Union[x, y]``
* Mobjects have the typehint ``Mobject``, e.g.:
.. code:: py
@ -66,16 +71,55 @@ Typing guidelines
return self.set_color(mobject.get_color())
* Always parametrize generics (``list[int]`` instead of ``list``,
``type[Any]`` instead of ``type``, etc.). This also applies to callables:
``type[Any]`` instead of ``type``, etc.). This also applies to callables.
.. code:: py
rate_func: Callable[[float], float] = lambda t: smooth(1 - t)
* Use ``TypeVar`` when you want to "link" type hints as being the same type.
Consider ``Mobject.copy``, which returns a new instance of the same class.
It would be type-hinted as:
.. code:: py
T = TypeVar("T")
def copy(self: T) -> T:
...
* Use ``typing.Iterable`` whenever the function works with *any* iterable, not a specific type.
* If the function returns a container of a specific length each time, consider using ``tuple`` instead of ``list``.
.. code:: py
def foo() -> tuple[float, float, float]:
return (0, 0, 0)
* If a function works with a parameter as long as said parameter has a ``__getitem__``, ``__iter___`` and ``__len__`` method,
the typehint of the parameter should be ``collections.abc.Mapping``. If it also supports ``__setitem__`` and/or ``__delitem__``, it
should be marked as ``collections.abc.MutableMapping``.
* Typehinting something as ``object`` means that only attributes available on every Python object should be accessed,
like ``__str__`` and so on. On the other hand, literally any attribute can be accessed on a variable with the ``Any`` typehint -
it's more freeing than the ``object`` typehint, and makes mypy stop typechecking the variable. Note that whenever possible,
try to keep typehints as specific as possible.
* If objects are imported purely for type hint purposes, keep it under an ``if typing.TYPE_CHECKING`` guard, to prevent them from
being imported at runtime (helps library performance). Do not forget to use the ``from __future__ import annotations`` import to avoid having runtime ``NameError`` exceptions.
.. code:: py
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from manim.typing import Vector3
# type stuff with Vector3
Missing Sections for typehints are:
-----------------------------------
* Mypy and numpy import errors: https://realpython.com/python-type-checking/#running-mypy
* When to use ``object`` vs ``Any``
* The use of a TypeVar on the type hints for ``copy()``.
* The definition and use of Protocols (like ``Sized``, ``Sequence``, ``Iterable``...)
* Explain ``mypy.ini`` (see above link)

View file

@ -84,7 +84,7 @@ class Broadcast(LaggedStart):
mob.move_to(self.focal_point)
mob.save_state()
mob.set_width(self.initial_width)
mob.set(width=self.initial_width)
if fill_o:
mob.set_opacity(self.initial_opacity)

View file

@ -225,7 +225,7 @@ class TransformMatchingShapes(TransformMatchingAbstractBase):
def get_mobject_key(mobject: Mobject) -> int:
mobject.save_state()
mobject.center()
mobject.set_height(1)
mobject.set(height=1)
result = hash(np.round(mobject.points, 3).tobytes())
mobject.restore()
return result

View file

@ -305,7 +305,7 @@ class CoordinateSystem:
label
The label. Defaults to :class:`~.MathTex` for ``str`` and ``float`` inputs.
edge
The edge of the x-axis to which the label will be added, by default ``UR``.
The edge of the y-axis to which the label will be added, by default ``UR``.
direction
Allows for further positioning of the label from an edge, by default ``UR``
buff

View file

@ -475,7 +475,7 @@ class NumberLine(Line):
num_mob.next_to(self.number_to_point(x), direction=direction, buff=buff)
if x < 0 and self.label_direction[0] == 0:
# Align without the minus sign
num_mob.shift(num_mob[0].get_width() * LEFT / 2)
num_mob.shift(num_mob[0].width * LEFT / 2)
return num_mob
def get_number_mobjects(self, *numbers, **kwargs) -> VGroup:

View file

@ -441,9 +441,9 @@ class SVGMobject(VMobject, metaclass=ConvertToOpenGL):
if self.should_center:
self.center()
if self.svg_height is not None:
self.set_height(self.svg_height)
self.set(height=self.svg_height)
if self.svg_width is not None:
self.set_width(self.svg_width)
self.set(width=self.svg_width)
class VMobjectFromSVGPath(VMobject, metaclass=ConvertToOpenGL):

View file

@ -352,7 +352,7 @@ class Text(SVGMobject):
)
text6.scale(1.3).shift(DOWN)
self.add(text1, text2, text3, text4, text5 , text6)
Group(*self.mobjects).arrange(DOWN, buff=.8).set_height(config.frame_height-LARGE_BUFF)
Group(*self.mobjects).arrange(DOWN, buff=.8).set(height=config.frame_height-LARGE_BUFF)
.. manim:: TextMoreCustomization
:save_last_frame:

View file

@ -71,7 +71,7 @@ class ValueTracker(Mobject, metaclass=ConvertToOpenGL):
def __init__(self, value=0, **kwargs):
super().__init__(**kwargs)
self.set_points(np.zeros((1, 3)))
self.set(points=np.zeros((1, 3)))
self.set_value(value)
def get_value(self) -> float:
@ -132,7 +132,7 @@ class ValueTracker(Mobject, metaclass=ConvertToOpenGL):
Turns self into an interpolation between mobject1
and mobject2.
"""
self.set_points(path_func(mobject1.points, mobject2.points, alpha))
self.set(points=path_func(mobject1.points, mobject2.points, alpha))
return self