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

NestedObjects.nested should return a more complex type #1985

Open
DanieleIsoni opened this issue Feb 29, 2024 · 1 comment
Open

NestedObjects.nested should return a more complex type #1985

DanieleIsoni opened this issue Feb 29, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@DanieleIsoni
Copy link

Bug report

What's wrong

def nested(self, format_callback: None = None) -> list[Model]: ...

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

  • OS: MacOS
  • python version: 2.11.2
  • django version: 4.2.10
  • mypy version: 1.8.0
  • django-stubs version: 4.2.7
  • django-stubs-ext version: 4.2.7
@DanieleIsoni DanieleIsoni added the bug Something isn't working label Feb 29, 2024
@sobolevn
Copy link
Member

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

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

2 participants