2626classified by category in different `AliasCategoryDict` objects.
2727"""
2828
29+ ModuleTypeVarDict : TypeAlias = dict [str , str ]
30+ """Dictionary containing every :class:`TypeVar` defined in a module."""
31+
32+
2933AliasDocsDict : TypeAlias = dict [str , ModuleLevelAliasDict ]
3034"""Dictionary which, for every module in Manim, contains documentation
3135about their module-level attributes which are explicitly defined as
3943explicitly defined as :class:`TypeAlias`.
4044"""
4145
46+ TypeVarDict : TypeAlias = dict [str , ModuleTypeVarDict ]
47+ """A dictionary mapping module names to dictionaries of :class:`TypeVar` objects."""
48+
4249ALIAS_DOCS_DICT : AliasDocsDict = {}
4350DATA_DICT : DataDict = {}
51+ TYPEVAR_DICT : TypeVarDict = {}
4452
4553MANIM_ROOT = Path (__file__ ).resolve ().parent .parent .parent
4654
5058# ruff: noqa: E721
5159
5260
53- def parse_module_attributes () -> tuple [AliasDocsDict , DataDict ]:
61+ def parse_module_attributes () -> tuple [AliasDocsDict , DataDict , TypeVarDict ]:
5462 """Read all files, generate Abstract Syntax Trees from them, and
5563 extract useful information about the type aliases defined in the
5664 files: the category they belong to, their definition and their
5765 description, separating them from the "regular" module attributes.
5866
5967 Returns
6068 -------
61- ALIAS_DOCS_DICT : `AliasDocsDict`
69+ ALIAS_DOCS_DICT : :class: `AliasDocsDict`
6270 A dictionary containing the information from all the type
63- aliases in Manim. See `AliasDocsDict` for more information.
71+ aliases in Manim. See :class: `AliasDocsDict` for more information.
6472
65- DATA_DICT : `DataDict`
73+ DATA_DICT : :class: `DataDict`
6674 A dictionary containing the names of all DOCUMENTED
6775 module-level attributes which are not a :class:`TypeAlias`.
76+
77+ TYPEVAR_DICT : :class:`TypeVarDict`
78+ A dictionary containing the definitions of :class:`TypeVar` objects,
79+ organized by modules.
6880 """
6981 global ALIAS_DOCS_DICT
7082 global DATA_DICT
83+ global TYPEVAR_DICT
7184
72- if ALIAS_DOCS_DICT or DATA_DICT :
73- return ALIAS_DOCS_DICT , DATA_DICT
85+ if ALIAS_DOCS_DICT or DATA_DICT or TYPEVAR_DICT :
86+ return ALIAS_DOCS_DICT , DATA_DICT , TYPEVAR_DICT
7487
7588 for module_path in MANIM_ROOT .rglob ("*.py" ):
7689 module_name = module_path .resolve ().relative_to (MANIM_ROOT )
@@ -85,6 +98,9 @@ def parse_module_attributes() -> tuple[AliasDocsDict, DataDict]:
8598 category_dict : AliasCategoryDict | None = None
8699 alias_info : AliasInfo | None = None
87100
101+ # For storing TypeVars
102+ module_typevars : ModuleTypeVarDict = {}
103+
88104 # For storing regular module attributes
89105 data_list : list [str ] = []
90106 data_name : str | None = None
@@ -172,6 +188,19 @@ def parse_module_attributes() -> tuple[AliasDocsDict, DataDict]:
172188 alias_info = category_dict [alias_name ]
173189 continue
174190
191+ # Check if it is a typing.TypeVar
192+ elif (
193+ type (node ) is ast .Assign
194+ and type (node .targets [0 ]) is ast .Name
195+ and type (node .value ) is ast .Call
196+ and type (node .value .func ) is ast .Name
197+ and node .value .func .id .endswith ("TypeVar" )
198+ ):
199+ module_typevars [node .targets [0 ].id ] = ast .unparse (
200+ node .value
201+ ).replace ("_" , r"\_" )
202+ continue
203+
175204 # If here, the node is not a TypeAlias definition
176205 alias_info = None
177206
@@ -185,7 +214,9 @@ def parse_module_attributes() -> tuple[AliasDocsDict, DataDict]:
185214 else :
186215 target = None
187216
188- if type (target ) is ast .Name :
217+ if type (target ) is ast .Name and not (
218+ type (node ) is ast .Assign and target .id not in module_typevars
219+ ):
189220 data_name = target .id
190221 else :
191222 data_name = None
@@ -194,5 +225,7 @@ def parse_module_attributes() -> tuple[AliasDocsDict, DataDict]:
194225 ALIAS_DOCS_DICT [module_name ] = module_dict
195226 if len (data_list ) > 0 :
196227 DATA_DICT [module_name ] = data_list
228+ if module_typevars :
229+ TYPEVAR_DICT [module_name ] = module_typevars
197230
198- return ALIAS_DOCS_DICT , DATA_DICT
231+ return ALIAS_DOCS_DICT , DATA_DICT , TYPEVAR_DICT
0 commit comments