Skip to content

Commit 69693e4

Browse files
calmmageclaude
andcommitted
Migrate from motor to pymongo native async client
Replace all motor.motor_asyncio imports with pymongo async equivalents: - AsyncIOMotorClient → AsyncMongoClient - AsyncIOMotorDatabase → AsyncDatabase - AsyncIOMotorCollection → AsyncCollection Remove motor from pyproject.toml dependencies. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 4e41db2 commit 69693e4

File tree

14 files changed

+43
-60
lines changed

14 files changed

+43
-60
lines changed

botspot/components/data/access_control.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from pydantic_settings import BaseSettings
1515

1616
if TYPE_CHECKING:
17-
from motor.motor_asyncio import AsyncIOMotorCollection
17+
from pymongo.asynchronous.collection import AsyncCollection
1818

1919
logger = get_logger()
2020

@@ -39,7 +39,7 @@ class AccessControl:
3939
"""Manages persistent friends and admins lists with MongoDB support."""
4040

4141
def __init__(
42-
self, settings: AccessControlSettings, collection: Optional["AsyncIOMotorCollection"] = None
42+
self, settings: AccessControlSettings, collection: Optional["AsyncCollection"] = None
4343
):
4444
self.settings = settings
4545
self._mongo_available = None

botspot/components/data/contact_data.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
# from botspot.utils.internal import get_logger
1616
#
1717
# if TYPE_CHECKING:
18-
# from motor.motor_asyncio import AsyncIOMotorCollection, AsyncIOMotorDatabase # noqa: F401
18+
# from pymongo.asynchronous.collection import AsyncCollection # noqa: F401
19+
# from pymongo.asynchronous.database import AsyncDatabase # noqa: F401
1920
#
2021
# from botspot.core.botspot_settings import BotspotSettings
2122
#

botspot/components/data/mongo_database.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
from botspot.utils.internal import get_logger
77

88
if TYPE_CHECKING:
9-
from motor.motor_asyncio import AsyncIOMotorClient # noqa: F401
10-
from motor.motor_asyncio import AsyncIOMotorDatabase # noqa: F401
9+
from pymongo import AsyncMongoClient # noqa: F401
10+
from pymongo.asynchronous.database import AsyncDatabase # noqa: F401
1111

1212
logger = get_logger()
1313

@@ -28,7 +28,7 @@ def setup_dispatcher(dp):
2828
return dp
2929

3030

31-
def get_database() -> "AsyncIOMotorDatabase":
31+
def get_database() -> "AsyncDatabase":
3232
"""Get MongoDB database instance from dependency manager."""
3333
from botspot.core.dependency_manager import get_dependency_manager
3434

@@ -38,7 +38,7 @@ def get_database() -> "AsyncIOMotorDatabase":
3838
return db
3939

4040

41-
def get_mongo_client() -> "AsyncIOMotorClient":
41+
def get_mongo_client() -> "AsyncMongoClient":
4242
"""Get MongoDB client instance from dependency manager."""
4343
from botspot.core.dependency_manager import get_dependency_manager
4444

@@ -52,34 +52,34 @@ def get_mongo_client() -> "AsyncIOMotorClient":
5252

5353
def initialize(
5454
settings: MongoDatabaseSettings,
55-
) -> Tuple[Optional["AsyncIOMotorClient"], Optional["AsyncIOMotorDatabase"]]:
55+
) -> Tuple[Optional["AsyncMongoClient"], Optional["AsyncDatabase"]]:
5656
"""Initialize MongoDB connection and return both client and database.
5757
5858
Args:
5959
settings: MongoDB settings
6060
6161
Returns:
62-
Tuple of (AsyncIOMotorClient, AsyncIOMotorDatabase) or (None, None) if disabled
62+
Tuple of (AsyncMongoClient, AsyncDatabase) or (None, None) if disabled
6363
6464
Raises:
65-
ImportError: If motor is not installed
65+
ImportError: If pymongo is not installed
6666
"""
6767
# Check if MongoDB is enabled
6868
if not settings.enabled:
6969
logger.info("MongoDB is disabled")
7070
return None, None
7171

72-
# Check if motor is installed
72+
# Check if pymongo is installed
7373
try:
74-
from motor.motor_asyncio import AsyncIOMotorClient
74+
from pymongo import AsyncMongoClient
7575
except ImportError:
76-
logger.error("motor package is not installed. Please install it to use MongoDB.")
76+
logger.error("pymongo package is not installed. Please install it to use MongoDB.")
7777
raise ImportError(
78-
"motor package is not installed. Run 'poetry add motor' or 'pip install motor'"
78+
"pymongo package is not installed. Run 'uv add pymongo' or 'pip install pymongo'"
7979
)
8080

8181
# Initialize client and database
82-
client = AsyncIOMotorClient(settings.conn_str.get_secret_value())
82+
client = AsyncMongoClient(settings.conn_str.get_secret_value())
8383
db = client[settings.database]
8484
logger.info(f"MongoDB client initialized. Database: {settings.database}")
8585
return client, db

botspot/components/data/user_data.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
from botspot.utils.internal import get_logger
1111

1212
if TYPE_CHECKING:
13-
from motor.motor_asyncio import AsyncIOMotorCollection # noqa: F401
14-
from motor.motor_asyncio import AsyncIOMotorDatabase # noqa: F401
13+
from pymongo.asynchronous.collection import AsyncCollection # noqa: F401
14+
from pymongo.asynchronous.database import AsyncDatabase # noqa: F401
1515

1616
from botspot.core.botspot_settings import BotspotSettings
1717

@@ -57,7 +57,7 @@ class UserManager(Generic[UserT]):
5757

5858
def __init__(
5959
self,
60-
db: "AsyncIOMotorDatabase",
60+
db: "AsyncDatabase",
6161
collection: str,
6262
user_class: Type[UserT],
6363
settings: Optional["BotspotSettings"] = None,
@@ -108,7 +108,7 @@ async def has_user(self, user_id: int) -> bool:
108108
return bool(await self.get_user(user_id))
109109

110110
@property
111-
def users_collection(self) -> "AsyncIOMotorCollection":
111+
def users_collection(self) -> "AsyncCollection":
112112
return self.db[self.collection]
113113

114114
async def get_users(self, query: Optional[dict] = None) -> list[UserT]:
@@ -337,20 +337,20 @@ def initialize(settings: "BotspotSettings", user_class=None) -> UserManager:
337337
338338
Raises:
339339
RuntimeError: If MongoDB is not enabled or initialized
340-
ImportError: If motor package is not installed
340+
ImportError: If pymongo package is not installed
341341
"""
342-
# Check that motor is installed
342+
# Check that pymongo is installed
343343
try:
344-
import motor # noqa: F401
344+
import pymongo # noqa: F401
345345
except ImportError:
346346
from botspot.utils.internal import get_logger
347347

348348
logger = get_logger()
349349
logger.error(
350-
"motor package is not installed. Please install it to use the user_data component."
350+
"pymongo package is not installed. Please install it to use the user_data component."
351351
)
352352
raise ImportError(
353-
"motor package is not installed. Please install it with 'poetry add motor' or 'pip install motor' to use the user_data component."
353+
"pymongo package is not installed. Please install it with 'uv add pymongo' or 'pip install pymongo' to use the user_data component."
354354
)
355355

356356
# Check that MongoDB component is enabled

botspot/components/new/auto_archive.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from botspot.utils.send_safe import send_safe
1515

1616
if TYPE_CHECKING:
17-
from motor.motor_asyncio import AsyncIOMotorCollection # noqa: F401
17+
from pymongo.asynchronous.collection import AsyncCollection # noqa: F401
1818

1919

2020
class CommandFilterMode(str, Enum):
@@ -52,7 +52,7 @@ def __init__(self, settings: AutoArchiveSettings) -> None:
5252
self._warning_sent: Set[int] = set()
5353
self._collection = None
5454

55-
async def _get_collection(self) -> "AsyncIOMotorCollection":
55+
async def _get_collection(self) -> "AsyncCollection":
5656
if self._collection is None:
5757
db = get_database()
5858
self._collection = db["auto_archive_intro"]

botspot/components/new/chat_binder.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from botspot.utils.internal import get_logger
2222

2323
if TYPE_CHECKING:
24-
from motor.motor_asyncio import AsyncIOMotorCollection # noqa: F401
24+
from pymongo.asynchronous.collection import AsyncCollection # noqa: F401
2525

2626
logger = get_logger()
2727

@@ -58,7 +58,7 @@ class BoundChatRecord(BaseModel):
5858

5959
class ChatBinder:
6060
def __init__(
61-
self, settings: ChatBinderSettings, collection: Optional["AsyncIOMotorCollection"] = None
61+
self, settings: ChatBinderSettings, collection: Optional["AsyncCollection"] = None
6262
):
6363
"""Initialize the ChatBinder.
6464

botspot/components/new/contact_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from pydantic_settings import BaseSettings
44

55
if TYPE_CHECKING:
6-
from motor.motor_asyncio import AsyncIOMotorCollection # noqa: F401
6+
from pymongo.asynchronous.collection import AsyncCollection # noqa: F401
77

88

99
class ContactManagerSettings(BaseSettings):

botspot/components/new/context_builder.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
from botspot.utils.internal import get_logger
4242

4343
if TYPE_CHECKING:
44-
from motor.motor_asyncio import AsyncIOMotorCollection # noqa: F401
45-
from motor.motor_asyncio import AsyncIOMotorDatabase # noqa: F401
44+
from pymongo.asynchronous.collection import AsyncCollection # noqa: F401
45+
from pymongo.asynchronous.database import AsyncDatabase # noqa: F401
4646

4747
logger = get_logger()
4848

botspot/components/new/subscription_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from pydantic_settings import BaseSettings
44

55
if TYPE_CHECKING:
6-
from motor.motor_asyncio import AsyncIOMotorCollection # noqa: F401
6+
from pymongo.asynchronous.collection import AsyncCollection # noqa: F401
77

88

99
class SubscriptionManagerSettings(BaseSettings):

botspot/core/dependency_manager.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@
1818
from botspot.components.new.llm_provider import LLMProvider
1919
from botspot.components.new.queue_manager import QueueManager
2020
from botspot.components.new.s3_storage import S3StorageProvider
21-
from motor.motor_asyncio import (
22-
AsyncIOMotorClient, # noqa: F401
23-
AsyncIOMotorDatabase, # noqa: F401
24-
)
21+
from pymongo import AsyncMongoClient # noqa: F401
22+
from pymongo.asynchronous.database import AsyncDatabase # noqa: F401
2523

2624

2725
class DependencyManager(metaclass=Singleton):
@@ -30,8 +28,8 @@ def __init__(
3028
botspot_settings: Optional[BotspotSettings] = None,
3129
bot: Optional[Bot] = None,
3230
dispatcher: Optional[Dispatcher] = None,
33-
mongo_client: Optional["AsyncIOMotorClient"] = None,
34-
mongo_database: Optional["AsyncIOMotorDatabase"] = None,
31+
mongo_client: Optional["AsyncMongoClient"] = None,
32+
mongo_database: Optional["AsyncDatabase"] = None,
3533
**kwargs,
3634
):
3735
self._botspot_settings = botspot_settings or BotspotSettings()
@@ -81,7 +79,7 @@ def dispatcher(self, value):
8179
self._dispatcher = value
8280

8381
@property
84-
def mongo_client(self) -> "AsyncIOMotorClient":
82+
def mongo_client(self) -> "AsyncMongoClient":
8583
if self._mongo_client is None:
8684
raise BotspotError("MongoDB client is not initialized")
8785
return self._mongo_client
@@ -91,7 +89,7 @@ def mongo_client(self, value):
9189
self._mongo_client = value
9290

9391
@property
94-
def mongo_database(self) -> "AsyncIOMotorDatabase":
92+
def mongo_database(self) -> "AsyncDatabase":
9593
if self._mongo_database is None:
9694
raise BotspotError("MongoDB database is not initialized")
9795
return self._mongo_database

0 commit comments

Comments
 (0)