Skip to content

ObjectId seems not to be properly typed #137

@alebg

Description

@alebg

Hi guys!

I'm using your library in my project. I have a very strict typing setup, with poetry and python 3.13, and mypy complains about certain things. Using the latest (5.10) version of bson .

Context

...
[tool.poetry.dependencies]
python = "^3.13"

...

[tool.poetry.group.mongo.dependencies]
pymongo = "^4.11.3"
bson = "^0.5.10"
mongoengine = "^0.29.1"

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

[tool.mypy]
python_version = "3.13"
strict = true
ignore_missing_imports = true
plugins = ["pydantic.mypy"]
explicit_package_bases = true
exclude = [
  "tests/types",
]

Issues

  1. More of a question, but could point to an issue:
from bson import ObjectId

Yields the typing error:
Module "bson" does not explicitly export attribute "ObjectId"

Which gets fixed by doing:

from bson.objectid import ObjectId

Does this make sense as a solution on my side? both seem to point to the same place

  1. Independently of that, doing a simple:
value = ObjectId(<anything>)

Yields the typing error:
Call to untyped function "ObjectId" in typed context

The issue seems to come from this line not being properly typed:

def __init__(self, oid=None):

A possible fix:

def __init__(self, oid: Any = None) -> None:

Thanks a lot! Let me know if this makes sense, or if you need more details

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions