Skip to content
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

Fix Typing #3086

Merged
merged 101 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
101 commits
Select commit Hold shift + click to select a range
bd4d123
first draft of color class + starting library conversion
MrDiver Nov 13, 2022
01e28fd
Merge branch 'main' into color_fix
MrDiver Nov 13, 2022
a0e85af
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 13, 2022
8af3f81
Merge branch 'main' into color_fix
MrDiver Nov 19, 2022
332d327
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 19, 2022
da0806a
changed everything to Manim color todo: figure out circular dependenc…
MrDiver Nov 19, 2022
73ffb51
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 19, 2022
157c504
first working draft of new color version
MrDiver Dec 10, 2022
a7e4a3e
Merge branch 'main' into color_fix
MrDiver Dec 10, 2022
d21583d
resolving conflicts
MrDiver Dec 11, 2022
074345e
resolving conflicts
MrDiver Dec 11, 2022
128b8ab
resolving conflicts
MrDiver Dec 11, 2022
31bcdac
resolving conflicts
MrDiver Dec 11, 2022
27c0ddb
resolving conflicts
MrDiver Dec 11, 2022
195512a
changed default internal value of ManimColor to np.ndarray[float]
MrDiver Dec 16, 2022
9823616
starting to fix tests
MrDiver Dec 16, 2022
e638643
fixed more tests and changed precision of manim color
MrDiver Dec 17, 2022
7867aa3
removed premature color conversion
MrDiver Dec 17, 2022
a12507a
fixed some more tests
MrDiver Dec 17, 2022
15c4998
final test changes
MrDiver Dec 17, 2022
76d7cda
fix doctests
MrDiver Dec 17, 2022
a27aff4
fix for 3.8
MrDiver Dec 17, 2022
1580c82
fixing ManimColor string representation
MrDiver Dec 17, 2022
b2cc282
removing some unneccesary conversions
MrDiver Dec 17, 2022
2baeff5
moved community constants to manim_colors.py and added more color sta…
MrDiver Dec 22, 2022
86c5d07
Merge branch 'main' into color_fix
MrDiver Dec 22, 2022
b44a3e9
Merge branch 'color_fix' of github.com:MrDiver/manim into 52-fix-typing
MrDiver Dec 24, 2022
cd7a385
Added typing.py and typed bezier.py, core.py, constants.py fully
MrDiver Dec 25, 2022
47e16b6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 25, 2022
3193597
fixed codeql complaints
MrDiver Dec 25, 2022
bd3bf0c
add type ignore for np.allclose
MrDiver Dec 25, 2022
8c1b871
fixed import in three_dimensions
MrDiver Dec 25, 2022
2433c1d
added ignore for F401 back again in flake
MrDiver Dec 25, 2022
d32b267
added typings to coordinate_systems.py
alembcke Jan 7, 2023
0356591
Few improvements to `graphing/coordinate_systems.py`
Viicos Jan 20, 2023
b08e4a6
added some typings to mobject/geometry/line.py
alembcke Jan 20, 2023
ee865ff
updated typings for mobject/geometry/line.py
alembcke Jan 21, 2023
9eb6ed3
Add missing imports to `line.py`
Viicos Jan 21, 2023
561ef52
added typings to three_dimensions.py
alembcke Feb 6, 2023
bf2bcbf
Use `FunctionOverride` for animation overrides
Viicos Feb 18, 2023
1f56200
Remove `TYPE_CHECKING` check
Viicos Feb 18, 2023
802ab71
Revert "Remove `TYPE_CHECKING` check"
Viicos Feb 18, 2023
b11ffe2
Use `Self` in `coordinate_systems.py`
Viicos Feb 18, 2023
a41900c
Typehinted mobject.py and updated manim.typing.py
JasonGrace2282 Aug 6, 2023
c0f121c
Typed VMobject
JasonGrace2282 Aug 10, 2023
88f9b2d
Type-hinted manim.mobject.geometry
JasonGrace2282 Aug 10, 2023
17ce647
math.cos->np.cos, etc & fixed incorrect typehints
JasonGrace2282 Aug 10, 2023
747167b
Merge branch 'main' of github.com:ManimCommunity/manim into 52-fix-ty…
MrDiver Aug 11, 2023
2a71462
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 11, 2023
8a149ca
fix missing annotations import
MrDiver Aug 11, 2023
28d9b24
Merge branch '52-fix-typing' of github.com:MrDiver/manim into 52-fix-…
MrDiver Aug 11, 2023
6987498
TypeAlias fix in typing.py
MrDiver Aug 11, 2023
9026015
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 11, 2023
51922d1
Add ignore errors again to mypy because commits are not possible like…
MrDiver Aug 11, 2023
e8ab923
Merge main
Viicos Sep 22, 2023
106774d
Fix last typing issues
Viicos Sep 22, 2023
ca3154c
Update docs
Viicos Sep 22, 2023
920d15c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 22, 2023
c6183e8
Only type check manim
Viicos Sep 22, 2023
ad7043f
Try fixing pre-commit
Viicos Sep 22, 2023
1da1b99
fix merge
Viicos Sep 22, 2023
8522b59
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 22, 2023
6f95fd3
Fix compat
Viicos Sep 22, 2023
c2d7d42
Fix compat again
Viicos Sep 22, 2023
3a3d501
Fix imports compat
Viicos Sep 22, 2023
2189d01
Use union syntax
Viicos Sep 22, 2023
e62ade2
Use union syntax
Viicos Sep 22, 2023
4497734
Fix reduce_across_dimension
Viicos Sep 22, 2023
aadb22c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 22, 2023
0852403
Various test and merge fixes
Viicos Sep 22, 2023
b9fbf54
Doc fixes
Viicos Sep 22, 2023
0f108c0
Last doc fix
Viicos Sep 22, 2023
5160a1f
Revert usage of np over math
Viicos Sep 23, 2023
388b6d1
Bump numpy version
Viicos Sep 23, 2023
287c3ad
Remove obsolete duplicate example
Viicos Sep 23, 2023
0c0f875
Fixed Incorrect Typehint in manim.constants
JasonGrace2282 Sep 23, 2023
b87b594
Fix docstring typo
Viicos Sep 24, 2023
1c139af
More fixes
Viicos Sep 25, 2023
ffaa031
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 25, 2023
56501d7
docs fixes
Viicos Sep 25, 2023
921f450
Add internal aliases
Viicos Sep 25, 2023
4e3fe30
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 25, 2023
5663791
fix compat
Viicos Sep 25, 2023
f92ebd6
Merge remote-tracking branch 'origin/main' into 52-fix-typing
behackl Oct 27, 2023
feca683
line lengths in .rst file, formatting, typos
behackl Oct 28, 2023
2bcd8ac
add docstring for space_ops:cross2d
behackl Oct 28, 2023
5514ac4
add some more arrow tip typings (in a non-circular import causing way)
behackl Oct 28, 2023
b259a04
yes, this can be deleted
behackl Oct 28, 2023
e390082
fix formatting of example
behackl Oct 28, 2023
737189c
added docstring to bezier::inverse_interpolation
behackl Oct 28, 2023
3c4051f
added docstring + test for bezier::match_interpolate
behackl Oct 28, 2023
c7659d3
some improvements in coordinate_systems
behackl Oct 28, 2023
351353a
Vector -> Vector3
behackl Oct 28, 2023
3616c50
replaced np.ndarray with more appropriate type hints
behackl Oct 28, 2023
672b0e3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 28, 2023
84c9cdc
Apply feedback
Viicos Oct 29, 2023
4fed4af
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 29, 2023
ad228f1
revert to previous (new) version
behackl Nov 2, 2023
c2b48f8
fix doctest
behackl Nov 3, 2023
948214e
fix ReST errors
behackl Nov 3, 2023
33d9e4a
Merge remote-tracking branch 'origin/main' into 52-fix-typing
behackl Nov 3, 2023
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
7 changes: 4 additions & 3 deletions .flake8
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[flake8]
# Exclude the grpc generated code
exclude = ./manim/grpc/gen/*
exclude = ./manim/grpc/gen/*, __pycache__,.git,
per-file-ignores = __init__.py:F401
max-complexity = 15
max-line-length = 88
statistics = True
Expand All @@ -9,7 +10,7 @@ rst-roles = attr,class,func,meth,mod,obj,ref,doc,exc
rst-directives = manim, SEEALSO, seealso
docstring-convention=numpy

select = A,A00,B,B9,C4,C90,D,E,F,F,PT,RST,SIM,W
select = A,A00,B,B9,C4,C90,D,E,F,F,PT,RST,SIM,W,F401

# General Compatibility
extend-ignore = E203, W503, D202, D212, D213, D404
Expand Down Expand Up @@ -40,4 +41,4 @@ extend-ignore = E203, W503, D202, D212, D213, D404

# Plug-in: flake8-rst-docstrings
RST201, RST203, RST210, RST212, RST213, RST215,
RST301, RST303,
RST301, RST303, RST499
3 changes: 2 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ repos:
flake8-simplify==0.14.1,
]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.4.1
rev: v1.5.1
hooks:
- id: mypy
additional_dependencies:
Expand All @@ -69,6 +69,7 @@ repos:
types-requests,
types-setuptools,
]
files: ^manim/

- repo: https://github.com/codespell-project/codespell
rev: v2.2.5
Expand Down
110 changes: 44 additions & 66 deletions docs/source/contributing/typings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,102 +2,80 @@
Adding Typings
==============

.. warning::
This section is still a work in progress.

Adding type hints to functions and parameters
---------------------------------------------

.. warning::
This section is still a work in progress.
Manim is currently in the process of adding type hints into the library. In this
section, you will find information about the standards used and some general
guidelines.

If you've never used type hints before, this is a good place to get started:
https://realpython.com/python-type-checking/#hello-types.

When adding type hints to manim, there are some guidelines that should be followed:
Typing standards
~~~~~~~~~~~~~~~~

* Coordinates have the typehint ``Sequence[float]``, e.g.
Manim uses `mypy`_ to type check its codebase. You will find a list of
configuration values in the ``mypy.ini`` configuration file.

.. code:: py
To be able to use the newest typing features not available in the lowest
supported Python version, make use of `typing_extensions`_.

def set_points_as_corners(self, points: Sequence[float]) -> "VMobject":
"""Given an array of points, set them as corner of the Vmobject."""
To be able to use the new Union syntax (``|``) and builtins subscripting, use
the ``from __future__ import annotations`` import.

* ``**kwargs`` has no typehint
.. _mypy: https://mypy-lang.org/
.. _typing_extensions: https://pypi.org/project/typing-extensions/

* Mobjects have the typehint "Mobject", e.g.
Typing guidelines
~~~~~~~~~~~~~~~~~

.. code:: py
* Manim has a dedicated :mod:`~.typing` module where type aliases are provided.
Most of them may seem redundant, in particular the ones related to ``numpy``.
This is in anticipation of the support for shape type hinting
(`related issue <https://github.com/numpy/numpy/issues/16544>`_). Besides the
pending shape support, using the correct type aliases will help users understand
which shape should be used.

def match_color(self, mobject: "Mobject"):
"""Match the color with the color of another :class:`~.Mobject`."""
return self.set_color(mobject.get_color())

* Colors have the typehint ``Color``, e.g.

.. code:: py

def set_color(self, color: Color = YELLOW_C, family: bool = True):
"""Condition is function which takes in one arguments, (x, y, z)."""

* As ``float`` and ``Union[int, float]`` are the same, use only ``float``

* For numpy arrays use the typehint ``np.ndarray``

* Functions that does not return a value should get the type hint ``None``. (This annotations help catch the kinds of subtle bugs where you are trying to use a meaningless return value. )
* Always use a type hint of ``None`` for functions that does not return
a value (this also applies to ``__init__``), e.g.:

.. code:: py

def height(self, value) -> None:
self.scale_to_fit_height(value)

* Parameters that are None by default should get the type hint ``Optional``

.. code:: py
* For variables representing paths, use the ``StrPath`` or ``StrOrBytesPath``
type alias defined in the :mod:`~.typing` module.

def rotate(
self,
angle,
axis=OUT,
about_point: Optional[Sequence[float]] = None,
**kwargs,
):
pass
* ``*args`` and ``**kwargs`` shouldn't be left untyped (in most cases you can
use ``Any``).

* Following `PEP 484 <https://peps.python.org/pep-0484/#the-numeric-tower>`_,
use ``float`` instead of ``int | float``.

* The ``__init__()`` method always should have None as its return type.

* Functions and lambda functions should get the typehint ``Callable``
* Mobjects have the typehint ``Mobject``, e.g.:

.. code:: py

rate_func: Callable[[float], float] = lambda t: smooth(1 - t)


* Assuming that typical path objects are either Paths or strs, one can use the typehint ``typing.Union[str, pathlib.Path]``
def match_color(self, mobject: "Mobject"):
"""Match the color with the color of another :class:`~.Mobject`."""
return self.set_color(mobject.get_color())

.. note::
As a helper for tool for typesets, you can use `typestring-parser
<https://github.com/Dominik1123/typestring-parser>`_
which can be accessed by first installing it via ``pip`` - ``pip install typestring-parser`` and
then using ``from typestring_parser import parse``.
* Always parametrize generics (``list[int]`` instead of ``list``,
``type[Any]`` instead of ``type``, etc.). This also applies to callables:

.. doctest::
:options: +SKIP
.. code:: py

>>> from typestring_parser import parse
>>> parse("int")
<class 'int'>
>>> parse("int or str")
typing.Union[int, str]
>>> parse("list of str or str")
typing.Union[typing.List[str], str]
>>> parse("list of (int, str)")
typing.List[typing.Tuple[int, str]]
rate_func: Callable[[float], float] = lambda t: smooth(1 - t)

Missing Sections for typehints are:
-----------------------------------
* Tools for typehinting
* Link to MyPy

* Mypy and numpy import errors: https://realpython.com/python-type-checking/#running-mypy
* Where to find the alias
* When to use Object and when to use "Object".
* The use of a TypeVar on the type hints for copy().
* The definition and use of Protocols (like Sized, or Sequence, or Iterable...)
* 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``...)
1 change: 0 additions & 1 deletion manim/_config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
]

parser = make_config_parser()
logger: logging.Logger

# The logger can be accessed from anywhere as manim.logger, or as
# logging.getLogger("manim"). The console must be accessed as manim.console.
Expand Down
1 change: 1 addition & 0 deletions manim/_config/logger_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

if TYPE_CHECKING:
from pathlib import Path

HIGHLIGHTED_KEYWORDS = [ # these keywords are highlighted specially
"Played",
"animations",
Expand Down
4 changes: 2 additions & 2 deletions manim/_config/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@

from .. import constants
from ..constants import RendererType
from ..typing import StrPath
from ..utils.color import ManimColor
from ..utils.tex import TexTemplate, TexTemplateFromFile
from ..utils.tex_templates import TexTemplateLibrary


def config_file_paths() -> list[Path]:
Expand Down Expand Up @@ -76,7 +76,7 @@ def config_file_paths() -> list[Path]:


def make_config_parser(
custom_file: str | os.PathLike | None = None,
custom_file: StrPath | None = None,
) -> configparser.ConfigParser:
"""Make a :class:`ConfigParser` object and load any ``.cfg`` files.

Expand Down
1 change: 0 additions & 1 deletion manim/animation/changing.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

from typing import Callable

from manim._config import config
from manim.mobject.opengl.opengl_compatibility import ConvertToOpenGL
from manim.mobject.types.vectorized_mobject import VGroup, VMobject
from manim.utils.color import (
Expand Down
2 changes: 1 addition & 1 deletion manim/animation/creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def construct(self):
from ..mobject.mobject import Group, Mobject
from ..mobject.types.vectorized_mobject import VMobject
from ..utils.bezier import integer_interpolate
from ..utils.rate_functions import double_smooth, linear, smooth
from ..utils.rate_functions import double_smooth, linear


class ShowPartial(Animation):
Expand Down
3 changes: 2 additions & 1 deletion manim/animation/indication.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def construct(self):
from manim.mobject.geometry.line import Line
from manim.mobject.geometry.polygram import Rectangle
from manim.mobject.geometry.shape_matchers import SurroundingRectangle
from manim.scene.scene import Scene

from .. import config
from ..animation.animation import Animation
Expand Down Expand Up @@ -313,7 +314,7 @@ def _get_bounds(self, alpha: float) -> Tuple[float]:
lower = max(lower, 0)
return (lower, upper)

def clean_up_from_scene(self, scene: "Scene") -> None:
def clean_up_from_scene(self, scene: Scene) -> None:
super().clean_up_from_scene(scene)
for submob, start in self.get_all_families_zipped():
submob.pointwise_become_partial(start, 0, 1)
Expand Down
1 change: 0 additions & 1 deletion manim/camera/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import itertools as it
import operator as op
import pathlib
import time
from functools import reduce
from typing import Any, Callable, Iterable

Expand Down
Loading