diff --git a/changelog.d/172.feature.md b/changelog.d/172.feature.md new file mode 100644 index 0000000..db79b1a --- /dev/null +++ b/changelog.d/172.feature.md @@ -0,0 +1 @@ +Use `importlib.metadata` to include project version string as `fastapi_cache.__version__`. \ No newline at end of file diff --git a/fastapi_cache/__init__.py b/fastapi_cache/__init__.py index d9e96a4..e127a8e 100644 --- a/fastapi_cache/__init__.py +++ b/fastapi_cache/__init__.py @@ -1,10 +1,19 @@ from typing import ClassVar, Optional, Type +# Because this project supports python 3.7 and up, Pyright treats importlib as +# an external library and so needs to be told to ignore the type issues it sees. +try: + # Python 3.8+ + from importlib.metadata import version # type: ignore +except ImportError: + # Python 3.7 + from importlib_metadata import version # type: ignore + from fastapi_cache.coder import Coder, JsonCoder from fastapi_cache.key_builder import default_key_builder from fastapi_cache.types import Backend, KeyBuilder -__version__ = "0.2.1" +__version__ = version("fastapi-cache2") # pyright: ignore[reportUnknownVariableType] __all__ = [ "Backend", "Coder", @@ -92,7 +101,11 @@ class FastAPICache: return cls._enable @classmethod - async def clear(cls, namespace: Optional[str] = None, key: Optional[str] = None) -> int: - assert cls._backend and cls._prefix is not None, "You must call init first!" # noqa: S101 + async def clear( + cls, namespace: Optional[str] = None, key: Optional[str] = None + ) -> int: + assert ( # noqa: S101 + cls._backend and cls._prefix is not None + ), "You must call init first!" namespace = cls._prefix + (":" + namespace if namespace else "") return await cls._backend.clear(namespace, key) diff --git a/poetry.lock b/poetry.lock index 886dd06..451c5a0 100644 --- a/poetry.lock +++ b/poetry.lock @@ -2574,4 +2574,4 @@ redis = ["redis"] [metadata] lock-version = "2.0" python-versions = "^3.7" -content-hash = "c16f63dc3f2e9a5c4c01faed05d4b50c48edf0221e6e371665b0b83598bc7aa9" +content-hash = "d96f466d1d43a0c98a946d3a89da93233e8f3c83faa2df3c35c937388dedef39" diff --git a/pyproject.toml b/pyproject.toml index ef2d8b1..46dd79b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -23,6 +23,7 @@ aiomcache = { version = "*", optional = true } pendulum = "*" aiobotocore = { version = ">=1.4.1,<3.0.0", optional = true } typing-extensions = { version = ">=4.1.0" } +importlib-metadata = {version = "^6.6.0", python = "<3.8"} [tool.poetry.group.linting] optional = true