Related to #1724
Currently setup.cfg provide an attr: directive to extract a value from a file, which is really nice typically to single-source version and description like:
version = attr: the_package.__version__
description = attr: the_package.__doc__
long_description = file: README.md
long_description_content_type = text/markdown; charset=UTF-8
But the version = attr: the_package.__version__ will fail if the_package imports install dependencies (that are typically not installed yet), which is in fact really common.
Maybe we could use the ast module in the current attr implementation, and fallback on importing if it does not work, for backard compatibility? (for the cases where the attribute is imported or computed).
Tried a basic POC:
def attr(file, name):
with open(file) as f:
module = ast.parse(f.read())
for node in ast.iter_child_nodes(module):
if (
isinstance(node, ast.Assign)
and len(node.targets) == 1
and isinstance(node.targets[0], ast.Name)
and node.targets[0].id == name
and isinstance(node.value, ast.Constant)
):
return node.value.value
It would not work for __doc__ though, but we could also add a new directive, say doc: which would use the ast.get_docstring function, this is another issue.
Related to #1724
Currently
setup.cfgprovide anattr:directive to extract a value from a file, which is really nice typically to single-source version and description like:But the
version = attr: the_package.__version__will fail ifthe_packageimports install dependencies (that are typically not installed yet), which is in fact really common.Maybe we could use the ast module in the current
attrimplementation, and fallback on importing if it does not work, for backard compatibility? (for the cases where the attribute is imported or computed).Tried a basic POC:
It would not work for
__doc__though, but we could also add a new directive, saydoc:which would use theast.get_docstringfunction, this is another issue.