diff --git a/src/safir/metadata.py b/src/safir/metadata.py index 50848f6b..a9ab7807 100644 --- a/src/safir/metadata.py +++ b/src/safir/metadata.py @@ -3,9 +3,10 @@ from __future__ import annotations +import sys from email.message import Message from importlib.metadata import metadata -from typing import Optional +from typing import Optional, cast from pydantic import BaseModel, Field @@ -66,7 +67,10 @@ def get_metadata(*, package_name: str, application_name: str) -> Metadata: project_urls, Source code Used as the ``respository_url``. """ - pkg_metadata: Message = metadata(package_name) + if sys.version_info >= (3, 10): + pkg_metadata = cast(Message, metadata(package_name)) + else: + pkg_metadata = metadata(package_name) return Metadata( name=application_name, version=pkg_metadata.get("Version", "0.0.0"), diff --git a/tests/metadata_test.py b/tests/metadata_test.py index 8bf2bbba..20ab33be 100644 --- a/tests/metadata_test.py +++ b/tests/metadata_test.py @@ -3,20 +3,22 @@ from __future__ import annotations +import sys +from email.message import Message from importlib.metadata import metadata -from typing import TYPE_CHECKING +from typing import cast import pytest from safir.metadata import get_metadata, get_project_url -if TYPE_CHECKING: - from email.message import Message - @pytest.fixture(scope="session") def safir_metadata() -> Message: - return metadata("safir") + if sys.version_info >= (3, 10): + return cast(Message, metadata("safir")) + else: + return metadata("safir") def test_get_project_url(safir_metadata: Message) -> None: