Skip to content

EliahKagan/version

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

version - Version metadata retrieval and type checking

This compares a few ways to give a Python project version metadata and a top-level __version__ attribute, while specifying the version in only one place, and where mypy and pyright can still do strict type checking.

This is not exhaustive. There are other techniques besides the ones shown here. In addition, this only shows how to apply the techniques when configuring the package in pyproject.toml and using setuptools as a build backend.

License

0BSD. SeeLICENSE.

Approaches shown

  • by_literal/ - Set the value of __version__ in the package's top-level __init__.py, and have the build backend parse it out.

  • by_getattr/ - Define __getattr__ in the package's top-level __init__.py that dynamically retrieves version metadata on demand when __version__ is accessed. (This requires some extra work to get precise type hinting, since mypy rejects Literal["__version__"] as a parameter type annotation for module-level __getattr__.)

  • by_property/ - Create a subclass of ModuleType with a __version__ property that dynamically retrieves version metadata on demand. Rebind the package's __class__ attribute to that new class.

Running checks

Checks can be run together with tox.

To do so, install tox if it is not already installed. Make sure you have version 4 or higher.

Then, at the top of this repository's working tree (the directory that contains tox.ini), test and typecheck any of the individual packages as follows:

tox --root by_literal
tox --root by_getattr
tox --root by_property

These tox checks also all run on CI. See tox.yml.

Further reading

About

Version metadata retrieval and type checking

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages