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: Incorrect typing in SQLAlchemyAsyncRepositoryReadService #261

Closed
1 of 4 tasks
vikigenius opened this issue Oct 4, 2024 · 0 comments · Fixed by #342
Closed
1 of 4 tasks

Bug: Incorrect typing in SQLAlchemyAsyncRepositoryReadService #261

vikigenius opened this issue Oct 4, 2024 · 0 comments · Fixed by #342
Labels
accepted Change or enhancement is accepted for development. bug Something isn't working help wanted Extra attention is needed

Comments

@vikigenius
Copy link
Contributor

vikigenius commented Oct 4, 2024

Description

class SQLAlchemyAsyncRepositoryReadService(Generic[ModelT], ResultConverter):
    """Service object that operates on a repository object."""

    repository_type: type[SQLAlchemyAsyncRepositoryProtocol[ModelT] | SQLAlchemyAsyncSlugRepositoryProtocol[ModelT]]

Firstly repository_type is uninitialized. This automatically raises an issue if strict.

But more importantly when you do something like this: Which happens in the litestar fullstack example

class UserService(SQLAlchemyAsyncRepositoryService[User]):
    """Handles database operations for users."""

    repository_type = UserRepository

And then try to do

self.repository: UserRepository = self.repository_type(**repo_kwargs)

Typechecker complains that Type "SQLAlchemyAsyncRepositoryProtocol[User] | SQLAlchemyAsyncSlugRepositoryProtocol[User]" is not assignable to type "UserRepository"

URL to code causing the issue

No response

MCVE

This is easier to see with a completely synthetic example

class ClassA:
    """Class A."""

class ClassB:
    """Class B."""


class ClassSA(ClassA):
    """Class SA"""

class ClassR:
    """Class R."""
    ctype: type[ClassA] | type[ClassB] = ClassA


class ClassSAR(ClassR):
    ctype = ClassSA

    def __init__(self) -> None:
        super().__init__()
        self.c: ClassSA = self.ctype()

The type checker complains that Type "ClassA | ClassB" is not assignable to type "ClassSA"

This might be a bug in pyright. But I think the behavior does make sense because at runtime ctype() could still return ClassB. So a Cast is needed

Steps to reproduce

Run the MCVE

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)
@vikigenius vikigenius added the bug Something isn't working label Oct 4, 2024
@cofin cofin added accepted Change or enhancement is accepted for development. help wanted Extra attention is needed labels Jan 10, 2025
@cofin cofin linked a pull request Jan 15, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted Change or enhancement is accepted for development. bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants