Skip to content

Commit

Permalink
chore: Use built-in importlib.metadata on Python 3.8
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitry.dygalo authored and Stranger6667 committed Jan 27, 2020
1 parent f08d5d9 commit 52b24f0
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 45 deletions.
6 changes: 6 additions & 0 deletions docs/changelog.rst
Expand Up @@ -6,6 +6,11 @@ Changelog
`Unreleased`_
-------------

Changed
~~~~~~~

- Use built-in ``importlib.metadata`` on Python 3.8. `#376`_

`0.23.5`_ - 2020-01-24
----------------------

Expand Down Expand Up @@ -628,6 +633,7 @@ Fixed
.. _0.3.0: https://github.com/kiwicom/schemathesis/compare/v0.2.0...v0.3.0
.. _0.2.0: https://github.com/kiwicom/schemathesis/compare/v0.1.0...v0.2.0

.. _#376: https://github.com/kiwicom/schemathesis/issues/376
.. _#371: https://github.com/kiwicom/schemathesis/issues/371
.. _#367: https://github.com/kiwicom/schemathesis/issues/367
.. _#365: https://github.com/kiwicom/schemathesis/issues/365
Expand Down
2 changes: 1 addition & 1 deletion mypy.ini
@@ -1,5 +1,5 @@
[mypy]
python_version = 3.7
python_version = 3.8
show_error_context = true
verbosity = 0
ignore_missing_imports = true
Expand Down
59 changes: 29 additions & 30 deletions poetry.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pyproject.toml
Expand Up @@ -34,7 +34,7 @@ pyyaml = "^5.1"
pytest-subtests = "^0.2.1"
requests = "^2.22"
click = "^7.0"
importlib_metadata = "^1.1"
importlib_metadata = { version = "^1.1", python = "<3.8" }
werkzeug = "^0.16.0"

[tool.poetry.dev-dependencies]
Expand Down Expand Up @@ -64,7 +64,7 @@ multi_line_output = 3
default_section = "THIRDPARTY"
include_trailing_comma = true
known_first_party = "schemathesis"
known_third_party = ["_pytest", "aiohttp", "attr", "click", "flask", "hypothesis", "hypothesis_jsonschema", "importlib_metadata", "jsonschema", "pytest", "pytest_subtests", "requests", "schemathesis", "werkzeug", "yaml"]
known_third_party = ["_pytest", "aiohttp", "attr", "click", "flask", "hypothesis", "hypothesis_jsonschema", "jsonschema", "pytest", "pytest_subtests", "requests", "schemathesis", "werkzeug", "yaml"]

[build-system]
requires = ["poetry>=0.12"]
Expand Down
7 changes: 7 additions & 0 deletions src/schemathesis/_compat.py
@@ -1,3 +1,4 @@
# pylint: disable=unused-import
from contextlib import contextmanager
from typing import Generator
from warnings import catch_warnings, simplefilter
Expand All @@ -13,3 +14,9 @@ def handle_warnings() -> Generator[None, None, None]:
yield
except ImportError:
yield


try:
from importlib import metadata
except ImportError:
import importlib_metadata as metadata # type: ignore
8 changes: 4 additions & 4 deletions src/schemathesis/cli/output/default.py
Expand Up @@ -7,8 +7,8 @@
import click
from attr import Attribute
from hypothesis import settings
from importlib_metadata import version

from ..._compat import metadata
from ...constants import __version__
from ...models import Case, Check, Status, TestResult, TestResultSet
from ...runner import events
Expand Down Expand Up @@ -256,9 +256,9 @@ def handle_initialized(context: events.ExecutionContext, event: events.Initializ
f"platform {platform.system()} -- "
f"Python {platform.python_version()}, "
f"schemathesis-{__version__}, "
f"hypothesis-{version('hypothesis')}, "
f"hypothesis_jsonschema-{version('hypothesis_jsonschema')}, "
f"jsonschema-{version('jsonschema')}"
f"hypothesis-{metadata.version('hypothesis')}, "
f"hypothesis_jsonschema-{metadata.version('hypothesis_jsonschema')}, "
f"jsonschema-{metadata.version('jsonschema')}"
)
click.echo(versions)
click.echo(f"rootdir: {os.getcwd()}")
Expand Down
6 changes: 3 additions & 3 deletions src/schemathesis/constants.py
@@ -1,10 +1,10 @@
from enum import Enum

from importlib_metadata import PackageNotFoundError, version
from ._compat import metadata

try:
__version__ = version(__package__)
except PackageNotFoundError:
__version__ = metadata.version(__package__)
except metadata.PackageNotFoundError:
# Local run without installation
__version__ = "dev"

Expand Down
6 changes: 3 additions & 3 deletions test/cli/test_commands.py
Expand Up @@ -6,20 +6,20 @@
import yaml
from _pytest.main import ExitCode
from hypothesis import HealthCheck, Phase, Verbosity
from importlib_metadata import version
from requests import Response

from schemathesis import Case
from schemathesis._compat import metadata
from schemathesis.checks import ALL_CHECKS
from schemathesis.loaders import from_path
from schemathesis.models import Endpoint
from schemathesis.runner import DEFAULT_CHECKS

PHASES = "explicit, reuse, generate, target, shrink"
if version("hypothesis") < "4.5":
if metadata.version("hypothesis") < "4.5":
PHASES = "explicit, reuse, generate, shrink"
HEALTH_CHECKS = "data_too_large|filter_too_much|too_slow|return_value|large_base_example|not_a_test_method"
if version("hypothesis") < "5.0":
if metadata.version("hypothesis") < "5.0":
HEALTH_CHECKS = (
"data_too_large|filter_too_much|too_slow|return_value|hung_test|large_base_example|not_a_test_method"
)
Expand Down
8 changes: 6 additions & 2 deletions test/test_package.py
@@ -1,12 +1,16 @@
import sys

from importlib_metadata import PackageNotFoundError
from schemathesis._compat import metadata


def test_dev_version(monkeypatch, mocker):
# When Schemathesis is run in dev environment without installation
monkeypatch.delitem(sys.modules, "schemathesis.constants")
mocker.patch("importlib_metadata.version", side_effect=PackageNotFoundError)
if sys.version_info < (3, 8):
path = "importlib_metadata.version"
else:
path = "importlib.metadata.version"
mocker.patch(path, side_effect=metadata.PackageNotFoundError)
from schemathesis.constants import __version__

# Then it's version is "dev"
Expand Down

0 comments on commit 52b24f0

Please sign in to comment.