Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] type annotation for init_beanie() should use Sequence #911

Open
entropymatters opened this issue Apr 4, 2024 · 0 comments
Open

[BUG] type annotation for init_beanie() should use Sequence #911

entropymatters opened this issue Apr 4, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@entropymatters
Copy link

Describe the bug
The current annotation of init_beanie() is:

async def init_beanie(
    database: AsyncIOMotorDatabase = None,
    connection_string: Optional[str] = None,
    document_models: Optional[
        List[Union[Type[Document], Type["View"], str]]
    ] = None,
    allow_index_dropping: bool = False,
    recreate_views: bool = False,
    multiprocessing_mode: bool = False,
): ...

I.e. it uses List for the document_models argument.

To Reproduce

import asyncio

from motor import motor_asyncio
from beanie import Document, init_beanie


### these will come from other modules
class Sample(Document):
    name: str

class Another(Document):
    greeting: str

some_models = [Sample]
more_models = [Another]
###


DOCUMENT_MODELS = [
    *some_models,
    *more_models,
]

async def main() -> None:
    client = motor_asyncio.AsyncIOMotorClient('mongodb://mongo')
    database = client['mydatabase']
    await init_beanie(database=database, document_models=DOCUMENT_MODELS)

if __name__ == '__main__':
    asyncio.run(main())

Expected behavior
Code type checks using mypy

Actual behaviour

error: Argument "document_models" to "init_beanie" has incompatible type "list[type[Document]]"; expected "list[type[Document] | type[View] | str] | None"  [arg-type]
note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
note: Consider using "Sequence" instead, which is covariant

Note that DOCUMENT_MODELS is generally compatible with with the expectation, but using List and Union here appears to result in this error. IMHO, this can be fixed by using Sequence instead, since this is an argument.

I'd be glad to provide a PR if this would fix the issue.

Additional context
none

@roman-right roman-right added the bug Something isn't working label Apr 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants