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

Read version from version files via ast #630

Merged
merged 3 commits into from Apr 9, 2023
Merged

Conversation

amyreese
Copy link
Contributor

In cases where packages import __version__ from version modules (like
those generated by setuptools_scm or attribution), this allows flit-core
to use the existing ast-based logic to read the version directly from
those files when possible. It checks a few well-known version filenames,
including the package-level __init__.py as usual, with __init__.py
taking precedence over external version files if an assigned
__version__ value exists in multiple places.

This makes flit more resilient to reading version from these packages,
even if their __init__.py has transitive imports that aren't available
in the environment at build time (eg, conda-forge builds). It also
reduces the number of times when flit has to fall back to actually
importing the __init__.py file rather than reading with ast.

@amyreese
Copy link
Contributor Author

Being able to read __version__ from a separate file, without needing to import the entire package, would resolve omnilib/ufmt#123 and mitigate the need for conda-forge to install runtime dependencies at build time to support packages that import __version__ from a separate file.

@akx
Copy link

akx commented Mar 23, 2023

@takluyver
Copy link
Member

Thanks! There's one little change I want (comment above), but I like the idea and the implementation. 👍

In cases where packages import `__version__` from version modules (like
those generated by setuptools_scm or attribution), this allows flit-core
to use the existing ast-based logic to read the version directly from
those files when possible. It checks a few well-known version filenames,
including the package-level `__init__.py` as usual, with `__init__.py`
taking precedence over external version files if an assigned
`__version__` value exists in multiple places.

This makes flit more resilient to reading version from these packages,
even if their `__init__.py` has transitive imports that aren't available
in the environment at build time (eg, conda-forge builds). It also
reduces the number of times when flit has to fall back to actually
importing the `__init__.py` file rather than reading with ast.
@takluyver takluyver merged commit 0c6608f into pypa:main Apr 9, 2023
16 checks passed
@takluyver
Copy link
Member

Thanks @amyreese, and nice work on a first contribution. 🙂

@takluyver takluyver added this to the 3.9 milestone Apr 9, 2023
@amyreese amyreese deleted the version-file branch April 9, 2023 18:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants