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

[5.0.3 regression] GenericRelation has no attribute #2299

Open
andersk opened this issue Jul 31, 2024 · 2 comments
Open

[5.0.3 regression] GenericRelation has no attribute #2299

andersk opened this issue Jul 31, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@andersk
Copy link
Contributor

andersk commented Jul 31, 2024

In 5.0.3, the value of a GenericRelation field is given the useless type _ST (an unbound type variable), and trying to do anything with it gives errors like error: "_ST" has no attribute "all" [attr-defined].

This was introduced with b2b1afa (#2261, cc @rafonseca).

class GenericRelation(ForeignObject):

ForeignObject is generic and requires generic arguments. Without these generic arguments, it was previously interpreted as ForeignObject[Any, Any] but now it’s interpreted as the invalid ForeignObject[Any, _ST].

# __set__ value type
_ST = TypeVar("_ST")
# __get__ return type
_GT = TypeVar("_GT", default=_ST)
class RelatedField(FieldCacheMixin, Field[_ST, _GT]):

Complete test case

pyproject.toml

[tool.django-stubs]
django_settings_module = "my_settings"

[tool.mypy]
plugins = ["mypy_django_plugin.main"]

my_settings.py

INSTALLED_APPS = ["django.contrib.contenttypes", "my_app"]

my_app/__init__.py

# empty

my_app/models.py

from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
from django.contrib.contenttypes.models import ContentType
from django.db import models

class TaggedItem(models.Model):
    tag = models.SlugField()
    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey("content_type", "object_id")

class Bookmark(models.Model):
    url = models.URLField()
    tags = GenericRelation(TaggedItem)

def test(b: Bookmark) -> None:
    b.tags.all()  # error: "_ST" has no attribute "all"  [attr-defined]
$ mypy .
my_app/models.py:16: error: "_ST" has no attribute "all"  [attr-defined]
Found 1 error in 1 file (checked 3 source files)
@andersk andersk added the bug Something isn't working label Jul 31, 2024
sobolevn added a commit that referenced this issue Jul 31, 2024
This is not a fix, but a workaround for #2299

We would need to add proper generic type params later.
@sobolevn
Copy link
Member

This is a simple workaround: #2300
PR with a proper generic type is welcome!

sobolevn added a commit that referenced this issue Jul 31, 2024
This is not a fix, but a workaround for #2299

We would need to add proper generic type params later.
@rafonseca
Copy link
Contributor

Hi @andersk ,
Sorry for that, I'm using pyright. This should get solved when mypy implements PEP696

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

3 participants