Skip to content

Commit

Permalink
Merge pull request #27 from mbdevpl/feature/fix-linting-issues
Browse files Browse the repository at this point in the history
fix linting issues
  • Loading branch information
mbdevpl authored Feb 19, 2025
2 parents a99ac98 + c048770 commit fe538fd
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 138 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ jobs:
- run: python -m pip install -e .
- run: python -m coverage run --append --branch --source . -m pip install --no-build-isolation --no-binary ebrains-drive ebrains-drive==0.6.0
- run: python -m coverage report --show-missing
- run: codecov
- run: python -m codecov --token ${{ secrets.CODECOV_TOKEN }}
publish:
if: startsWith(github.ref, 'refs/tags/v')
needs: build
Expand Down
6 changes: 3 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,9 @@ Incrementing any version component clears all existing following components.
Examples of how version is incremented:

* for ``1.5``, incrementing ``<major>`` results in ``2.0``;
* for ``1.5.1-2.4``, ``<minor>``++ results in ``1.6``;
* ``1.5.1-2.4``, ``<patch>``++, ``1.5.2``;
* ``1.5.1``, ``<major>``+=3, ``4.0.0``.
* for ``1.5.1-2.4``, ``<minor>`` ++ results in ``1.6``;
* ``1.5.1-2.4``, ``<patch>`` ++, ``1.5.2``;
* ``1.5.1``, ``<major>`` += 3, ``4.0.0``.


API details
Expand Down
13 changes: 8 additions & 5 deletions test/test_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,21 @@ def test_from_str(self):
with self.subTest(version_str=version_str, version_tuple=version_tuple):

py_version = \
pkg_resources.parse_version(version_str) # type: packaging.version.Version
# self.assertIsInstance(
# py_version, packaging.version.Version, msg=(type(py_version), py_version))
pkg_resources.parse_version(version_str)
_LOG.debug('packaging parsed version string %s into %s: %s',
repr(version_str), type(py_version), py_version)
# self.assertIsInstance(
# py_version, packaging.version.Version, msg=(type(py_version), py_version))

try:
sem_version = semver.VersionInfo.parse(version_str) # type: semver.VersionInfo
sem_version = semver.VersionInfo.parse(version_str)
except ValueError:
_LOG.debug('semver could not parse version string %s', repr(version_str))
else:
_LOG.debug('semver parsed version string %s into %s: %s',
repr(version_str), type(sem_version), sem_version)
# self.assertIsInstance(
# sem_version, semver.VersionInfo, msg=(type(sem_version), sem_version))

self.assertEqual(Version.from_str(version_str).to_tuple(), version_tuple)

Expand Down Expand Up @@ -76,7 +78,7 @@ def test_from_sem_version(self):
version_tuple = case_to_version_tuple(args, kwargs)
with self.subTest(version_str=version_str, version_tuple=version_tuple):
try:
sem_version = semver.VersionInfo.parse(version_str) # type: semver.VersionInfo
sem_version = semver.VersionInfo.parse(version_str)
except ValueError:
continue
else:
Expand Down Expand Up @@ -123,6 +125,7 @@ def test_init(self):
self.assertIsInstance(version.local, tuple)

def test_init_bad(self):
# pylint: disable = redefined-variable-type
for (args, kwargs), exception in BAD_INIT_CASES.items():
with self.subTest(args=args, kwargs=kwargs, exception=exception):
with self.assertRaises(exception):
Expand Down
18 changes: 12 additions & 6 deletions version_query/git_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ def _git_version_tags(repo: git.Repo) -> t.Mapping[git.Tag, Version]:
return versions


def _git_version_tag_commits(
version_tags: t.Iterable[git.Tag]) -> t.Mapping[git.objects.Commit, t.Set[git.Tag]]:
version_tag_commits: t.Dict[git.objects.Commit, t.Set[git.Tag]] = {}
for tag in version_tags:
_commit = tag.commit
if _commit not in version_tag_commits:
version_tag_commits[_commit] = set()
version_tag_commits[_commit].add(tag)
return version_tag_commits


def _latest_git_version_tag_on_branches(
repo: git.Repo, assume_if_none: bool, commit: git.objects.Commit, commit_distance: int,
skip_commits: t.Set[git.objects.Commit]) -> t.Union[int, t.Tuple[
Expand Down Expand Up @@ -80,12 +91,7 @@ def _latest_git_version_tag(
t.Optional[git.objects.Commit], t.Optional[git.TagReference], t.Optional[Version], int]:
"""Return (commit, tag at that commit if any, latest version, distance from the version)."""
version_tags = _git_version_tags(repo)
version_tag_commits: t.Dict[git.objects.Commit, set] = {}
for tag, version in version_tags.items():
_commit = tag.commit
if _commit not in version_tag_commits:
version_tag_commits[_commit] = set()
version_tag_commits[_commit].add(tag)
version_tag_commits = _git_version_tag_commits(version_tags.keys())
current_version_tags = {}
commit = None
if skip_commits is None:
Expand Down
53 changes: 53 additions & 0 deletions version_query/parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""Functions for parsing version strings into their components and verifying the format."""

import logging
import typing as t

from . import patterns

_LOG = logging.getLogger(__name__)


def parse_release_str(release: str) -> tuple:
"""Parse a release string into major, minor, and patch version numbers."""
match = patterns.RELEASE.fullmatch(release)
assert match is not None
major_match = match.group('major')
assert major_match is not None
major = int(major_match)
minor_match = match.group('minor')
if minor_match is not None:
minor = int(minor_match)
else:
minor = None
patch_match = match.group('patch')
if patch_match is not None:
patch = int(patch_match)
else:
patch = None
return major, minor, patch


def parse_pre_release_str(pre_release: str) -> t.Sequence[
t.Tuple[t.Optional[str], t.Optional[str], t.Optional[int]]]:
"""Parse a pre-release string into a sequence of tuples."""
parts = patterns.PRE_RELEASE.findall(pre_release)
_LOG.debug('parsed pre-release string %s into %s', repr(pre_release), parts)
tuples = []
for part in parts:
match = patterns.PRE_RELEASE_PART.fullmatch(part)
assert match is not None
pre_patch_match = match.group('prepatch')
if pre_patch_match is not None:
pre_patch = int(pre_patch_match)
else:
pre_patch = None
tuples.append((match.group('preseparator'), match.group('pretype'), pre_patch))
return tuples


def parse_local_str(local: str) -> tuple:
"""Parse a local version suffix string into a sequence."""
match = patterns.LOCAL.fullmatch(local)
assert match is not None
return tuple(_ for _ in match.groups() if _ is not None)
39 changes: 39 additions & 0 deletions version_query/patterns.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""Patterns for recognising parts of version strings and parsing them."""

import re

# pylint: disable = consider-using-f-string

_NUMBER = r'(?:0|[123456789][0123456789]*)'
# _SHA = r'[0123456789abcdef]+'
_LETTERS = r'(?:[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ]+)'
LETTERS = re.compile(_LETTERS)
_ALPHANUMERIC = r'(?:[0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ]+)'
ALPHANUMERIC = re.compile(_ALPHANUMERIC)
_SEP = r'(?:[\.-])'

_RELEASE_PARTS = \
r'(?P<major>{n})(?:\.(?P<minor>{n}))?(?:\.(?P<patch>{n}))?'.format(n=_NUMBER)
RELEASE = re.compile(_RELEASE_PARTS)

_PRE_SEPARATOR = rf'(?P<preseparator>{_SEP})'
_PRE_TYPE = rf'(?P<pretype>{_LETTERS})'
_PRE_PATCH = rf'(?P<prepatch>{_NUMBER})'
_PRE_RELEASE_PART = rf'{_PRE_SEPARATOR}?{_PRE_TYPE}?{_PRE_PATCH}?'
PRE_RELEASE_PART = re.compile(_PRE_RELEASE_PART)
_PRE_RELEASE_PARTS = r'(?:{0}{2})|(?:{0}?{1}{2}?)'.format(_SEP, _LETTERS, _NUMBER)
PRE_RELEASE = re.compile(_PRE_RELEASE_PARTS)
# PRE_RELEASE_CHECK = re.compile(rf'(?:{_PRE_RELEASE_PARTS})+')

_LOCAL_SEPARATOR = rf'({_SEP})'
_LOCAL_PART = rf'({_ALPHANUMERIC})'
_LOCAL_PARTS = rf'\+{_LOCAL_PART}(?:{_LOCAL_SEPARATOR}{_LOCAL_PART})*'
LOCAL = re.compile(_LOCAL_PARTS)


_RELEASE = r'(?P<release>{n}(?:\.{n})?(?:\.{n})?)'.format(n=_NUMBER)
_PRE_RELEASE = r'(?P<prerelease>(?:(?:{0}{2})|(?:{0}?{1}{2}?))+)'.format(_SEP, _LETTERS, _NUMBER)
_LOCAL = r'(?P<local>\+{0}([\.-]{0})*)'.format(_ALPHANUMERIC)
# _NAMED_PARTS_COUNT = 3 + 3
_VERSION = rf'{_RELEASE}{_PRE_RELEASE}?{_LOCAL}?'
VERSION = re.compile(_VERSION)
Loading

0 comments on commit fe538fd

Please sign in to comment.