Skip to content

Commit

Permalink
fix: remove async things from sync interface (#391)
Browse files Browse the repository at this point in the history
  • Loading branch information
roman-right committed Oct 26, 2022
1 parent 43dfb13 commit ff9be63
Show file tree
Hide file tree
Showing 15 changed files with 42 additions and 39 deletions.
2 changes: 1 addition & 1 deletion beanie/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from beanie.odm.views import View
from beanie.odm.union_doc import UnionDoc

__version__ = "1.13.0"
__version__ = "1.13.1"
__all__ = [
# ODM
"Document",
Expand Down
10 changes: 1 addition & 9 deletions beanie/sync/odm/actions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import asyncio
import inspect
from functools import wraps
from typing import (
Callable,
Expand Down Expand Up @@ -98,17 +96,11 @@ def run_actions(
actions_list = cls.get_action_list(
document_class, event_type, action_direction
)
coros = []
for action in actions_list:
if action.__name__ in exclude:
continue

if inspect.iscoroutinefunction(action):
coros.append(action(instance))
elif inspect.isfunction(action):
action(instance)

asyncio.gather(*coros)
action(instance)


def register_action(
Expand Down
10 changes: 5 additions & 5 deletions beanie/sync/odm/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from uuid import UUID, uuid4

from bson import ObjectId, DBRef
from motor.motor_asyncio import AsyncIOMotorDatabase
from pydantic import (
ValidationError,
PrivateAttr,
Expand All @@ -25,6 +24,7 @@
from pydantic.main import BaseModel
from pymongo import InsertOne
from pymongo.client_session import ClientSession
from pymongo.database import Database
from pymongo.results import (
DeleteResult,
InsertManyResult,
Expand Down Expand Up @@ -841,12 +841,12 @@ def init_fields(cls) -> None:

@classmethod
def init_settings(
cls, database: AsyncIOMotorDatabase, allow_index_dropping: bool
cls, database: Database, allow_index_dropping: bool
) -> None:
"""
Init document settings (collection and models)
:param database: AsyncIOMotorDatabase - motor database
:param database: Database - pymongo database
:param allow_index_dropping: bool
:return: None
"""
Expand Down Expand Up @@ -876,11 +876,11 @@ def init_actions(cls):

@classmethod
def init_model(
cls, database: AsyncIOMotorDatabase, allow_index_dropping: bool
cls, database: Database, allow_index_dropping: bool
) -> None:
"""
Init wrapper
:param database: AsyncIOMotorDatabase
:param database: Database
:param allow_index_dropping: bool
:return: None
"""
Expand Down
4 changes: 2 additions & 2 deletions beanie/sync/odm/interfaces/getters.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from motor.motor_asyncio import AsyncIOMotorCollection
from pymongo.collection import Collection

from beanie.sync.odm.settings.base import ItemSettings

Expand All @@ -9,7 +9,7 @@ def get_settings(cls) -> ItemSettings:
pass

@classmethod
def get_motor_collection(cls) -> AsyncIOMotorCollection:
def get_motor_collection(cls) -> Collection:
return cls.get_settings().motor_collection

@classmethod
Expand Down
2 changes: 1 addition & 1 deletion beanie/sync/odm/queries/cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

class BaseCursorQuery(Generic[CursorResultType], RunInterface):
"""
BaseCursorQuery class. Wrapper over AsyncIOMotorCursor,
BaseCursorQuery class. Wrapper over pymongo Cursor,
which parse result with model
"""

Expand Down
7 changes: 4 additions & 3 deletions beanie/sync/odm/settings/base.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from datetime import timedelta
from typing import Optional, Dict, Any, Type

from motor.motor_asyncio import AsyncIOMotorDatabase, AsyncIOMotorCollection
from pydantic import BaseModel, Field
from pymongo.collection import Collection
from pymongo.database import Database


class ItemSettings(BaseModel):
Expand All @@ -14,8 +15,8 @@ class ItemSettings(BaseModel):
bson_encoders: Dict[Any, Any] = Field(default_factory=dict)
projection: Optional[Dict[str, Any]] = None

motor_db: Optional[AsyncIOMotorDatabase]
motor_collection: Optional[AsyncIOMotorCollection] = None
motor_db: Optional[Database]
motor_collection: Optional[Collection] = None

union_doc: Optional[Type] = None

Expand Down
4 changes: 2 additions & 2 deletions beanie/sync/odm/settings/document.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import warnings
from typing import Optional, Type, List

from motor.motor_asyncio import AsyncIOMotorDatabase
from pydantic import Field
from pymongo import IndexModel
from pymongo.database import Database

from beanie.exceptions import MongoDBVersionError
from beanie.sync.odm.settings.base import ItemSettings
Expand Down Expand Up @@ -35,7 +35,7 @@ class DocumentSettings(ItemSettings):
@classmethod
def init(
cls,
database: AsyncIOMotorDatabase,
database: Database,
document_model: Type,
allow_index_dropping: bool,
) -> "DocumentSettings":
Expand Down
6 changes: 2 additions & 4 deletions beanie/sync/odm/settings/union_doc.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
from typing import Type

from motor.motor_asyncio import AsyncIOMotorDatabase
from pymongo.database import Database

from beanie.sync.odm.settings.base import ItemSettings


class UnionDocSettings(ItemSettings):
@classmethod
def init(
cls, doc_class: Type, database: AsyncIOMotorDatabase
) -> "UnionDocSettings":
def init(cls, doc_class: Type, database: Database) -> "UnionDocSettings":
settings_class = getattr(doc_class, "Settings", None)

multi_doc_settings = cls.parse_obj(vars(settings_class))
Expand Down
6 changes: 2 additions & 4 deletions beanie/sync/odm/settings/view.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from inspect import isclass
from typing import List, Dict, Any, Union, Type

from motor.motor_asyncio import AsyncIOMotorDatabase
from pymongo.database import Database

from beanie.exceptions import ViewHasNoSettings
from beanie.sync.odm.settings.base import ItemSettings
Expand All @@ -12,9 +12,7 @@ class ViewSettings(ItemSettings):
pipeline: List[Dict[str, Any]]

@classmethod
def init(
cls, view_class: Type, database: AsyncIOMotorDatabase
) -> "ViewSettings":
def init(cls, view_class: Type, database: Database) -> "ViewSettings":
settings_class = getattr(view_class, "Settings", None)
if settings_class is None:
raise ViewHasNoSettings("View must have Settings inner class")
Expand Down
4 changes: 2 additions & 2 deletions beanie/sync/odm/union_doc.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import ClassVar, Type, Dict, Optional

from motor.motor_asyncio import AsyncIOMotorDatabase
from pymongo.database import Database

from beanie.exceptions import UnionDocNotInited
from beanie.sync.odm.interfaces.aggregate import AggregateInterface
Expand All @@ -25,7 +25,7 @@ def get_settings(cls) -> UnionDocSettings:
return cls._settings

@classmethod
def init(cls, database: AsyncIOMotorDatabase):
def init(cls, database: Database):
cls._settings = UnionDocSettings.init(database=database, doc_class=cls)
cls._is_inited = True

Expand Down
2 changes: 1 addition & 1 deletion beanie/sync/odm/utils/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def init_beanie(
"""
Beanie initialization
:param database: AsyncIOMotorDatabase - motor database instance
:param database: Database - pymongo database instance
:param connection_string: str - MongoDB connection string
:param document_models: List[Union[Type[DocType], str]] - model classes
or strings with dot separated paths
Expand Down
4 changes: 2 additions & 2 deletions beanie/sync/odm/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import ClassVar

from motor.motor_asyncio import AsyncIOMotorDatabase
from pydantic import BaseModel
from pymongo.database import Database

from beanie.exceptions import ViewWasNotInitialized
from beanie.odm.fields import ExpressionField
Expand Down Expand Up @@ -48,7 +48,7 @@ def init_view(cls, database, recreate_view: bool):
)

@classmethod
def init_settings(cls, database: AsyncIOMotorDatabase) -> None:
def init_settings(cls, database: Database) -> None:
cls._settings = ViewSettings.init(database=database, view_class=cls)

@classmethod
Expand Down
16 changes: 15 additions & 1 deletion docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

Beanie project

## [1.13.1] - 2022-10-26

### Fix

- Remove redundant async things from sync interface

### Implementation

- ISSUE <https://github.com/roman-right/beanie/issues/390>

## [1.13.0] - 2022-10-22

### Improvement
Expand Down Expand Up @@ -1008,4 +1018,8 @@ how specific type should be presented in the database

[1.12.0]: https://pypi.org/project/beanie/1.12.0

[1.12.1]: https://pypi.org/project/beanie/1.12.1
[1.12.1]: https://pypi.org/project/beanie/1.12.1

[1.13.0]: https://pypi.org/project/beanie/1.13.0

[1.13.1]: https://pypi.org/project/beanie/1.13.1
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "beanie"
version = "1.13.0"
version = "1.13.1"
description = "Python ODM for MongoDB"
authors = ["Roman <roman-right@protonmail.com>"]
license = "Apache-2.0"
Expand Down
2 changes: 1 addition & 1 deletion tests/test_beanie.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@


def test_version():
assert __version__ == "1.13.0"
assert __version__ == "1.13.1"

0 comments on commit ff9be63

Please sign in to comment.