Skip to content

Introduce ClassVar type #420

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

Merged
merged 1 commit into from
Jul 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelog.d/pr420.trivial.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Introduce :py:class:`~typing.ClassVar` for some :class:`~semver.version.Version`
class variables, mainly :data:`~semver.version.Version.NAMES` and some private.
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ requires = [
build-backend = "setuptools.build_meta"



[tool.black]
line-length = 88
target-version = ['py37', 'py38', 'py39', 'py310', 'py311']
Expand Down
14 changes: 9 additions & 5 deletions src/semver/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
from functools import wraps
from typing import (
Any,
ClassVar,
Dict,
Iterable,
Optional,
Pattern,
SupportsInt,
Tuple,
Union,
Expand Down Expand Up @@ -72,12 +74,14 @@ class Version:
__slots__ = ("_major", "_minor", "_patch", "_prerelease", "_build")

#: The names of the different parts of a version
NAMES = tuple([item[1:] for item in __slots__])
NAMES: ClassVar[Tuple[str, ...]] = tuple([item[1:] for item in __slots__])

#: Regex for number in a prerelease
_LAST_NUMBER = re.compile(r"(?:[^\d]*(\d+)[^\d]*)+")
_LAST_NUMBER: ClassVar[Pattern] = re.compile(r"(?:[^\d]*(\d+)[^\d]*)+")
#: Regex template for a semver version
_REGEX_TEMPLATE = r"""
_REGEX_TEMPLATE: ClassVar[
str
] = r"""
^
(?P<major>0|[1-9]\d*)
(?:
Expand All @@ -99,12 +103,12 @@ class Version:
$
"""
#: Regex for a semver version
_REGEX = re.compile(
_REGEX: ClassVar[Pattern] = re.compile(
_REGEX_TEMPLATE.format(opt_patch="", opt_minor=""),
re.VERBOSE,
)
#: Regex for a semver version that might be shorter
_REGEX_OPTIONAL_MINOR_AND_PATCH = re.compile(
_REGEX_OPTIONAL_MINOR_AND_PATCH: ClassVar[Pattern] = re.compile(
_REGEX_TEMPLATE.format(opt_patch="?", opt_minor="?"),
re.VERBOSE,
)
Expand Down