StatCan / datascience-cookiecutter

A Cookiecutter template for Data Science Projects in Python
MIT License
7 stars 1 forks source link

Single location for package version number. #29

Closed asolis closed 2 years ago

asolis commented 2 years ago

The current package version is repeated across setup.cfg and sphinx/conf.py configuration. This creates developer overhead by sync and updating both files while creating documentation and/or packaging.

Suggestion:modify package version should be in just one place and have sphinx and setup.cfg sync with it. The proposal is to place the __version___ value it in the __init__.py file of the default top-level package name. Accessible by: {{ cookiecutter.package_name }}.__version__

Note: this issue has been combined with creating a top level package folder (Issue #6). Necessary for easy access form setup.cfg and sphinx/conf.py.

Changes:

  1. Create cookiecutter variable {{ cookiecutter.package_name }} by replacing "-" for "_" from project name. No sure if that's the most secure way. Let's discuss about it

  2. Create a {{ cookiecutter.package_name }}.init.py with content

    __version__ = "{{ cookiecutter.project_version }}"
  3. Modify setup.cfg to read version from attr: {{ cookiecutter.package_name }}.__version__

  4. Modify conf.py to import {{ cookiecutter.package_name }}`` and read version from{{ cookiecutter.package_name }}.version```

Closes #6 @ToucheSir FYI, brainstorming if there could be an improvement to this feature

ToucheSir commented 2 years ago

This makes sense to me. It may be possible to centralize version metadata in pyproject.toml, but that is contingent on using Python >= 3.8. Likewise making it available from Sphinx requires either reading the correct TOML field yourself or a third-party library like https://github.com/sphinx-toolbox/sphinx-pyproject.

asolis commented 2 years ago

I found that this solution is creating a bit of a problem if you want to use it without installing the package editable or not. In order to read the __version__ from the top-level package, we need to set PYTHONPATH pointing to src. If we do this inside conf.py violates PEP8 : E402 and flake8 won't allow you to commit. Using pip install is a 'solution' but creates the same problem of checking who is installed. Would like to make it possible via just make docs.

asolis commented 2 years ago

It seems that a possible cleaner solution would be to integrate Sphinx with setup.cfg directly https://www.sphinx-doc.org/en/master/usage/advanced/setuptools.html

asolis commented 2 years ago

The simplest solution if we decide to keep a top level package is to include conf.py to the list of file exclusions from flake8. It's actually being recommended by the flake8 configuration page. The reason is that sphinx conf.py recommends to modify sys.path dynamically in their file, and PEP E402 doesn't.

Not completely sure if we would like to go for a top-level package template. If that's the decision we could keep this. If we vote for not keeping a top-level package we still should include the conf.py in the exclusion list of flake8, and point to "../src" instead of ".." (I believe that is a typo in the current version of conf.py)

If not top-level package then setup.cfg o pyproject.toml , needs to share the version with sphinx.

goatsweater commented 2 years ago

Setuptools integration is deprecated, and I think we're converging on a solution in #30. Closing this for now and we can reopen if the other solution doesn't pan out.