Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix version pattern pre-releases #705

Merged
merged 2 commits into from
Aug 11, 2023
Merged

Fix version pattern pre-releases #705

merged 2 commits into from
Aug 11, 2023

Conversation

deathaxe
Copy link
Contributor

@deathaxe deathaxe commented Aug 5, 2023

This commit fixes a precedence issue with version pre-release tags.

Relational

The version module exports VERSION_PATTERN.

Directly using it, might not produce expected results for pre-releases.

>>> import re
>>> from packaging.version import VERSION_PATTERN

>>> re.match(VERSION_PATTERN, "1.0.0-alpha1")
<re.Match object; span=(0, 7), match='1.0.0-a'>

Trailing 1 from -alpha1 is not matched, because previous pattern preferred consuming only a instead of alpha due to missing word boundary checks.

Simplest solution is to re-order tokens, so regexp prefers longer tokens over their abbreviation.

>>> import re
>>> from packaging.version import VERSION_PATTERN

>>> re.match(VERSION_PATTERN, "1.0.0-alpha1")
<re.Match object; span=(0, 12), match='1.0.0-alpha1'>

Note: This commit also removes an unnecessary capture group.

This commit fixes a precedence issue with version pre-release tags.

Relational
---

The `version` module exports `VERSION_PATTERN`.

Directly using it, might not produce expected results for pre-releases.

```py
>>> import re
>>> from packaging.version import VERSION_PATTERN

>>> re.match(VERSION_PATTERN, "1.0.0-alpha1")
<re.Match object; span=(0, 7), match='1.0.0-a'>
```

Trailing `1` from `-alpha1` is not matched, because previous pattern preferred consuming only `a` instead of `alpha` due to missing word boundary checks.

Simplest solution is to re-order tokens, so regexp prefers longer tokens over
their abbreviation.

```py
>>> import re
>>> from packaging.version import VERSION_PATTERN

>>> re.match(VERSION_PATTERN, "1.0.0-alpha1")
<re.Match object; span=(0, 12), match='1.0.0-alpha1'>
```

Note: This commit also removes an unnecessary capture group.
@brettcannon
Copy link
Member

Is there an open issue for this?

@uranusjr
Copy link
Member

Since VERSION_PATTERN is marked as :meta hide-value: I don’t think you’re supposed to use it in the first place. An issue is defeinitely needed for discussion if you want to have a public value.

VERSION_PATTERN = _VERSION_PATTERN
"""
A string containing the regular expression used to match a valid version.
The pattern is not anchored at either end, and is intended for embedding in larger
expressions (for example, matching a version number as part of a file name). The
regular expression should be compiled with the ``re.VERBOSE`` and ``re.IGNORECASE``
flags set.
:meta hide-value:
"""

@deathaxe
Copy link
Contributor Author

A public value wouldn't have any value for me as I can't use this package anyway, due to the need of supporting python 3.3

I just stumbled about the pattern not doing what I expected without explicit anchors being added.

If the variable is not intented for public use, it should probably not be part of __all__.

Copy link
Member

@pradyunsg pradyunsg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like a reasonable fix to me.

@pradyunsg
Copy link
Member

Since VERSION_PATTERN is marked as :meta hide-value: I don’t think you’re supposed to use it in the first place

It's a part of the public API, but we don't want the entire regex string to be presented in the documentation: https://packaging.pypa.io/en/stable/version.html#packaging.version.VERSION_PATTERN

@deathaxe
Copy link
Contributor Author

An alternative would be to align the pattern with the ones from specifiers module.

(alpha|beta|preview|pre|a|b|c|rc)

@brettcannon brettcannon enabled auto-merge (squash) August 11, 2023 18:35
@brettcannon brettcannon merged commit e120b49 into pypa:main Aug 11, 2023
@deathaxe deathaxe deleted the fix/version_pattern branch August 11, 2023 19:11
RazerM added a commit to RazerM/peps that referenced this pull request Aug 12, 2023
kodiakhq bot referenced this pull request in cloudquery/plugin-sdk-python Nov 1, 2023
This PR contains the following updates:

