Skip to content

Version variable is not changed when using PEP440 specification #1018

Open
@lukasbehammer

Description

@lukasbehammer

Bug Report

Description

If the version variable includes a pre-release segment , post-release segment or development segment (PEP440 / PyPA Version Specifiers), the version is not getting updated.

Expected behavior

Version variable gets updated to new version if a release is made.

Actual behavior

Version variable stays the same and does not get updated if a release is made.

Environment

  • Operating System (w/ version): Windows 11 (local), Ubuntu (GitHub Actions Runner)
  • Python version: 3.11.9
  • Pip version: 24.0
  • Semantic-release version: 9.8.6
  • Build tool (w/ version): meson-python 0.16.0
pip freeze
alabaster==0.7.16
annotated-types==0.7.0
anyio==4.3.0
argon2-cffi==23.1.0
argon2-cffi-bindings==21.2.0
arrow==1.3.0
asttokens==2.4.1
async-lru==2.0.4
attrs==23.2.0
Babel==2.15.0
backports.tarfile==1.2.0
beautifulsoup4==4.12.3
bleach==6.1.0
build==1.2.1
cachetools==5.3.3
certifi==2024.2.2
cffi==1.16.0
cfgv==3.4.0
chardet==5.2.0
charset-normalizer==3.3.2
click==8.1.7
click-option-group==0.5.6
colorama==0.4.6
comm==0.2.2
contourpy==1.2.1
coverage==7.5.1
cycler==0.12.1
debugpy==1.8.1
decorator==5.1.1
defusedxml==0.7.1
distlib==0.3.8
docutils==0.21.2
dotty-dict==1.3.1
executing==2.0.1
fastjsonschema==2.19.1
filelock==3.14.0
fonttools==4.51.0
fqdn==1.5.1
fsspec==2024.5.0
furo==2024.5.6
gitdb==4.0.11
GitPython==3.1.43
h11==0.14.0
httpcore==1.0.5
httpx==0.27.0
identify==2.5.36
idna==3.7
imageio==2.34.1
imagesize==1.4.1
importlib_metadata==8.0.0
importlib_resources==6.4.0
iniconfig==2.0.0
intel-openmp==2021.4.0
ipykernel==6.29.4
ipython==8.24.0
ipywidgets==8.1.2
isoduration==20.11.0
jaraco.classes==3.4.0
jaraco.context==5.3.0
jaraco.functools==4.0.1
jedi==0.19.1
Jinja2==3.1.4
json5==0.9.25
jsonpointer==2.4
jsonschema==4.22.0
jsonschema-specifications==2023.12.1
jupyter==1.0.0
jupyter-console==6.6.3
jupyter-events==0.10.0
jupyter-lsp==2.2.5
jupyter_client==8.6.1
jupyter_core==5.7.2
jupyter_server==2.14.0
jupyter_server_terminals==0.5.3
jupyterlab==4.2.0
jupyterlab_pygments==0.3.0
jupyterlab_server==2.27.1
jupyterlab_widgets==3.0.10
keyring==25.2.1
kiwisolver==1.4.5
lazy_loader==0.4
lxml==5.3.0
markdown-it-py==3.0.0
MarkupSafe==2.1.5
matplotlib==3.9.0
matplotlib-inline==0.1.7
mdurl==0.1.2
meson==1.4.0
meson-python==0.16.0
mistune==3.0.2
mkl==2021.4.0
more-itertools==10.3.0
mpmath==1.3.0
mypy==1.10.0
mypy-extensions==1.0.0
nbclient==0.10.0
nbconvert==7.16.4
nbformat==5.10.4
nest-asyncio==1.6.0
networkx==3.3
nh3==0.2.17
nibabel==5.2.1
nodeenv==1.8.0
notebook==7.2.0
notebook_shim==0.2.4
numpy==1.26.4
overrides==7.7.0
packaging==24.0
pandocfilters==1.5.1
parso==0.8.4
pillow==10.3.0
piq==0.8.0
pkginfo==1.10.0
platformdirs==4.2.2
pluggy==1.5.0
pre-commit==3.8.0
prometheus_client==0.20.0
prompt-toolkit==3.0.43
psutil==5.9.8
pure-eval==0.2.2
pycparser==2.22
pydantic==2.8.2
pydantic_core==2.20.1
Pygments==2.18.0
pyparsing==3.1.2
pyproject-api==1.6.1
pyproject-metadata==0.8.0
pyproject_hooks==1.1.0
pytest==8.2.1
pytest-cov==5.0.0
pytest-doctestplus==1.2.1
python-dateutil==2.9.0.post0
python-gitlab==4.9.0
python-json-logger==2.0.7
python-semantic-release==9.8.6
pywin32==306
pywin32-ctypes==0.2.2
pywinpty==2.0.13
PyYAML==6.0.1
pyzmq==26.0.3
qtconsole==5.5.2
QtPy==2.4.1
readme_renderer==43.0
referencing==0.35.1
requests==2.32.1
requests-toolbelt==1.0.0
rfc3339-validator==0.1.4
rfc3986==2.0.0
rfc3986-validator==0.1.1
rich==13.7.1
rpds-py==0.18.1
ruff==0.4.4
scikit-image==0.23.2
scipy==1.13.0
Send2Trash==1.8.3
shellingham==1.5.4
six==1.16.0
smmap==5.0.1
sniffio==1.3.1
snowballstemmer==2.2.0
soupsieve==2.5
Sphinx==7.3.7
sphinx-autobuild==2024.4.16
sphinx-basic-ng==1.0.0b2
sphinx-copybutton==0.5.2
sphinx-github-changelog==1.4.0
sphinx-hoverxref==1.4.0
sphinx-last-updated-by-git==0.3.8
sphinx-notfound-page==1.0.4
sphinxcontrib-applehelp==1.0.8
sphinxcontrib-devhelp==1.0.6
sphinxcontrib-email==0.3.6
sphinxcontrib-htmlhelp==2.0.5
sphinxcontrib-jquery==4.1
sphinxcontrib-jsmath==1.0.1
sphinxcontrib-qthelp==1.0.7
sphinxcontrib-serializinghtml==1.1.10
sphinxprettysearchresults==0.3.5
stack-data==0.6.3
starlette==0.38.2
sympy==1.12
tbb==2021.12.0
terminado==0.18.1
tifffile==2024.5.10
tinycss2==1.3.0
tomlkit==0.13.0
torch==2.4.0
torchvision==0.19.0
tornado==6.4
tox==4.15.0
tqdm==4.66.5
traitlets==5.14.3
twine==5.1.1
types-python-dateutil==2.9.0.20240316
typing_extensions==4.11.0
uri-template==1.3.0
urllib3==2.2.1
uvicorn==0.30.6
virtualenv==20.26.2
watchfiles==0.23.0
wcwidth==0.2.13
webcolors==1.13
webencodings==0.5.1
websocket-client==1.8.0
websockets==13.0
widgetsnbextension==4.0.10
zipp==3.19.2

