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

#### New Contributors

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

None yet

4 participants