File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -44,7 +44,8 @@ def parse_version(fpath):
4444
4545def static_parse (varname , fpath ):
4646 """
47- Statically parse the a constant variable from a python file
47+ Statically parse the a constant variable from a python file.
48+ Raise an error if the variable is not a constant.
4849 """
4950 import ast
5051
@@ -55,10 +56,13 @@ def static_parse(varname, fpath):
5556 pt = ast .parse (sourcecode )
5657
5758 class StaticVisitor (ast .NodeVisitor ):
58- def visit_Assign (self , node ):
59+ def visit_Assign (self , node : ast . Assign ):
5960 for target in node .targets :
6061 if getattr (target , "id" , None ) == varname :
61- self .static_value = node .value .s
62+ value : ast .expr = node .value
63+ if not isinstance (value , ast .Constant ):
64+ raise ValueError ("variable {!r} is not a constant" .format (varname ))
65+ self .static_value = value .value
6266
6367 visitor = StaticVisitor ()
6468 visitor .visit (pt )
You can’t perform that action at this time.
0 commit comments