BeanieODM / beanie

Asynchronous Python ODM for MongoDB
http://beanie-odm.dev/
Apache License 2.0
1.91k stars 201 forks source link

[BUG] mypy strict no-untyped-call #836

Closed aaronted009 closed 3 weeks ago

aaronted009 commented 5 months ago

Describe the bug When using Beanie with mypy strict typing, some typing issues make it impossible to pass the linter validation.

To Reproduce Here is an example of code:

import asyncio
from typing import Optional

from motor.motor_asyncio import AsyncIOMotorClient
from pydantic import BaseModel

from beanie import Document, init_beanie, operators

class Category(BaseModel):
    name: str
    description: str

class Product(Document):
    name: str
    description: Optional[str] = None
    price: float
    category: Category

async def example() -> None:
    client = AsyncIOMotorClient("mongodb://user:pass@host:27017")
    await init_beanie(database=client["beanie_test"], document_models=[Product])

    chocolate = Category(
        name="Chocolate",
        description="A preparation of roasted and ground cacao seeds.",
    )
    tonybar = Product(name="Tony's", price=5.95, category=chocolate)
    await tonybar.insert()

    product = await Product.find_one(operators.LTE(Product.price, 10))

if __name__ == "__main__":
    asyncio.run(example())

Here is the output.

$ > mypy --stritct _local/test_mypy_1.py
test_mypy_1.py:35: error: Call to untyped function "LTE" in typed context  [no-untyped-call]

Expected behavior There should be no error

I believe this can be fixed by specifying the NoneType for the functions missing it.

If no objection, I can submit a Pull Request for the changes.

roman-right commented 5 months ago

Thank you for the catch! I'll add this case to the typing tests suite

aaronted009 commented 4 months ago

Can I submit a PR for this

joaoheusi commented 3 months ago

I would also like to see this implemented. It bugs me to have to use # type: ignore every time i use a operator