Skip to content

[BUG] Error on import #1168

@rnd42

Description

@rnd42

Describe the bug
Immediately upon importing beanie I get a TypeError.

To Reproduce
Run this code using UV run

# /// script
# dependencies = [
#   "beanie",
#   "motor<3.1",
#   "pymongo<4.2",
# ]
# requires-python = ">=3.10,<3.11"
# ///
import beanie

results:

$ uv run beanie_bug.py 
Installed 11 packages in 6ms
Traceback (most recent call last):
  File "/home/rnd/Documents/projects/python/temp/beanie_bug.py", line 15, in <module>
    import beanie
  File "/home/rnd/.cache/uv/environments-v2/beanie-3-10-bug-92db2db86771011b/lib/python3.10/site-packages/beanie/__init__.py", line 1, in <module>
    from beanie.migrations.controllers.free_fall import free_fall_migration
  File "/home/rnd/.cache/uv/environments-v2/beanie-3-10-bug-92db2db86771011b/lib/python3.10/site-packages/beanie/migrations/controllers/free_fall.py", line 4, in <module>
    from beanie.migrations.controllers.base import BaseMigrationController
  File "/home/rnd/.cache/uv/environments-v2/beanie-3-10-bug-92db2db86771011b/lib/python3.10/site-packages/beanie/migrations/controllers/base.py", line 4, in <module>
    from beanie.odm.documents import Document
  File "/home/rnd/.cache/uv/environments-v2/beanie-3-10-bug-92db2db86771011b/lib/python3.10/site-packages/beanie/odm/documents.py", line 55, in <module>
    from beanie.odm.bulk import BulkWriter
  File "/home/rnd/.cache/uv/environments-v2/beanie-3-10-bug-92db2db86771011b/lib/python3.10/site-packages/beanie/odm/bulk.py", line 22, in <module>
    InsertOne[Mapping[str, Any]],
TypeError: 'type' object is not subscriptable

I found that this is an incompatibility between Beanie 1.29 and pymongo < 4.2 given that the issue disappears with this patch:

$ git diff
diff --git a/beanie_bug.py b/beanie_bug.py
index 65be78b..d0e15e7 100644
--- a/beanie_bug.py
+++ b/beanie_bug.py
@@ -1,6 +1,6 @@
 # /// script
 # dependencies = [
-#   "beanie",
+#   "beanie<1.29",
 #   "motor<3.1",
 #   "pymongo<4.2",
 # ]
$ uv run beanie_bug.py 
Uninstalled 1 package in 1ms
Installed 1 package in 9ms
$ echo $?
0

Expected behavior
To be able to import and use beanie.

Additional context
I'm trying to thread a needle version wise to be able to use Beanie for an old Mongo 5.x server (motor<3.1, pymongo<4.2 which in turn requires Python >=3.8,<3.11) and FastMCP (requires Python >=3.10.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions