11# This file is part of 'miniver': https://github.com/jbweston/miniver
22#
3+ from __future__ import annotations
4+
35import os
46import subprocess
57from collections import namedtuple
1012Version = namedtuple ("Version" , ("release" , "dev" , "labels" ))
1113
1214# No public API
13- __all__ = []
15+ __all__ : list [ str ] = []
1416
1517package_root = os .path .dirname (os .path .realpath (__file__ ))
1618package_name = os .path .basename (package_root )
2628STATIC_VERSION_FILE = "_static_version.py"
2729
2830
29- def get_version (version_file = STATIC_VERSION_FILE ):
31+ def get_version (version_file : str = STATIC_VERSION_FILE ) -> str :
3032 version_info = get_static_version_info (version_file )
31- version = version_info ["version" ]
32- if version == "__use_git__" :
33+ if version_info ["version" ] == "__use_git__" :
3334 version = get_version_from_git ()
3435 if not version :
3536 version = get_version_from_git_archive (version_info )
3637 if not version :
3738 version = Version ("unknown" , None , None )
3839 return pep440_format (version )
3940 else :
40- return version
41+ return version_info [ " version" ]
4142
4243
43- def get_static_version_info (version_file = STATIC_VERSION_FILE ):
44- version_info = {}
44+ def get_static_version_info (version_file : str = STATIC_VERSION_FILE ) -> dict [ str , str ] :
45+ version_info : dict [ str , str ] = {}
4546 with open (os .path .join (package_root , version_file ), "rb" ) as f :
4647 exec (f .read (), {}, version_info )
4748 return version_info
@@ -51,7 +52,7 @@ def version_is_from_git(version_file=STATIC_VERSION_FILE):
5152 return get_static_version_info (version_file )["version" ] == "__use_git__"
5253
5354
54- def pep440_format (version_info ) :
55+ def pep440_format (version_info : Version ) -> str :
5556 release , dev , labels = version_info
5657
5758 version_parts = [release ]
@@ -68,7 +69,7 @@ def pep440_format(version_info):
6869 return "" .join (version_parts )
6970
7071
71- def get_version_from_git ():
72+ def get_version_from_git () -> Version :
7273 try :
7374 p = subprocess .Popen (
7475 ["git" , "rev-parse" , "--show-toplevel" ],
@@ -77,32 +78,32 @@ def get_version_from_git():
7778 stderr = subprocess .PIPE ,
7879 )
7980 except OSError :
80- return
81+ return None
8182 if p .wait () != 0 :
82- return
83+ return None
8384 if not os .path .samefile (p .communicate ()[0 ].decode ().rstrip ("\n " ), distr_root ):
8485 # The top-level directory of the current Git repository is not the same
8586 # as the root directory of the distribution: do not extract the
8687 # version from Git.
87- return
88+ return None
8889
8990 # git describe --first-parent does not take into account tags from branches
9091 # that were merged-in. The '--long' flag gets us the 'dev' version and
9192 # git hash, '--always' returns the git hash even if there are no tags.
9293 for opts in [["--first-parent" ], []]:
9394 try :
9495 p = subprocess .Popen (
95- ["git" , "describe" , "--long" , "--always" , "--tags" ] + opts ,
96+ ["git" , "describe" , "--long" , "--always" , "--tags" ] + opts , # type: ignore
9697 cwd = distr_root ,
9798 stdout = subprocess .PIPE ,
9899 stderr = subprocess .PIPE ,
99100 )
100101 except OSError :
101- return
102+ return None
102103 if p .wait () == 0 :
103104 break
104105 else :
105- return
106+ return None
106107
107108 description = (
108109 p .communicate ()[0 ]
@@ -142,7 +143,7 @@ def get_version_from_git():
142143# Currently we can only tell the tag the current commit is
143144# pointing to, or its hash (with no version info)
144145# if it is not tagged.
145- def get_version_from_git_archive (version_info ):
146+ def get_version_from_git_archive (version_info ) -> Version :
146147 try :
147148 refnames = version_info ["refnames" ]
148149 git_hash = version_info ["git_hash" ]
@@ -165,7 +166,7 @@ def get_version_from_git_archive(version_info):
165166 return Version ("unknown" , dev = None , labels = [f"g{ git_hash } " ])
166167
167168
168- __version__ = get_version ()
169+ __version__ : str = get_version ()
169170
170171
171172# The following section defines a module global 'cmdclass',
0 commit comments