Skip to content

Commit

Permalink
Use importlib.metadata.version instead of __version__
Browse files Browse the repository at this point in the history
  • Loading branch information
itssimon committed Oct 20, 2023
1 parent cb69f07 commit 3dbf9cf
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 24 deletions.
19 changes: 7 additions & 12 deletions apitally/django.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions apitally/flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
13 changes: 5 additions & 8 deletions apitally/starlette.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3dbf9cf

Please sign in to comment.