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

abstract Models don't have cls.objects #2514

Open
rrauenza opened this issue Feb 19, 2025 · 0 comments
Open

abstract Models don't have cls.objects #2514

rrauenza opened this issue Feb 19, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@rrauenza
Copy link

Bug report

A Model with

class Meta:
    abstract = True

won't have an objects field in the type annotations:

"type[Self]" has no attribute "objects" [attr-defined]

This is kind of a duplicate, but the other bug was specific to AbstractUser.

How is that should be

It kind of makes sense, since it is an abstract model -- but sometimes you are writing an abstract model that needs to reference self.objects as you write abstract functions for the future inheritors.

This does not seem to work as a workaround:

class Meta:                                                                           
          abstract = False if typing.TYPE_CHECKING else True    

This also does not seem to work:

   if not typing.TYPE_CHECKING:                                                          
          class Meta:                                                                       
              abstract = True                  

This IS a viable workaround it seems:

class MyModel(Model):
    class Meta: 
         abstract = True

   if typing.TYPE_CHECKING:                                                          
        objects: Manager

Since there is a workaround, no fix is necessarily needed, but wanted to document this here.

I'd argue, though, that an abstract model ought to have objects: Manager type annotated?

System information

  • OS:
  • python version: 3.12
  • django version: 3.2.25
  • mypy version: 1.15.0
  • django-stubs version: 5.1.3
  • django-stubs-ext version: 5.1.3
@rrauenza rrauenza added the bug Something isn't working label Feb 19, 2025
@rrauenza rrauenza changed the title abstract models don't have objects abstract Models don't have cls.objects Feb 19, 2025
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

No branches or pull requests

1 participant