| Package | Update | Change |
|---|---|---|
| [packaging](https://togithub.com/pypa/packaging) | minor | `==23.1` -> `==23.2` |

---

### Release Notes

<details>
<summary>pypa/packaging (packaging)</summary>

### [`v23.2`](https://togithub.com/pypa/packaging/releases/tag/23.2)

[Compare Source](https://togithub.com/pypa/packaging/compare/23.1...23.2)

#### What's Changed

-   parse_marker should consume the entire source string by [@&#8203;mwerschy](https://togithub.com/mwerschy) in [https://github.com/pypa/packaging/pull/687](https://togithub.com/pypa/packaging/pull/687)
-   Create a Security Policy file  by [@&#8203;joycebrum](https://togithub.com/joycebrum) in [https://github.com/pypa/packaging/pull/695](https://togithub.com/pypa/packaging/pull/695)
-   Add python 3.12 to CI by [@&#8203;mayeut](https://togithub.com/mayeut) in [https://github.com/pypa/packaging/pull/689](https://togithub.com/pypa/packaging/pull/689)
-   Remove URL validation from requirement parsing by [@&#8203;uranusjr](https://togithub.com/uranusjr) in [https://github.com/pypa/packaging/pull/684](https://togithub.com/pypa/packaging/pull/684)
-   Add types for packaging.version.\_Version by [@&#8203;hauntsaninja](https://togithub.com/hauntsaninja) in [https://github.com/pypa/packaging/pull/665](https://togithub.com/pypa/packaging/pull/665)
-   Add PyPy 3.10 to CI by [@&#8203;mayeut](https://togithub.com/mayeut) in [https://github.com/pypa/packaging/pull/699](https://togithub.com/pypa/packaging/pull/699)
-   Remove unused argument in `_manylinux._is_compatible` by [@&#8203;mayeut](https://togithub.com/mayeut) in [https://github.com/pypa/packaging/pull/700](https://togithub.com/pypa/packaging/pull/700)
-   Canonicalize names for requirements comparison by [@&#8203;astrojuanlu](https://togithub.com/astrojuanlu) in [https://github.com/pypa/packaging/pull/696](https://togithub.com/pypa/packaging/pull/696)
-   Add platform tag support for LoongArch by [@&#8203;loongson-zn](https://togithub.com/loongson-zn) in [https://github.com/pypa/packaging/pull/693](https://togithub.com/pypa/packaging/pull/693)
-   Ability to install `armv7l manylinux/musllinux` wheels on `armv8l` by [@&#8203;mayeut](https://togithub.com/mayeut) in [https://github.com/pypa/packaging/pull/690](https://togithub.com/pypa/packaging/pull/690)
-   Include CHANGELOG.rst in sdist by [@&#8203;astrojuanlu](https://togithub.com/astrojuanlu) in [https://github.com/pypa/packaging/pull/704](https://togithub.com/pypa/packaging/pull/704)
-   Update pyupgrade to Python 3.7+ by [@&#8203;fangchenli](https://togithub.com/fangchenli) in [https://github.com/pypa/packaging/pull/580](https://togithub.com/pypa/packaging/pull/580)
-   Fix version pattern pre-releases by [@&#8203;deathaxe](https://togithub.com/deathaxe) in [https://github.com/pypa/packaging/pull/705](https://togithub.com/pypa/packaging/pull/705)
-   Fix typos found by codespell by [@&#8203;DimitriPapadopoulos](https://togithub.com/DimitriPapadopoulos) in [https://github.com/pypa/packaging/pull/706](https://togithub.com/pypa/packaging/pull/706)
-   Support enriched metadata by [@&#8203;brettcannon](https://togithub.com/brettcannon) in [https://github.com/pypa/packaging/pull/686](https://togithub.com/pypa/packaging/pull/686)
-   Correct rST syntax in CHANGELOG.rst by [@&#8203;atugushev](https://togithub.com/atugushev) in [https://github.com/pypa/packaging/pull/709](https://togithub.com/pypa/packaging/pull/709)
-   fix: platform tag for GraalPy by [@&#8203;mayeut](https://togithub.com/mayeut) in [https://github.com/pypa/packaging/pull/711](https://togithub.com/pypa/packaging/pull/711)
-   Document that this library uses a calendar-based versioning scheme by [@&#8203;faph](https://togithub.com/faph) in [https://github.com/pypa/packaging/pull/717](https://togithub.com/pypa/packaging/pull/717)
-   fix: Update copyright date for docs by [@&#8203;garrypolley](https://togithub.com/garrypolley) in [https://github.com/pypa/packaging/pull/713](https://togithub.com/pypa/packaging/pull/713)
-   Bump pip version to avoid known vulnerabilities by [@&#8203;joycebrum](https://togithub.com/joycebrum) in [https://github.com/pypa/packaging/pull/720](https://togithub.com/pypa/packaging/pull/720)
-   Typing annotations fixed in version.py by [@&#8203;jolaf](https://togithub.com/jolaf) in [https://github.com/pypa/packaging/pull/723](https://togithub.com/pypa/packaging/pull/723)
-   parse\_{sdist,wheel}\_filename: don't raise InvalidVersion by [@&#8203;SpecLad](https://togithub.com/SpecLad) in [https://github.com/pypa/packaging/pull/721](https://togithub.com/pypa/packaging/pull/721)
-   Fix code blocks in CHANGELOG.md by [@&#8203;edmorley](https://togithub.com/edmorley) in [https://github.com/pypa/packaging/pull/724](https://togithub.com/pypa/packaging/pull/724)

#### New Contributors

-   [@&#8203;mwerschy](https://togithub.com/mwerschy) made their first contribution in [https://github.com/pypa/packaging/pull/687](https://togithub.com/pypa/packaging/pull/687)
-   [@&#8203;joycebrum](https://togithub.com/joycebrum) made their first contribution in [https://github.com/pypa/packaging/pull/695](https://togithub.com/pypa/packaging/pull/695)
-   [@&#8203;astrojuanlu](https://togithub.com/astrojuanlu) made their first contribution in [https://github.com/pypa/packaging/pull/696](https://togithub.com/pypa/packaging/pull/696)
-   [@&#8203;loongson-zn](https://togithub.com/loongson-zn) made their first contribution in [https://github.com/pypa/packaging/pull/693](https://togithub.com/pypa/packaging/pull/693)
-   [@&#8203;fangchenli](https://togithub.com/fangchenli) made their first contribution in [https://github.com/pypa/packaging/pull/580](https://togithub.com/pypa/packaging/pull/580)
-   [@&#8203;deathaxe](https://togithub.com/deathaxe) made their first contribution in [https://github.com/pypa/packaging/pull/705](https://togithub.com/pypa/packaging/pull/705)
-   [@&#8203;DimitriPapadopoulos](https://togithub.com/DimitriPapadopoulos) made their first contribution in [https://github.com/pypa/packaging/pull/706](https://togithub.com/pypa/packaging/pull/706)
-   [@&#8203;atugushev](https://togithub.com/atugushev) made their first contribution in [https://github.com/pypa/packaging/pull/709](https://togithub.com/pypa/packaging/pull/709)
-   [@&#8203;faph](https://togithub.com/faph) made their first contribution in [https://github.com/pypa/packaging/pull/717](https://togithub.com/pypa/packaging/pull/717)
-   [@&#8203;garrypolley](https://togithub.com/garrypolley) made their first contribution in [https://github.com/pypa/packaging/pull/713](https://togithub.com/pypa/packaging/pull/713)
-   [@&#8203;jolaf](https://togithub.com/jolaf) made their first contribution in [https://github.com/pypa/packaging/pull/723](https://togithub.com/pypa/packaging/pull/723)
-   [@&#8203;SpecLad](https://togithub.com/SpecLad) made their first contribution in [https://github.com/pypa/packaging/pull/721](https://togithub.com/pypa/packaging/pull/721)
-   [@&#8203;edmorley](https://togithub.com/edmorley) made their first contribution in [https://github.com/pypa/packaging/pull/724](https://togithub.com/pypa/packaging/pull/724)

**Full Changelog**: pypa/packaging@23.1...23.2

</details>

---

### Configuration

📅 **Schedule**: Branch creation - "before 4am on the first day of the month" (UTC), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Renovate Bot](https://togithub.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNi4xMDkuNCIsInVwZGF0ZWRJblZlciI6IjM2LjEwOS40IiwidGFyZ2V0QnJhbmNoIjoibWFpbiJ9-->
abravalheri added a commit to abravalheri/validate-pyproject that referenced this pull request Feb 9, 2024
hauntsaninja pushed a commit to python/peps that referenced this pull request Nov 24, 2024
Update regex from packaging project

See pypa/packaging#705

Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com>
gvanrossum pushed a commit to gvanrossum/peps that referenced this pull request Dec 10, 2024
Update regex from packaging project

See pypa/packaging#705

Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants