BeanieODM / beanie

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

[BUG] type pylance #696

Open CAPITAINMARVEL opened 11 months ago

CAPITAINMARVEL commented 11 months ago

Describe the bug so I did try the example from github and i have a pylance error

To Reproduce

import asyncio
from typing import Optional

from motor.motor_asyncio import AsyncIOMotorClient
from pydantic import BaseModel

from beanie import Document, Indexed, init_beanie

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

class Product(Document):
    name: str                          # You can use normal types just like in pydantic
    description: Optional[str] = None
    price: Indexed(float)              # You can also specify that a field should correspond to an index
    category: Category                 # You can include pydantic models as well

# This is an asynchronous example, so we will access it from an async function
async def example():
    # Beanie uses Motor async client under the hood 
    client = AsyncIOMotorClient("mongodb://user:pass@host:27017")

    # Initialize beanie with the Product document class
    await init_beanie(database=client.db_name, document_models=[Product])

    chocolate = Category(name="Chocolate", description="A preparation of roasted and ground cacao seeds.")
    # Beanie documents work just like pydantic models
    tonybar = Product(name="Tony's", price=5.95, category=chocolate)
    # And can be inserted into the database
    await tonybar.insert() 

    # You can find documents with pythonic syntax
    product = await Product.find_one(Product.price < 10)

    # And update them
    await product.set({Product.name:"Gold bar"})

if __name__ == "__main__":
    asyncio.run(example())
CAPITAINMARVEL commented 11 months ago

I have also another error : Call expression not allowed in typePylance expression (on the Indexed(float))

class Product(Document):
    name: str                          # You can use normal types just like in pydantic
    description: Optional[str] = None
    price: Indexed(float)              # You can also specify that a field should correspond to an index
    category: Category                 # You can include pydantic models as well
roman-right commented 11 months ago

Thank you for the catch

CAPITAINMARVEL commented 11 months ago

I also have another isue with sorting

It is not possible to assign the argument of type “int” to the parameter “args” of type “str | Tuple[str, SortDirection] | List[Tuple[str, SortDirection]] | None” in “sort” function Cannot assign type 'int' to type 'str | Tuple[str, SortDirection] | List[Tuple[str, SortDirection]] | None » “int” is not compatible with “str” “int” is not compatible with “Tuple[str, SortDirection]” “int” is not compatible with “List[Tuple[str, SortDirection]]” Type cannot be assigned to type “None” Pylance

await DocPlayer.find(DocPlayer.lb.rep >0).sort(-DocPlayer.lb.rep).project(ProjectTopRep).to_list()

class ProjectTopRep(BaseModel):
    player_id: int
    rep: int

    class Settings:
        projection = {"player_id": True, "rep": "$lb.rep"}
CAPITAINMARVEL commented 11 months ago

Unable to assign argument of type “dict[bool, bool]” to parameter “expression” of type “Dict[ExpressionField | str, Any]” in the “set” function Unable to assign type 'bool' to type 'ExpressionField | str » “bool” is not compatible with “ExpressionField” “bool” is not compatible with “str”Pylanc

how to do it :

class Player(Document):
    player_id: int
    hidestats: bool

await Product.find_one().set({Player.hidestats : True})

It does update the field but Pylance do not like it

CAPITAINMARVEL commented 11 months ago
class Quiz(Document):
    right: int
await Quiz.find_one(Quiz.id == self.quiz.id).inc({Quiz.right : 1})

Unable to assign argument of type “dict[int, int]” to parameter “expression” of type “Dict[ExpressionField | str, Any]” in the “inc” function Cannot assign type 'int' to type 'ExpressionField | str » “int” is not compatible with “ExpressionField” “int” is not compatible with “str”PylancereportGeneralTypeIssues (variable) right: int

CAPITAINMARVEL commented 11 months ago
class Player(Document):
    money: int
        data = await Player.find(
            Player.money >0,
            sort=[(Player.money, -1)]
            ).to_list()

Unable to assign argument of type “list[tuple[int, Literal[-1]]]” to parameter “sort” of type “str | List[Tuple[str, SortDirection]] | None” in “find” function “int” is not compatible with “str” “Literal[-1]” is not compatible with “SortDirection” Pylance

class Player(Document):
    money: int
        data = await Player.find(
            Player.money >0).sort(-Player.money).to_list()

Cannot assign argument of type "int" to parameter "args" of type "str | Tuple[str, SortDirection] | List[Tuple[str, SortDirection]] | None” in “sort” function Cannot assign type 'int' to type 'str | Tuple[str, SortDirection] | List[Tuple[str, SortDirection]] | None » “int” is not compatible with “str” “int” is not compatible with “Tuple[str, SortDirection]” “int” is not compatible with “List[Tuple[str, SortDirection]]” Type cannot be assigned to type “None” Pylance

HazyFish commented 4 days ago

Any plans to address the typing issues?

CAPITAINMARVEL commented 4 days ago

Any plans to address the typing issues?

https://github.com/BeanieODM/beanie/pull/929 i fixed it there no idea if its gonna be approved