Skip to content

Bug: Typing issues in ModelDictT #265

Closed

Description

@vikigenius

Description

pyright complains when I try to assign DTOData[User] to ModelDictT[User]

/home/void/Projects/Python/litestartest/src/litestartest/controller.py
  /home/void/Projects/Python/litestartest/src/litestartest/controller.py:35:45 - error: Argument of type "DTOData[User]" cannot be assigned to parameter "data" of type "ModelDictT[User]" in function "create"
    Type "DTOData[User]" is incompatible with type "ModelDictT[User]"
      "DTOData[User]" is incompatible with "Dict[str, Any]"
      "DTOData[User]" is incompatible with "User"
      "DTOData[User]" is incompatible with "Struct"
      "DTOData[User]" is incompatible with protocol "Struct"
        "__struct_fields__" is not present
      "DTOData[User]" is incompatible with "BaseModel"
      "DTOData[User]" is incompatible with protocol "BaseModel"
    ... (reportArgumentType)
1 error, 0 warnings, 0 informations

You can't really see the error because pyright is supressing the last lines. But if I remove BaseModel from one of the union types you cans see the actual error

      "litestar.dto.data_structures.DTOData" is incompatible with "advanced_alchemy.service.typing.DTOData" (reportArgumentType)

You usually can't reproduce this error until you install pydantic. Because without pydantic installed in strict mode

you get

  /home/void/Projects/Python/litestartest/src/litestartest/service.py:10:42 - error: Type of "ModelDictT" is partially unknown
    Type of "ModelDictT" is "type[Dict[str, Any]] | type[ModelT@ModelDictT] | type[msgspec.Struct] | type[advanced_alchemy.service.typing.Struct] | Unknown | type[BaseModel] | type[DTOData[ModelT@ModelDictT]]" (reportUnknownVariableType)

Which suppresses the other issue.

I think in general the way ModelDictT is typed with try except does not actually seem to work properly.

URL to code causing the issue

https://github.com/vikigenius/litestartest/tree/main

MCVE

No response

Steps to reproduce

Run pyright or basedpyright on repository
Verify that reportUnknownVariableType is being produced in strict mode (because pydantic is not installed).

Install pydantic

Run pyright or basedpyright again.

This should cause the

/home/void/Projects/Python/litestartest/src/litestartest/controller.py
  /home/void/Projects/Python/litestartest/src/litestartest/controller.py:35:45 - error: Argument of type "DTOData[User]" cannot be assigned to parameter "data" of type "ModelDictT[User]" in function "create"

Screenshots

"In the format of: ![SCREENSHOT_DESCRIPTION](SCREENSHOT_LINK.png)"

Logs

No response

Package Version

0.20.0

Platform

  • Linux
  • Mac
  • Windows
  • Other (Please specify in the description above)

Activity

  1. cofin commented on Nov 2, 2024

    @cofin
    Member

    Closed by #277

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions