From 8b9388c1898d909301042bc4b5ace75fd0bb1727 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20S=C3=B8rensen?= Date: Sun, 4 Apr 2021 17:43:44 +0200 Subject: [PATCH] Deprecate staticmethod ensure_strategy (#50) --- awesomeversion/awesomeversion.py | 69 ++++++++++++++++++++++++-------- requirements.txt | 3 +- tests/test_awesomeversion.py | 16 +++++--- 3 files changed, 66 insertions(+), 22 deletions(-) diff --git a/awesomeversion/awesomeversion.py b/awesomeversion/awesomeversion.py index eb5fd76..3e1f57a 100644 --- a/awesomeversion/awesomeversion.py +++ b/awesomeversion/awesomeversion.py @@ -1,4 +1,6 @@ """AwesomeVersion.""" +# pylint: disable=unused-argument +import logging from typing import List, Optional, Union from .exceptions import AwesomeVersionCompare, AwesomeVersionStrategyException @@ -13,11 +15,41 @@ ) 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 @@ -25,7 +57,19 @@ def __init__(self, version: Union[str, float, int, "AwesomeVersion"]) -> None: 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 @@ -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: diff --git a/requirements.txt b/requirements.txt index b5a035b..43cb55a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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 \ No newline at end of file +requests==2.25.1 +git+git://github.com/python/typed_ast@master#egg=typed_ast==1.4.3 \ No newline at end of file diff --git a/tests/test_awesomeversion.py b/tests/test_awesomeversion.py index 870702b..1eb5869 100644 --- a/tests/test_awesomeversion.py +++ b/tests/test_awesomeversion.py @@ -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], ) @@ -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 + )