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

create_new_manager_class_from_as_manager_method : TypeVar QuerySet does not seems to work. #1646

Closed
MoranAbadieGatewatcher opened this issue Aug 9, 2023 · 0 comments · Fixed by #1666
Labels
bug Something isn't working

Comments

@MoranAbadieGatewatcher
Copy link

MoranAbadieGatewatcher commented Aug 9, 2023

Bug report

I have the following code (this is a simplified version of my problem):

from typing import TypeVar, TYPE_CHECKING
from typing_extensions import Self

from django.db import models

_CTE = TypeVar("_CTE", bound=models.Model)

class _MyModelQuerySet(models.QuerySet[_CTE]):

    if TYPE_CHECKING:
        def example(self) -> _CTE: ...
    
    def test(self) -> "Self":
        return self

class MyModelQuerySet(_MyModelQuerySet["MyModel"]):
    pass

class MyModel(models.Model):
    objects = MyModelQuerySet.as_manager()


def use_my_model():
    foo: MyModel = MyModel.objects.example() # error (1)
    foo2: int = MyModelQuerySet().example()  # error (2)
    MyModel.objects.test().example()   # error (3)

I have the mypy report :

(1) error: Incompatible types in assignment (expression has type "_CTE", variable has type "MyModel") [assignment]
(2) error: Incompatible types in assignment (expression has type "MyModel", variable has type "int") [assignment]
(3) error: <nothing> has no attribute "example"
The (2) is right, but the (1) is wrong.

Moreover Self types seems not to work (with (3))

What's wrong

It seems that when ManagerFromMyModelQuerySet is created, it does not work with a TypeVar, nor Self type.

System information

OS: Ubuntu
python version: 3.9.10
django version: 3.2.19
mypy version: 1.4.1
django-stubs version: 4.2.3
django-stubs-ext version: 4.2.2
@MoranAbadieGatewatcher MoranAbadieGatewatcher added the bug Something isn't working label Aug 9, 2023
@MoranAbadieGatewatcher MoranAbadieGatewatcher changed the title as_manager, TypeVar QuerySet does not semms to work. create_new_manager_class_from_as_manager_method : TypeVar QuerySet does not seems to work. Aug 9, 2023
moranabadie added a commit to moranabadie/django-stubs that referenced this issue Aug 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

Successfully merging a pull request may close this issue.

1 participant