Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
JulienPalard opened this issue Jan 9, 2020 · 3 comments
Closed

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

JulienPalard opened this issue Jan 9, 2020 · 3 comments

Comments

@JulienPalard
Copy link
Contributor

JulienPalard commented Jan 9, 2020

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.

@JulienPalard
Copy link
Contributor Author

This has been implemented in #1753 merged two weeks ago. Thanks @jwodder!!!

@RuRo
Copy link
Contributor

RuRo commented May 23, 2021

I just noticed, that

description = attr: the_package.__doc__

doesn't currently work and was kind of disappointed.

Would you be willing to accept a PR that enables attr: for description? (without any clever ast parsing for now)

@JulienPalard
Copy link
Contributor Author

Hi @RuRo ! I think you should open a new issue to discuss this, if you want you can still refer to this one in the description.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants