Skip to content

Single-sourcing version, setup.cfg, attr, and imports #1960

@JulienPalard

Description

@JulienPalard

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions