Skip to content

Commit

Permalink
Uses importlib.metadata in favor of pkg_resources
Browse files Browse the repository at this point in the history
  • Loading branch information
akayunov authored and funkyfuture committed Jul 22, 2023
1 parent cfed351 commit 81e2dfd
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 18 deletions.
10 changes: 5 additions & 5 deletions cerberus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@

from __future__ import absolute_import

from pkg_resources import get_distribution, DistributionNotFound

from cerberus.validator import DocumentError, Validator
from cerberus.platform import importlib_metadata
from cerberus.schema import rules_set_registry, schema_registry, SchemaError
from cerberus.utils import TypeDefinition
from cerberus.validator import DocumentError, Validator


try:
__version__ = get_distribution("Cerberus").version
except DistributionNotFound:
__version__ = importlib_metadata.version("Cerberus")
except importlib_metadata.PackageNotFoundError:
__version__ = "unknown"

__all__ = [
Expand All @@ -29,4 +28,5 @@
Validator.__name__,
"schema_registry",
"rules_set_registry",
"__version__",
]
29 changes: 25 additions & 4 deletions cerberus/platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@


if PYTHON_VERSION < 3:
_str_type = basestring # noqa: F821
_int_types = (int, long) # noqa: F821
_str_type = basestring # noqa: F821
else:
_str_type = str
_int_types = (int,)
_str_type = str


if PYTHON_VERSION < 3.3:
from collections import ( # noqa: F401
from collections import (
Callable,
Container,
Hashable,
Expand All @@ -30,7 +30,7 @@
Sized,
)
else:
from collections.abc import ( # noqa: F401
from collections.abc import (
Callable,
Container,
Hashable,
Expand All @@ -41,3 +41,24 @@
Set,
Sized,
)

if PYTHON_VERSION < 3.8:
import importlib_metadata
else:
import importlib.metadata as importlib_metadata


__all__ = (
"_int_types",
"_str_type",
"importlib_metadata",
Callable.__name__,
Container.__name__,
Hashable.__name__,
Iterable.__name__,
Mapping.__name__,
MutableMapping.__name__,
Sequence.__name__,
Set.__name__,
Sized.__name__,
)
17 changes: 9 additions & 8 deletions cerberus/tests/test_assorted.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
# -*- coding: utf-8 -*-

from decimal import Decimal
from pkg_resources import Distribution, DistributionNotFound

from pytest import mark

from cerberus import TypeDefinition, Validator
from cerberus.tests import assert_fail, assert_success
from cerberus.utils import validator_factory
from cerberus.validator import BareValidator
from cerberus.platform import PYTHON_VERSION
from cerberus.platform import PYTHON_VERSION, importlib_metadata


if PYTHON_VERSION > 3 and PYTHON_VERSION < 3.4:
Expand All @@ -21,23 +20,25 @@


def test_pkgresources_version(monkeypatch):
def create_fake_distribution(name):
return Distribution(project_name="cerberus", version="1.2.3")
def return_fake_version(name):
assert name == "Cerberus"
return "1.2.3"

with monkeypatch.context() as m:
cerberus = __import__("cerberus")
m.setattr("pkg_resources.get_distribution", create_fake_distribution)
m.setattr("cerberus.importlib_metadata.version", return_fake_version)
reload(cerberus)
assert cerberus.__version__ == "1.2.3"


def test_version_not_found(monkeypatch):
def raise_distribution_not_found(name):
raise DistributionNotFound("pkg_resources cannot get distribution")
def raise_package_not_found_error(name):
assert name == "Cerberus"
raise importlib_metadata.PackageNotFoundError

with monkeypatch.context() as m:
cerberus = __import__("cerberus")
m.setattr("pkg_resources.get_distribution", raise_distribution_not_found)
m.setattr("cerberus.importlib_metadata.version", raise_package_not_found_error)
reload(cerberus)
assert cerberus.__version__ == "unknown"

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
setup_requires=setup_requires,
tests_require=["pytest"],
test_suite="cerberus.tests",
install_requires=["setuptools"],
install_requires=["importlib-metadata; python_version < '3.8'"],
keywords=["validation", "schema", "dictionaries", "documents", "normalization"],
python_requires=">=2.7",
classifiers=[
Expand Down

0 comments on commit 81e2dfd

Please sign in to comment.