Skip to content

Commit 7d95955

Browse files
refactor: public api expose import
1 parent 589ae61 commit 7d95955

10 files changed

Lines changed: 90 additions & 48 deletions

File tree

mongoengine/__init__.py

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,65 @@
1-
# Import submodules so that we can expose their __all__
2-
from mongoengine import (
3-
document,
4-
errors,
5-
fields,
6-
signals,
7-
)
8-
from mongoengine.synchronous import connection
9-
from mongoengine.base import queryset
10-
11-
# Import everything from each submodule so that it can be accessed via
12-
# mongoengine, e.g. instead of `from mongoengine.connection import connect`,
13-
# users can simply use `from mongoengine import connect`, or even
14-
# `from mongoengine import *` and then `connect('testdb')`.
15-
from mongoengine.synchronous.connection import * # noqa: F401
16-
from mongoengine.document import * # noqa: F401
17-
from mongoengine.errors import * # noqa: F401
18-
from mongoengine.fields import * # noqa: F401
19-
from mongoengine.signals import * # noqa: F401
1+
"""
2+
MongoEngine top-level public API.
3+
4+
Import submodules and re-export their public symbols so that users can write:
5+
6+
from mongoengine import connect
7+
from mongoengine import async_connect
8+
from mongoengine import Document, StringField
9+
from mongoengine import QuerySet, AsyncQuerySet
10+
11+
Or simply:
12+
13+
from mongoengine import *
14+
15+
Instead of importing from internal submodules.
16+
17+
This module exposes both synchronous and asynchronous APIs.
18+
Asynchronous functionality is backed by PyMongo's native async support
19+
(PyMongo >= 4.14).
20+
"""
2021

22+
from mongoengine import document, errors, fields, signals
23+
24+
# ---- private imports (for __all__ only) ----
25+
from mongoengine.synchronous import connection as _sync_connection
26+
from mongoengine.asynchronous import connection as _async_connection
27+
from mongoengine.synchronous import queryset as _sync_queryset
28+
from mongoengine.asynchronous import queryset as _async_queryset
29+
30+
# ---- public re-exports ----
31+
from mongoengine.synchronous.connection import * # noqa: F401,F403
32+
from mongoengine.asynchronous.connection import * # noqa: F401,F403
33+
from mongoengine.synchronous.queryset import * # noqa: F401,F403
34+
from mongoengine.asynchronous.queryset import * # noqa: F401,F403
35+
36+
from mongoengine.document import * # noqa: F401,F403
37+
from mongoengine.errors import * # noqa: F401,F403
38+
from mongoengine.fields import * # noqa: F401,F403
39+
from mongoengine.signals import * # noqa: F401,F403
40+
41+
# ---- public API surface ----
2142
__all__ = (
2243
list(document.__all__)
2344
+ list(fields.__all__)
24-
+ list(connection.__all__)
45+
+ list(_sync_connection.__all__)
46+
+ list(_async_connection.__all__)
47+
+ list(_sync_queryset.__all__)
48+
+ list(_async_queryset.__all__)
2549
+ list(signals.__all__)
2650
+ list(errors.__all__)
2751
)
2852

53+
# ---- hide internals ----
54+
del _sync_connection
55+
del _async_connection
56+
del _sync_queryset
57+
del _async_queryset
58+
2959
VERSION = (0, 29, 0)
3060

3161

3262
def get_version():
33-
"""Return the VERSION as a string.
34-
35-
For example, if `VERSION == (0, 10, 7)`, return '0.10.7'.
36-
"""
3763
return ".".join(map(str, VERSION))
3864

3965

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
1-
from .base import *
2-
from .queryset import *
3-
4-
# Expose just the public subset of all imported objects and constants.
5-
__all__ = (
6-
list(base.__all__) +
7-
list(queryset.__all__)
8-
)
1+
"""
2+
Asynchronous QuerySet public API.
3+
4+
Re-export the public classes/functions from:
5+
- base.py
6+
- queryset.py
7+
"""
8+
9+
from . import base as _base
10+
from . import queryset as _queryset
11+
12+
from .base import * # noqa: F401,F403
13+
from .queryset import * # noqa: F401,F403
14+
15+
__all__ = tuple(_base.__all__) + tuple(_queryset.__all__)
16+
17+
del _base
18+
del _queryset

mongoengine/common.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from pymongo import ReadPreference
21
from pymongo.database_shared import _check_name
32
from pymongo.read_preferences import Secondary, Primary, PrimaryPreferred, SecondaryPreferred, Nearest
43

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
from .connection import *
12
from .queryset import *
23

34
__all__ = [
4-
list(queryset.__all__)
5+
list(connection.__all__) +
6+
list(queryset.__all__),
57
]
Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
1-
from .base import *
2-
from .queryset import *
3-
4-
# Expose just the public subset of all imported objects and constants.
5-
__all__ = (
6-
list(base.__all__) +
7-
list(queryset.__all__)
8-
)
1+
"""
2+
Synchronous QuerySet public API.
3+
4+
Re-export the public classes/functions from:
5+
- base.py
6+
- queryset.py
7+
"""
8+
9+
from . import base as _base
10+
from . import queryset as _queryset
11+
12+
from .base import * # noqa: F401,F403
13+
from .queryset import * # noqa: F401,F403
14+
15+
__all__ = tuple(_base.__all__) + tuple(_queryset.__all__)
16+
17+
del _base
18+
del _queryset

tests/asynchronous/all_warnings/test_warnings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import warnings
99

1010
from mongoengine import *
11-
from mongoengine.asynchronous import async_connect, async_disconnect_all
11+
# from mongoengine import async_connect, async_disconnect_all
1212
from mongoengine.base.common import _document_registry
1313
from tests.asynchronous.utils import reset_async_connections
1414

tests/asynchronous/document/test_class_methods.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import unittest
22

33
from mongoengine import *
4-
from mongoengine.asynchronous import async_connect, async_get_db, async_disconnect
54
from mongoengine.pymongo_support import async_list_collection_names
65
from mongoengine.base.queryset import NULLIFY, PULL
76
from tests.asynchronous.utils import reset_async_connections

tests/asynchronous/document/test_delta.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from bson import SON
44

55
from mongoengine import *
6-
from mongoengine.asynchronous import async_disconnect
76
from mongoengine.pymongo_support import list_collection_names, async_list_collection_names
87
from tests.asynchronous.utils import MongoDBAsyncTestCase, async_get_as_pymongo, reset_async_connections
98

tests/asynchronous/queryset/test_queryset.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
from pymongo.results import UpdateResult
1212

1313
from mongoengine import *
14-
from mongoengine.asynchronous import async_connect, async_disconnect, AsyncQuerySet, async_get_db, AsyncBaseQuerySet, \
15-
AsyncQuerySetNoCache, async_register_connection
1614
from mongoengine.base import LazyReference
1715
from mongoengine.context_managers import async_query_counter, switch_db
1816
from mongoengine.errors import InvalidQueryError

tests/asynchronous/queryset/test_visitor.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from bson import ObjectId
77

88
from mongoengine import *
9-
from mongoengine.asynchronous import async_connect, async_disconnect
109
from mongoengine.common import _async_queryset_to_values
1110
from mongoengine.errors import InvalidQueryError
1211
from mongoengine.base.queryset import Q

0 commit comments

Comments
 (0)