git log --oneline --decorate --graph --all -n 50

Configuration

Semantic Release Configuration
# Semantic Release
[tool.semantic_release]
commit_parser = "angular"
logging_use_named_masks = true
allow_zero_version = true
tag_format = "v{version}"
version_variables = ["./src/foo/__init__.py:__version__"]
commit_message = "chore(release): {version}\n\nAutomatically generated by python-semantic-release"

[tool.semantic_release.changelog]
template_dir = "./templates"
changelog_file = "CHANGELOG.md"
exclude_commit_patterns = [".*\\n\\nAutomatically generated by python-semantic-release", "chore: pre-commit autoupdate\n\nAutomatically generated by pre-commit.ci"]

[tool.semantic_release.commit_parser_options]
allowed_types = [
    "build", "chore", "ci", "docs", "feat", "fix",
    "perf", "style", "refactor", "test", "book",
]
minor_types = ["feat"]
patch_types = ["fix", "perf"]

Build System Configuration

GitHub Actions Job Definition
  semantic_release:
    name: Semantic Release
    runs-on: ubuntu-latest
    if: github.ref == 'refs/heads/main'
    permissions:
      id-token: write
      contents: write
    outputs:
      tag: ${{ steps.release.outputs.tag }}
      released: ${{ steps.release.outputs.released }}

    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
          ref: main
          submodules: recursive

      - name: Python Semantic Release
        id: release
        uses: python-semantic-release/python-semantic-release@master
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}

