typeddjango / django-stubs

PEP-484 stubs for Django
MIT License
1.56k stars 433 forks source link

NestedObjects.nested should return a more complex type #1985

Open DanieleIsoni opened 6 months ago

DanieleIsoni commented 6 months ago

Bug report

What's wrong

https://github.com/typeddjango/django-stubs/blob/9880684a36f70e61124115a296e34cd0df15e3f1/django-stubs/contrib/admin/utils.pyi#L57 In the code above, the nested method returns a simple list[Model], while it is a bit more complex than that.

How is that should be

It should return something like list[RecursiveModelList] with RecursiveModelList being:

RecursiveModelList: TypeAlias = Model | list['RecursiveModelList']

How to reproduce

You can verify by running the following and checking what's in collected_instances:

class Author(Model):
    name = CharField(max_length=100)

class BooksCollection(Model):
    name = CharField(max_length=100)
    author = ForeignKey(Author, on_delete=CASCADE)

class Book(Model):
    name = CharField(max_length=100)
    collection = ForeignKey(BooksCollection, on_delete=CASCADE)

author = Author.objects.create(name="Author")
collection = BooksCollection.objects.create(name="Author's life adventures", author=author)
book = Book.objects.create(name="Author's childhood", collection=collection)

collector = NestedObjects(using="default")
collector.collect([author])
collected_instances = collector.nested()

System information

sobolevn commented 6 months ago

Or list[Any] (if recursion is problematic).