Search code examples
pythonbump2version

No Pre-Release Tags if Major Version is 0


I am using bump2version with semantic versioning. Is there a way to prevent the pre-release components of a tag from appearing if the major verison is 0 (i.e. rapid development?). As it stands, my .bumpversion.cfg is

[bumpversion]
current_version = 0.0.0
tag = False
tag_name = {new_version}
commit = True
parse =
    (?P<major>\d+)
    \.
    (?P<minor>\d+)
    \.
    (?P<patch>\d+)
    (\-(?P<pre>[a-z]+)\.(?P<prenum>\d+))?
serialize =
    {major}.{minor}.{patch}-{pre}.{prenum}
    {major}.{minor}.{patch}

[bumpversion:part:pre]
optional_value = placeholder
first_value = alpha
values =
    alpha
    beta
    rc
    placeholder

[bumpversion:part:prenum]
first_value = 1

[bumpversion:file:pyproject.toml]

which adds pre-release values when I bump part minor

>>> bumpversion minor
Bumpversion: 0.0.0 -> 0.1.0-alpha.1

I don't want the -alpha.N, -beta.N, etc., part of the tag to appear when I'm at major version 0 (i.e. rapid development; pre-release testing doesn't occur for me until major version 1 and higher).

I do want the pre-relase parts when going from major rev 0 to 1 or N to N+1 for all N > 0 (because pre-release testing will occur at these stages), just not for rapid development. I'd hate to have to manually type out --new-version for every bump while I'm in rapid development (esp. because I'll have a lot of versions until 1.0.0).

Does anyone have a solution?


Solution

  • As a "monkey-patch"/workaround, in bumpversion.version_part, I edited the function _choose_serialize_format() from

    def _choose_serialize_format(self, version, context):
    
            chosen = None
    
            logger.debug(
                "Available serialization formats: '%s'", "', '".join(self.serialize_formats)
            )
    
            for serialize_format in self.serialize_formats:
                try:
                    self._serialize(
                        version, serialize_format, context, raise_if_incomplete=True
                    )
                    chosen = serialize_format
                    logger.debug("Found '%s' to be a usable serialization format", chosen)
                except IncompleteVersionRepresentationException as e:
                    if not chosen:
                        chosen = serialize_format
                except MissingValueForSerializationException as e:
                    logger.info(e.message)
                    raise e
    
            if not chosen:
                raise KeyError("Did not find suitable serialization format")
    
            logger.debug("Selected serialization format '%s'", chosen)
    
            return chosen
    

    to

    def _choose_serialize_format(self, version, context):
    
            chosen = None
    
            logger.debug(
                "Available serialization formats: '%s'", "', '".join(self.serialize_formats)
            )
    
            if version._values["major"].value == "0":
                _serialize_formats = [
                    self.serialize_formats[-1],
                ]
            else:
                _serialize_formats = self.serialize_formats
    
            for serialize_format in _serialize_formats:
                try:
                    self._serialize(
                        version, serialize_format, context, raise_if_incomplete=True
                    )
                    chosen = serialize_format
                    logger.debug("Found '%s' to be a usable serialization format", chosen)
                except IncompleteVersionRepresentationException as e:
                    if not chosen:
                        chosen = serialize_format
                except MissingValueForSerializationException as e:
                    logger.info(e.message)
                    raise e
    
            if not chosen:
                raise KeyError("Did not find suitable serialization format")
    
            logger.debug("Selected serialization format '%s'", chosen)
    
            return chosen
    

    The lines added are

    if version._values["major"].value == "0":
        _serialize_formats = [
            self.serialize_formats[-1],
        ]
    else:
        _serialize_formats = self.serialize_formats
    

    and the lines changed are

    for serialize_format in self.serialize_formats:
    

    to

    for serialize_format in _serialize_formats: