brightway-lca / cookiecutter-brightwaylib

A cookiecutter based project template for brightway libraries.
MIT License
1 stars 3 forks source link

Set `__version__` from file #11

Closed cmutel closed 1 year ago

cmutel commented 1 year ago

@tngTUDOR Can we merge this?

tngTUDOR commented 1 year ago

This is not a solution for issue #12 . If we don't need a version here, we shouldn't add more code.

tngTUDOR commented 1 year ago

Here's another proposition. If we only support python >= 3.8, we could use importlib.metadata to get the version, and not read the file ourselves:

"""{{cookiecutter.package_name}}."""
import importlib.metadata
from typing import Union

def get_version() -> tuple:
    def as_integer(x: str) -> Union[int, str]:
        try:
            return int(x)
        except ValueError:
            return x

    return tuple(
        as_integer(v)
        for v in importlib.metadata.version("{{ cookiecutter.package_name }}")
        .strip()
        .split(".")
    )

__version__ = get_version()

This can also work with python 3.7, if we add the retro-compatible importlib_metadata dependency and use the import correctly.

tngTUDOR commented 1 year ago

This looks good. Can I merge @cmutel ?