From 3dbf9cf2da171ed00f0c203a615a11cec8832b25 Mon Sep 17 00:00:00 2001 From: Simon Gurcke Date: Fri, 20 Oct 2023 19:26:30 +1000 Subject: [PATCH] Use importlib.metadata.version instead of __version__ --- apitally/django.py | 19 +++++++------------ apitally/flask.py | 7 +++---- apitally/starlette.py | 13 +++++-------- 3 files changed, 15 insertions(+), 24 deletions(-) diff --git a/apitally/django.py b/apitally/django.py index 2531851..2771481 100644 --- a/apitally/django.py +++ b/apitally/django.py @@ -4,16 +4,15 @@ import sys import time from dataclasses import dataclass +from importlib.metadata import PackageNotFoundError, version from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional -import django from django.conf import settings from django.core.exceptions import ViewDoesNotExist from django.test import RequestFactory from django.urls import URLPattern, URLResolver, get_resolver, resolve from django.utils.module_loading import import_string -import apitally from apitally.client.base import KeyInfo from apitally.client.threading import ApitallyClient @@ -263,20 +262,16 @@ def _extract_views_from_url_patterns( def _get_versions(app_version: Optional[str]) -> Dict[str, str]: versions = { "python": f"{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}", - "apitally": apitally.__version__, - "django": django.__version__, + "apitally": version("apitally"), + "django": version("django"), } try: - import rest_framework - - versions["django-rest-framework"] = rest_framework.__version__ # type: ignore[attr-defined] - except (ImportError, AttributeError): # pragma: no cover + versions["django-rest-framework"] = version("django-rest-framework") + except PackageNotFoundError: # pragma: no cover pass try: - import ninja - - versions["django-ninja"] = ninja.__version__ - except (ImportError, AttributeError): # pragma: no cover + versions["django-ninja"] = version("django-ninja") + except PackageNotFoundError: # pragma: no cover pass if app_version: versions["app"] = app_version diff --git a/apitally/flask.py b/apitally/flask.py index 1ab53f8..fd69fb9 100644 --- a/apitally/flask.py +++ b/apitally/flask.py @@ -3,15 +3,14 @@ import sys import time from functools import wraps +from importlib.metadata import version from threading import Timer from typing import TYPE_CHECKING, Any, Dict, Iterable, List, Optional, Tuple -import flask from flask import Flask, g, make_response, request from werkzeug.exceptions import NotFound from werkzeug.test import Client -import apitally from apitally.client.base import KeyInfo from apitally.client.threading import ApitallyClient @@ -160,8 +159,8 @@ def _get_openapi(app: WSGIApplication, openapi_url: str) -> Optional[str]: def _get_versions(app_version: Optional[str]) -> Dict[str, str]: versions = { "python": f"{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}", - "apitally": apitally.__version__, - "flask": flask.__version__, + "apitally": version("apitally"), + "flask": version("flask"), } if app_version: versions["app"] = app_version diff --git a/apitally/starlette.py b/apitally/starlette.py index 40344e8..bfc4536 100644 --- a/apitally/starlette.py +++ b/apitally/starlette.py @@ -4,9 +4,9 @@ import json import sys import time +from importlib.metadata import PackageNotFoundError, version from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Tuple -import starlette from httpx import HTTPStatusError from starlette.authentication import ( AuthCredentials, @@ -23,7 +23,6 @@ from starlette.testclient import TestClient from starlette.types import ASGIApp -import apitally from apitally.client.asyncio import ApitallyClient from apitally.client.base import KeyInfo @@ -230,14 +229,12 @@ def _get_routes(app: ASGIApp) -> List[BaseRoute]: def _get_versions(app_version: Optional[str]) -> Dict[str, str]: versions = { "python": f"{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}", - "apitally": apitally.__version__, - "starlette": starlette.__version__, + "apitally": version("apitally"), + "starlette": version("starlette"), } try: - import fastapi - - versions["fastapi"] = fastapi.__version__ - except (ImportError, AttributeError): # pragma: no cover + versions["fastapi"] = version("fastapi") + except PackageNotFoundError: # pragma: no cover pass if app_version: versions["app"] = app_version