diff --git a/CHANGELOG.md b/CHANGELOG.md index e1b9e59..0923727 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,13 @@ We follow [Semantic Versions](https://semver.org/). +## Version 0.7.0 + +### Features + +- Use `importlib` instead of `pkg_resources` to get package version + + ## Version 0.6.0 ### Features diff --git a/coverage_conditional_plugin/__init__.py b/coverage_conditional_plugin/__init__.py index 5fd6b3b..b9ac519 100644 --- a/coverage_conditional_plugin/__init__.py +++ b/coverage_conditional_plugin/__init__.py @@ -2,17 +2,14 @@ import sys import traceback from importlib import import_module -from typing import ClassVar, Dict, Iterable, Optional, Tuple, Union +from typing import ClassVar, Dict, Iterable, Tuple, Union -try: # pragma: no cover - from importlib.metadata import version as metadata_version -except ImportError: # pragma: no cover - from importlib_metadata import version as metadata_version # type: ignore from coverage import CoveragePlugin from coverage.config import CoverageConfig -from packaging import version from packaging.markers import default_environment +from coverage_conditional_plugin.version import package_version + def get_env_info() -> Dict[str, object]: """Public helper to get the same env we pass to the plugin.""" @@ -23,7 +20,7 @@ def get_env_info() -> Dict[str, object]: 'sys_version_info': sys.version_info, 'os_environ': os.environ, 'is_installed': _is_installed, - 'package_version': _package_version, + 'package_version': package_version, # We need this, otherwise `_should_be_applied` can generate a warning: 'sys': sys, }) @@ -121,20 +118,6 @@ def _is_installed(package: str) -> bool: return True -def _package_version( - package: str, -) -> Optional[Tuple[int, ...]]: - """ - Helper function that fetches distribution version. - - Can throw multiple exceptions. - Be careful, use ``is_installed`` before using this one. - - Returns parsed varsion to be easily worked with. - """ - return version.parse(metadata_version(package)).release - - def coverage_init(reg, options) -> None: """ Entrypoint, part of the ``coverage`` API. diff --git a/coverage_conditional_plugin/version.py b/coverage_conditional_plugin/version.py new file mode 100644 index 0000000..7a070d6 --- /dev/null +++ b/coverage_conditional_plugin/version.py @@ -0,0 +1,22 @@ +from typing import Optional, Tuple + +try: # pragma: no cover + from importlib.metadata import version as metadata_version +except ImportError: # pragma: no cover + from importlib_metadata import version as metadata_version # type: ignore + +from packaging import version + + +def package_version( + package: str, +) -> Optional[Tuple[int, ...]]: + """ + Helper function that fetches distribution version. + + Can throw multiple exceptions. + Be careful, use ``is_installed`` before using this one. + + Returns parsed varsion to be easily worked with. + """ + return version.parse(metadata_version(package)).release diff --git a/pyproject.toml b/pyproject.toml index 225f7bf..11a3c35 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "coverage-conditional-plugin" -version = "0.6.0" +version = "0.7.0" description = "Conditional coverage based on any rules you define!" license = "MIT" diff --git a/setup.cfg b/setup.cfg index 0ca71e8..647301f 100644 --- a/setup.cfg +++ b/setup.cfg @@ -33,7 +33,7 @@ extend-exclude = per-file-ignores = # we need nested import and block variable overlap for conditional # importlib_metadata import - coverage_conditional_plugin/__init__.py: WPS433, WPS440 + coverage_conditional_plugin/version.py: WPS433, WPS440 # Disable `test_project` complexity checks: test_project/example.py: WPS202 # Enable `assert` keyword and magic numbers for tests: @@ -95,6 +95,6 @@ warn_redundant_casts = true warn_unused_configs = true warn_unreachable = true -[mypy-coverage_conditional_plugin] +[mypy-coverage_conditional_plugin.version] # We allow unused `ignore` comments, because we cannot sync it between versions: warn_unused_ignores = false diff --git a/tests/test_implementation.py b/tests/test_implementation.py index 135a4ec..6c0501a 100644 --- a/tests/test_implementation.py +++ b/tests/test_implementation.py @@ -1,4 +1,5 @@ -from coverage_conditional_plugin import _is_installed, _package_version +from coverage_conditional_plugin import _is_installed +from coverage_conditional_plugin.version import package_version def test_is_installed(): @@ -10,8 +11,8 @@ def test_is_installed(): def test_package_version(): """Ensures that ``_package_version`` is correct.""" - coverage_version = _package_version('coverage') - pytest_version = _package_version('pytest') + coverage_version = package_version('coverage') + pytest_version = package_version('pytest') assert coverage_version is not None assert coverage_version < (1000, 0, 0)