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

feature: write __version__.py automatically #2

Closed
lsth opened this issue Oct 28, 2019 · 3 comments
Closed

feature: write __version__.py automatically #2

lsth opened this issue Oct 28, 2019 · 3 comments
Labels
enhancement New feature or request

Comments

@lsth
Copy link

lsth commented Oct 28, 2019

Dear Matthew,

thanks a lot for poetry-dynamic-versioning. It is great!

Additionally it would be interesting if poetry could write the file

    <package>/__version__.py

automatically and fill it with the dynamic version number string.

This feature could be implemented with just three lines at the end of _get_version():

    if config["versionfile"]:
        version_file = pyproject_path.parent / pyproject["tool"]["poetry"]["name"] / "__version__.py"
        version_file.write_text(_VERSION_FILE_TEMPLATE.format(serialized))

Would you consider a pull request?

Best regards
Lars

@mtkennerly
Copy link
Owner

Hey there! I think this would definitely make sense to add to the plugin, although it's a little more complicated than just that change. If you'd like to send a PR, that would be great, or I can take a look as soon as I have a chance.

Some thoughts:

  • I think _version.py would be a better default than __version__.py. If you have a foo/__version__.py and forget to add from foo.__version__ import __version__ in foo/__init__.py, then if an end user runs from foo import __version__, they'll get the module itself instead of a string, which would be quite surprising.
  • The module folder won't always match pyproject["tool"]["poetry"]["name"]. For example, hyphens are converted to underscores, as in this repo's poetry_dynamic_versioning folder. Poetry also supports an src layout and arbitrary package lists.
  • After poetry build finishes, the change should be reverted, like how the plugin currently reverts pyproject.toml, to make sure users won't end up accidentally committing the auto-generated change. (Once Poetry plugins are officially supported, maybe we could add it to the distribution archive directly without even making/changing the file on disk.)

I'm not sure the best way to handle the second point. We could use some info from pyproject.toml and maybe some info exposed by Poetry to make a reasonable guess on a default location, but we'd probably need a fallback option to specify the file explicitly.

@miracle2k
Copy link

I'm researching this issue right now. First of all, note the discussion here python-poetry/poetry#144 (comment) Apparently the idea of adding this to poetry existed at some point.

Also, it seems that with 3.8 (3.7 backported) it is possible to access the version from code: python-poetry/poetry#144 (comment)

For me personally, this would solve the problem of accessing the version at runtime and make it unnecessary to dynamically write a __version__.py file.

@mtkennerly
Copy link
Owner

mtkennerly commented Dec 13, 2019

This is now available in 0.4.0! Instead of creating a __version__.py from scratch, the plugin will update any placeholder __version__ = ... declarations it finds, so you have more control over how it's handled. You can customize the files and placeholders it looks for, but by default, it will update __version__ = "0.0.0" (for example) in any of these:

*.py              # e.g., my_package.py
*/__init__.py     # e.g., my_package/__init__.py
*/__version__.py
*/_version.py

@mtkennerly mtkennerly added the enhancement New feature or request label Jul 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants