Skip to content

Commit

Permalink
Switch to new deprecation module
Browse files Browse the repository at this point in the history
  • Loading branch information
erichare committed Jan 31, 2024
1 parent 0d18faa commit a402472
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 148 deletions.
39 changes: 16 additions & 23 deletions astrapy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,30 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import importlib.metadata
import toml
import os

from typing import Any


def get_version() -> Any:
# Get the path to the pyproject.toml file
dir_path = os.path.dirname(os.path.realpath(__file__))
pyproject_path = os.path.join(dir_path, "..", "pyproject.toml")

# Read the pyproject.toml file and get the version from the poetry section
try:
# Poetry will create a __version__ attribute in the package's __init__.py file
return importlib.metadata.version(__package__)

# If the package is not installed, we can still get the version from the pyproject.toml file
except importlib.metadata.PackageNotFoundError:
# Get the path to the pyproject.toml file
dir_path = os.path.dirname(os.path.realpath(__file__))
pyproject_path = os.path.join(dir_path, "..", "pyproject.toml")

# Read the pyproject.toml file and get the version from the poetry section
try:
with open(pyproject_path, encoding="utf-8") as pyproject:
# Load the pyproject.toml file as a dictionary
file_contents = pyproject.read()
pyproject_data = toml.loads(file_contents)

# Return the version from the poetry section
return pyproject_data["tool"]["poetry"]["version"]

# If the pyproject.toml file does not exist or the version is not found, return unknown
except (FileNotFoundError, KeyError):
return "unknown"
with open(pyproject_path, encoding="utf-8") as pyproject:
# Load the pyproject.toml file as a dictionary
file_contents = pyproject.read()
pyproject_data = toml.loads(file_contents)

# Return the version from the poetry section
return pyproject_data["tool"]["poetry"]["version"]

# If the pyproject.toml file does not exist or the version is not found, return unknown
except (FileNotFoundError, KeyError):
return "unknown"


__version__ = get_version()
64 changes: 32 additions & 32 deletions astrapy/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@
from __future__ import annotations

import asyncio
import deprecation
import httpx
import logging
import json
import threading
from warnings import warn


from concurrent.futures import ThreadPoolExecutor
from functools import partial
Expand All @@ -39,6 +38,7 @@
AsyncGenerator,
)

from astrapy import __version__
from astrapy.api import AsyncAPIRequestHandler, APIRequestHandler
from astrapy.defaults import (
DEFAULT_AUTH_HEADER,
Expand Down Expand Up @@ -798,12 +798,13 @@ def replace(self, path: str, document: API_DOC) -> API_RESPONSE:
"""
return self._put(path=path, document=document)

@deprecation.deprecated( # type: ignore
deprecated_in="0.7.0",
removed_in="1.0.0",
current_version=__version__,
details="Use the 'delete_one' method instead",
)
def delete(self, id: str) -> API_RESPONSE:
DEPRECATION_MESSAGE = (
"Method 'delete' of AstraDBCollection is deprecated. Please "
"switch to method 'delete_one'."
)
warn(DEPRECATION_MESSAGE, DeprecationWarning, stacklevel=2)
return self.delete_one(id)

def delete_one(self, id: str) -> API_RESPONSE:
Expand Down Expand Up @@ -883,12 +884,13 @@ def delete_subdocument(self, id: str, subdoc: str) -> API_RESPONSE:

return response

@deprecation.deprecated( # type: ignore
deprecated_in="0.7.0",
removed_in="1.0.0",
current_version=__version__,
details="Use the 'upsert_one' method instead",
)
def upsert(self, document: API_DOC) -> str:
# Deprecated: call the upsert_one method
DEPRECATION_MESSAGE = "Method 'upsert' of AstraDBCollection is deprecated. Please switch to method 'upsert_one'."

warn(DEPRECATION_MESSAGE, DeprecationWarning, stacklevel=2)

return self.upsert_one(document)

def upsert_one(self, document: API_DOC) -> str:
Expand Down Expand Up @@ -1760,12 +1762,13 @@ async def delete_subdocument(self, id: str, subdoc: str) -> API_RESPONSE:

return response

@deprecation.deprecated( # type: ignore
deprecated_in="0.7.0",
removed_in="1.0.0",
current_version=__version__,
details="Use the 'upsert_one' method instead",
)
async def upsert(self, document: API_DOC) -> str:
# Deprecated: call the upsert_one method
DEPRECATION_MESSAGE = "Method 'upsert' of AstraDBCollection is deprecated. Please switch to method 'upsert_one'."

warn(DEPRECATION_MESSAGE, DeprecationWarning, stacklevel=2)

return await self.upsert_one(document)

async def upsert_one(self, document: API_DOC) -> str:
Expand Down Expand Up @@ -2037,6 +2040,12 @@ def delete_collection(self, collection_name: str) -> API_RESPONSE:

return response

@deprecation.deprecated( # type: ignore
deprecated_in="0.7.0",
removed_in="1.0.0",
current_version=__version__,
details="Use the 'AstraDBCollection.clear()' method instead",
)
def truncate_collection(self, collection_name: str) -> AstraDBCollection:
"""
Clear a collection in the database, deleting all stored documents.
Expand All @@ -2045,14 +2054,6 @@ def truncate_collection(self, collection_name: str) -> AstraDBCollection:
Returns:
collection: an AstraDBCollection instance
"""
DEPRECATION_MESSAGE = (
"Method 'truncate_collection' of AstraDB is deprecated. Please "
"switch to method 'clear' of the AstraDBCollection object, e.g. "
"'astra_db.collection(\"my_collection\").clear()'."
" Note the returned object is different."
)
warn(DEPRECATION_MESSAGE, DeprecationWarning, stacklevel=2)

collection = AstraDBCollection(
collection_name=collection_name,
astra_db=self,
Expand Down Expand Up @@ -2275,6 +2276,12 @@ async def delete_collection(self, collection_name: str) -> API_RESPONSE:

return response

@deprecation.deprecated( # type: ignore
deprecated_in="0.7.0",
removed_in="1.0.0",
current_version=__version__,
details="Use the 'AsyncAstraDBCollection.clear()' method instead",
)
async def truncate_collection(self, collection_name: str) -> AsyncAstraDBCollection:
"""
Clear a collection in the database, deleting all stored documents.
Expand All @@ -2283,13 +2290,6 @@ async def truncate_collection(self, collection_name: str) -> AsyncAstraDBCollect
Returns:
collection: an AsyncAstraDBCollection instance
"""
DEPRECATION_MESSAGE = (
"Method 'truncate_collection' of AsyncAstraDB is deprecated. Please "
"switch to method 'clear' of the AsyncAstraDBCollection object, e.g. "
"'async_astra_db.collection(\"my_collection\").clear()'"
" Note the returned object is different."
)
warn(DEPRECATION_MESSAGE, DeprecationWarning, stacklevel=2)

collection = AsyncAstraDBCollection(
collection_name=collection_name,
Expand Down
Loading

0 comments on commit a402472

Please sign in to comment.