diff --git a/src/pycharting/__init__.py b/src/pycharting/__init__.py index f09fdc7..707cd0e 100644 --- a/src/pycharting/__init__.py +++ b/src/pycharting/__init__.py @@ -38,9 +38,13 @@ - `get_server_status`: Function to check the status of the background server. """ +from importlib.metadata import version as _distribution_version + from .api.interface import get_server_status, plot, stop_server __all__ = ["__version__", "get_server_status", "plot", "stop_server"] -# Keep this in sync with pyproject.toml -__version__ = "0.2.14" +# Resolved from the installed distribution metadata, which is generated from the +# version in pyproject.toml. That is the single place to bump; nothing here needs +# to be kept in sync by hand. +__version__ = _distribution_version("pycharting") diff --git a/src/pycharting/core/server.py b/src/pycharting/core/server.py index f096fbd..efae395 100644 --- a/src/pycharting/core/server.py +++ b/src/pycharting/core/server.py @@ -16,6 +16,7 @@ import logging import socket from collections.abc import MutableMapping +from importlib.metadata import version as _distribution_version from pathlib import Path from typing import Any @@ -118,7 +119,7 @@ def create_app() -> FastAPI: app = FastAPI( title="PyCharting", description="Interactive charting and data visualization API", - version="0.1.0", + version=_distribution_version("pycharting"), docs_url="/api/docs", redoc_url="/api/redoc", ) diff --git a/tests/pycharting/core/test_server.py b/tests/pycharting/core/test_server.py index 70b3002..407dc95 100644 --- a/tests/pycharting/core/test_server.py +++ b/tests/pycharting/core/test_server.py @@ -1,8 +1,11 @@ """Tests for FastAPI server.""" +from importlib.metadata import version as distribution_version + import pytest from fastapi.testclient import TestClient +import pycharting from pycharting.core.server import NoCacheStaticFiles, create_app, find_free_port, run_server @@ -136,10 +139,10 @@ def test_openapi_has_title(client): def test_openapi_has_version(client): - """Test that OpenAPI spec has version.""" + """The OpenAPI spec advertises the installed distribution version.""" response = client.get("/openapi.json") data = response.json() - assert data["info"]["version"] == "0.1.0" + assert data["info"]["version"] == distribution_version("pycharting") def test_docs_endpoint_available(client): @@ -190,7 +193,19 @@ def test_app_has_correct_metadata(client): assert data["info"]["title"] == "PyCharting" assert data["info"]["description"] == "Interactive charting and data visualization API" - assert data["info"]["version"] == "0.1.0" + assert data["info"]["version"] == distribution_version("pycharting") + + +def test_advertised_version_matches_the_package_attribute(client): + """The version served over HTTP is the same one ``pycharting.__version__`` reports. + + These used to be independent literals — ``0.1.0`` here and ``0.2.14`` in + ``__init__.py``, against ``0.2.16`` in the manifest — and the suite pinned the + stale values rather than catching the drift. + """ + served = client.get("/openapi.json").json()["info"]["version"] + + assert served == pycharting.__version__ == distribution_version("pycharting") class TestNoCacheStaticFiles: