diff --git a/setup.py b/setup.py index deaf58f..cec9839 100755 --- a/setup.py +++ b/setup.py @@ -44,7 +44,8 @@ def parse_version(fpath): def static_parse(varname, fpath): """ - Statically parse the a constant variable from a python file + Statically parse the a constant variable from a python file. + Raise an error if the variable is not a constant. """ import ast @@ -55,10 +56,13 @@ def static_parse(varname, fpath): pt = ast.parse(sourcecode) class StaticVisitor(ast.NodeVisitor): - def visit_Assign(self, node): + def visit_Assign(self, node: ast.Assign): for target in node.targets: if getattr(target, "id", None) == varname: - self.static_value = node.value.s + value: ast.expr = node.value + if not isinstance(value, ast.Constant): + raise ValueError("variable {!r} is not a constant".format(varname)) + self.static_value = value.value visitor = StaticVisitor() visitor.visit(pt)