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] optional link type field in the form of union type expression (Link[A] | None bitwise or operation between link type and None) can not be recognized. #951

Closed
IterableTrucks opened this issue Jun 17, 2024 · 2 comments
Labels

Comments

@IterableTrucks
Copy link
Contributor

Describe the bug
Python 3.10 introduced a new union type expression with | operator, so Optional[Link[A]] is semantically equal to Link[A] | None. However in the latest version of beanie (v1.26.0), the latter form can not be recognized as a link field.

To Reproduce

import asyncio
from typing import Optional
from pydantic import Field
from beanie import Document, Link, init_beanie, BackLink


class Directory(Document):
    files: list[BackLink['File']] | None = Field(json_schema_extra={'original_field': 'directory'})
    name: str


class File(Document):
    name: str
    size: int
    directory: Link[Directory]


async def main():
    await init_beanie(connection_string="mongodb://localhost:27017/test", document_models=[Directory, File])
    assert 'files' in Directory.get_link_fields()


if __name__ == "__main__":
    asyncio.run(main())
import asyncio
from typing import Optional
from pydantic import Field
from beanie import Document, Link, init_beanie, BackLink


class Directory(Document):
    files: Optional[list[BackLink['File']]] = Field(json_schema_extra={'original_field': 'directory'})
    name: str


class File(Document):
    name: str
    size: int
    directory: Link[Directory]


async def main():
    await init_beanie(connection_string="mongodb://localhost:27017/test", document_models=[Directory, File])
    assert 'files' in Directory.get_link_fields()


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

Expected behavior
Both forms of optional list backlink can be recognized as link fields.

Additional context
beanie version is 1.26.0
Python version is 3.12.3

Copy link
Contributor

This issue is stale because it has been open 30 days with no activity.

@github-actions github-actions bot added the Stale label Jul 18, 2024
Copy link
Contributor

github-actions bot commented Aug 1, 2024

This issue was closed because it has been stalled for 14 days with no activity.

@github-actions github-actions bot closed this as completed Aug 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant