alan-turing-institute / python-project-template

15 stars 3 forks source link

Dynamic version (?) #12

Closed yongrenjie closed 6 months ago

yongrenjie commented 10 months ago

In the current pyproject.toml we have this bit:

https://github.com/alan-turing-institute/python-project-template/blob/f1e40e728510fda5ca51874208912b4ffd934fba/project_template/pyproject.toml#L65-L67

Having the version number in two places is not ideal (unless there are checks to make sure that they stay in sync).

Is it possible / useful to change this to use tool.setuptools.dynamic as described in https://setuptools.pypa.io/en/latest/userguide/pyproject_config.html#dynamic-metadata? Then __init__.py would be the single source of truth for versioning.

It seems that we would need something like:

[tools.setuptools.dynamic]
version = {attr = "{{python_name}}.__version__"}

(though not sure about whether any of the curly braces above need to be escaped)

jack89roberts commented 8 months ago

Sorry for leaving random comments on closed issues, but if interesting for future reference you can also do this the other way around in Python 3.10+, i.e. have the version defined in pyproject.toml and use importlib.metadata to define __version__ in __init__.py, e.g. see https://github.com/alan-turing-institute/AIrsenal/blob/main/airsenal/__init__.py I find it slightly more intuitive for pyproject.toml to be the source of all metadata about the package.

yongrenjie commented 8 months ago

ooh @jack89roberts that's really helpful! agree that the toml file is better as the source of truth!

yongrenjie commented 8 months ago

maybe we should change the template to work that way too @phinate?

jack89roberts commented 8 months ago

you can also do this the other way around in Python 3.10+

I think it's 3.8+ actually, but it was called a "provisional feature" (whatever that is) until 3.10

phinate commented 8 months ago

Sorry for leaving random comments on closed issues

Open for the time being now ;)

I find it slightly more intuitive for pyproject.toml to be the source of all metadata about the package.

Yes, this makes sense. I only made peace with the __init__.py route because of knowing I can import __version__, and that I've seen it done as a release commit in other repos. Happy to explore this way though, don't think it's much work to swap it around!

phinate commented 6 months ago

this idea was implemented via #26! many thanks for bringing this back to life :)