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

Accessing .objects on variable typed type[Model] gives error: type[Model]" has no attribute "objects" #1685

Closed
richardebeling opened this issue Sep 5, 2023 · 5 comments
Labels
bug Something isn't working duplicate This issue or pull request already exists

Comments

@richardebeling
Copy link
Contributor

After updating django-stubs from 4.2.3 to 4.2.4, this code:

from django.db.models import Model

def test(SomeModel: type[Model]):
    SomeModel.objects.all()

produces this output when running mypy test.py:

test.py:4: error: "type[Model]" has no attribute "objects"  [attr-defined]

It passed typechecking with 4.2.3. According to django's documentation on Model attributes, objects should be accessible on models unless the model has a custom manager.

The release notes for 4.2.4 don't indicate to me that this was changed. Was this change intentional? If yes, how should I annotate test above to make clear that it expects some model class with the default objects manager attribute?

System information

  • OS: ubuntu 22.04
  • python version: 3.10.12
  • django version: 4.2.3
  • mypy version: 1.4.1
  • django-stubs version: 4.2.4
  • django-stubs-ext version: 4.2.2
@richardebeling richardebeling added the bug Something isn't working label Sep 5, 2023
@richardebeling richardebeling changed the title Accessing .objects on variable typed type[Model] gives error: "type[LoggedModel]" has no attribute "objects" Accessing .objects on variable typed type[Model] gives error: type[LoggedModel]" has no attribute "objects" Sep 5, 2023
@flaeppe
Copy link
Member

flaeppe commented Sep 5, 2023

Duplicate of #1684

@flaeppe flaeppe closed this as completed Sep 5, 2023
@flaeppe flaeppe added the duplicate This issue or pull request already exists label Sep 5, 2023
@richardebeling
Copy link
Contributor Author

richardebeling commented Sep 5, 2023

Ah, thanks. I'd argue the release notes should mention this as a possibly breaking change, since code that passed type checking before now fails. django-stubs 4.2.4 has this bullet point in the release notes:

We also update our stubs to be compatible with Django 4.2.5 (and all prior versions of 4.2).

but django doesn't seem to have changed their policy regarding .objects in the 4.1 -> 4.2 update, so imho the bullet point doesn't "justify" higher strictness regarding .objects on models.

What do you think?

@flaeppe
Copy link
Member

flaeppe commented Sep 5, 2023

Yeah maybe we should've been more clear in the release notes.

I think the bullet point aims for all prior 4.x versions of Django, not django-stubs? Not sure I follow otherwise

I'd argue that this improved alignment with the runtime justifies a breaking change. I'd say the new version just found a bug.

And I don't think the behavior regarding objects is anything new for the 4.x series. But perhaps we've found an ambiguity in the Django documentation?

@richardebeling richardebeling changed the title Accessing .objects on variable typed type[Model] gives error: type[LoggedModel]" has no attribute "objects" Accessing .objects on variable typed type[Model] gives error: type[Model]" has no attribute "objects" Sep 5, 2023
@richardebeling
Copy link
Contributor Author

I see how my last post was a bit ambiguous, I edited it to make it a bit clearer.

In the end, I'm only suggesting to add an additional bullet point in the release notes, something along the lines of:

  • We no longer assume the objects attribute to be present on generic Model classes. As per the django documentation, code working with generic models should use the _default_manager attribute instead.

@sobolevn
Copy link
Member

sobolevn commented Sep 5, 2023

Done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working duplicate This issue or pull request already exists
Development

No branches or pull requests

3 participants