pypa / flit

Simplified packaging of Python modules
https://flit.pypa.io/
BSD 3-Clause "New" or "Revised" License
2.16k stars 132 forks source link

Read version from version files via ast #630

Closed amyreese closed 1 year ago

amyreese commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

FWIW, there's prior art in pypa/setuptools for this: https://github.com/pypa/setuptools/blob/be6c0218bcba78dbd4ea0b5a8bb9acd5d5306240/setuptools/config/expand.py#L160-L168

takluyver commented 1 year ago

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

takluyver commented 1 year ago

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