26
26
classified by category in different `AliasCategoryDict` objects.
27
27
"""
28
28
29
+ ModuleTypeVarDict : TypeAlias = dict [str , str ]
30
+ """Dictionary containing every :class:`TypeVar` defined in a module."""
31
+
32
+
29
33
AliasDocsDict : TypeAlias = dict [str , ModuleLevelAliasDict ]
30
34
"""Dictionary which, for every module in Manim, contains documentation
31
35
about their module-level attributes which are explicitly defined as
39
43
explicitly defined as :class:`TypeAlias`.
40
44
"""
41
45
46
+ TypeVarDict : TypeAlias = dict [str , ModuleTypeVarDict ]
47
+ """A dictionary mapping module names to dictionaries of :class:`TypeVar` objects."""
48
+
42
49
ALIAS_DOCS_DICT : AliasDocsDict = {}
43
50
DATA_DICT : DataDict = {}
51
+ TYPEVAR_DICT : TypeVarDict = {}
44
52
45
53
MANIM_ROOT = Path (__file__ ).resolve ().parent .parent .parent
46
54
50
58
# ruff: noqa: E721
51
59
52
60
53
- def parse_module_attributes () -> tuple [AliasDocsDict , DataDict ]:
61
+ def parse_module_attributes () -> tuple [AliasDocsDict , DataDict , TypeVarDict ]:
54
62
"""Read all files, generate Abstract Syntax Trees from them, and
55
63
extract useful information about the type aliases defined in the
56
64
files: the category they belong to, their definition and their
57
65
description, separating them from the "regular" module attributes.
58
66
59
67
Returns
60
68
-------
61
- ALIAS_DOCS_DICT : `AliasDocsDict`
69
+ ALIAS_DOCS_DICT : :class: `AliasDocsDict`
62
70
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.
64
72
65
- DATA_DICT : `DataDict`
73
+ DATA_DICT : :class: `DataDict`
66
74
A dictionary containing the names of all DOCUMENTED
67
75
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.
68
80
"""
69
81
global ALIAS_DOCS_DICT
70
82
global DATA_DICT
83
+ global TYPEVAR_DICT
71
84
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
74
87
75
88
for module_path in MANIM_ROOT .rglob ("*.py" ):
76
89
module_name = module_path .resolve ().relative_to (MANIM_ROOT )
@@ -85,6 +98,9 @@ def parse_module_attributes() -> tuple[AliasDocsDict, DataDict]:
85
98
category_dict : AliasCategoryDict | None = None
86
99
alias_info : AliasInfo | None = None
87
100
101
+ # For storing TypeVars
102
+ module_typevars : ModuleTypeVarDict = {}
103
+
88
104
# For storing regular module attributes
89
105
data_list : list [str ] = []
90
106
data_name : str | None = None
@@ -172,6 +188,19 @@ def parse_module_attributes() -> tuple[AliasDocsDict, DataDict]:
172
188
alias_info = category_dict [alias_name ]
173
189
continue
174
190
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
+
175
204
# If here, the node is not a TypeAlias definition
176
205
alias_info = None
177
206
@@ -185,7 +214,9 @@ def parse_module_attributes() -> tuple[AliasDocsDict, DataDict]:
185
214
else :
186
215
target = None
187
216
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
+ ):
189
220
data_name = target .id
190
221
else :
191
222
data_name = None
@@ -194,5 +225,7 @@ def parse_module_attributes() -> tuple[AliasDocsDict, DataDict]:
194
225
ALIAS_DOCS_DICT [module_name ] = module_dict
195
226
if len (data_list ) > 0 :
196
227
DATA_DICT [module_name ] = data_list
228
+ if module_typevars :
229
+ TYPEVAR_DICT [module_name ] = module_typevars
197
230
198
- return ALIAS_DOCS_DICT , DATA_DICT
231
+ return ALIAS_DOCS_DICT , DATA_DICT , TYPEVAR_DICT
0 commit comments