Execution Log

semantic-release -vv command
The next version is: 1.11.1! 🚀
           DEBUG    [semantic_release.cli.commands.version] DEBUG version.apply_version_to_source_files: writing version 1.11.1 to      version.py:142
                    source paths ['src\\foo\\__init__.py']                                                                                           
           DEBUG    [semantic_release.version.declaration] DEBUG declaration.content: No content stored, reading from source file    declaration.py:37
                    C:\vIQA_Package\src\foo\__init__.py                                                                                              
           DEBUG    [semantic_release.version.declaration] DEBUG declaration.replace: path='src\\foo\\__init__.py'                 declaration.py:197
                    pattern='(?x)__version__\\s*(:=|[:=])\\s*(?P<quote>[\'\\"])(?P<version>\n    (?P<major>0|[1-9]\\d*)\n    \\.\n                    
                    (?P<minor>0|[1-9]\\d*)\n    \\.\n    (?P<patch>0|[1-9]\\d*)\n                                                                     
                    (?:-(?P<prerelease>(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\\.(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*                   
                    ))?\n    (?:\\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?\n    )(?P=quote)' num_matches=0                            
Skipping build due to --skip-build flag
           INFO     [semantic_release.cli.github_actions_output] INFO github_actions_output.write_if_possible: not writing github_actions_output.py:71
                    GitHub Actions output, as no file specified

Additional context

For my Python package I use two branches: A main branch with the code for releases and a dev branch for current feature development. If I start a new feature on the dev branch I manually set __version__ in __init__.py to x.x.xdevx according to PEP440. Automatic dev and post releases with python-semantic-release were even better, but that's a different topic (Maybe I'll write a seperate issue later, about how this could be incorporated without loosing the generalization as discussed in #455. After pushing to the dev branch a github action gets triggered, but no release is made (this is right at the moment, no release should be made on the dev branch). After testing of the feature, the main branch gets rebased on the dev branch (or dev gets pulled into main) and after pushing the GitHub Action is triggered again, this time on the main branch. A release is made, so my other jobs (uploading to PyPI, GitHub releases etc.) are also triggered. The problem is if the push still contains the dev version in __init__.py, which is likely, the file doesn't get updated. Because there's also no error the action proceeds with the build (the files have the wrong version in the filename and __init__.py) and get uploaded to PyPI with this false version (I have no version checking in between, because all other jobs pass and I rely on python-semantic-release to determine the right version). I tested this behavior on the GitHub Actions runner in my repository https://github.com/3dct/vIQA as well as locally. For a quick minimal example:

Build a sample package with some commits, set the __version__ in __init__.py to e.g. 1.1.1dev1 and run semantic-release -vv version --patch --no-commit --no-tag --no-push --no-vcs-release --skip-build --no-changelog, so only the init.py should be changed.

Possible solution

Based on the DEBUG text I suppose such PEP440 segments aren't covered by the regex pattern. Adding them shouldn't be a big problem. I can maybe have a look into it. Also there should be an Exception/Error or at least a loud warning, that the version variable didn't get changed. Currently this is only viewable in the --vv Debug mode.

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmedPrevent from becoming stalefeatureA new feature or a feature requestneeds-updateNeeds status update from maintainers

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions