Skip to content

Commit

Permalink
Deprecate staticmethod ensure_strategy (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
ludeeus authored Apr 4, 2021
1 parent 2984422 commit 8b9388c
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 22 deletions.
69 changes: 53 additions & 16 deletions awesomeversion/awesomeversion.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""AwesomeVersion."""
# pylint: disable=unused-argument
import logging
from typing import List, Optional, Union

from .exceptions import AwesomeVersionCompare, AwesomeVersionStrategyException
Expand All @@ -13,19 +15,61 @@
)
from .strategy import AwesomeVersionStrategy

_LOGGER: logging.Logger = logging.getLogger(__package__)

class AwesomeVersion(str):
"""AwesomeVersion class."""

def __init__(self, version: Union[str, float, int, "AwesomeVersion"]) -> None:
class _AwesomeVersionBase(str):
"""Base class for AwesomeVersion to allow the usage of the default JSON encoder."""

def __init__(self, string):
str.__init__(string)

def __new__(cls, version, _=None):
"""Create a new AwesomeVersion object."""

return super().__new__(cls, version)


class AwesomeVersion(_AwesomeVersionBase):
"""
AwesomeVersion class.
version:
The version to create a AwesomeVersion object from
ensure_strategy:
Match the AwesomeVersion object against spesific
strategies when creating if. If it does not match
AwesomeVersionStrategyException will be raised
"""

def __init__(
self,
version: Union[str, float, int, "AwesomeVersion"],
ensure_strategy: Optional[
Union[AwesomeVersionStrategy, List[AwesomeVersionStrategy]]
] = None,
) -> None:
"""Initialize AwesomeVersion."""
if isinstance(version, AwesomeVersion):
self._version = version._version
else:
self._version = str(version)
if isinstance(self._version, str):
self._version = self._version.strip()
str.__init__(self._version)

if ensure_strategy is not None:
ensure_strategy = (
ensure_strategy
if isinstance(ensure_strategy, list)
else [ensure_strategy]
)
if self.strategy not in ensure_strategy:
raise AwesomeVersionStrategyException(
f"Strategy {self.strategy} does not match {ensure_strategy} for {version}"
)

super().__init__(self._version)

def __enter__(self) -> "AwesomeVersion":
return self
Expand Down Expand Up @@ -96,18 +140,11 @@ def ensure_strategy(
strategy: Union[AwesomeVersionStrategy, List[AwesomeVersionStrategy]],
) -> "AwesomeVersion":
"""Return a AwesomeVersion object, or raise on creation."""
obj = AwesomeVersion(version)
if isinstance(strategy, list) and obj.strategy not in strategy:
raise AwesomeVersionStrategyException(
f"Strategy {obj.strategy} does not match {strategy} for {version}"
)

if isinstance(strategy, AwesomeVersionStrategy) and obj.strategy != strategy:
raise AwesomeVersionStrategyException(
f"Strategy {obj.strategy} does not match {strategy} for {version}"
)

return obj
_LOGGER.warning(
"Using AwesomeVersion.ensure_strategy(version, strategy) is deprecated, "
"use AwesomeVersion(version, strategy) instead"
)
return AwesomeVersion(version, strategy)

@property
def string(self) -> str:
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ black==20.8b1
pre-commit==2.11.1
pytest-cov==2.11.1
pytest==6.2.2
requests==2.25.1
requests==2.25.1
git+git://github.com/python/typed_ast@master#egg=typed_ast==1.4.3
16 changes: 11 additions & 5 deletions tests/test_awesomeversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,12 @@ def test_nesting(version):
)


def test_ensure_strategy():
def test_ensure_strategy(caplog):
"""test ensure_strategy."""
obj = AwesomeVersion.ensure_strategy("1.0.0", AwesomeVersionStrategy.SEMVER)
obj = AwesomeVersion("1.0.0", AwesomeVersionStrategy.SEMVER)
assert obj.strategy == AwesomeVersionStrategy.SEMVER

obj = AwesomeVersion.ensure_strategy(
obj = AwesomeVersion(
"1.0.0",
[AwesomeVersionStrategy.SEMVER, AwesomeVersionStrategy.SPECIALCONTAINER],
)
Expand All @@ -118,10 +118,16 @@ def test_ensure_strategy():
]

with pytest.raises(AwesomeVersionStrategyException):
AwesomeVersion.ensure_strategy("1", AwesomeVersionStrategy.SEMVER)
AwesomeVersion("1", AwesomeVersionStrategy.SEMVER)

with pytest.raises(AwesomeVersionStrategyException):
AwesomeVersion.ensure_strategy(
AwesomeVersion(
"1",
[AwesomeVersionStrategy.SEMVER, AwesomeVersionStrategy.SPECIALCONTAINER],
)

obj = AwesomeVersion.ensure_strategy("1.0.0", AwesomeVersionStrategy.SEMVER)
assert (
"Using AwesomeVersion.ensure_strategy(version, strategy) is deprecated"
in caplog.text
)

0 comments on commit 8b9388c

Please sign in to comment.