adamchainz / django-mysql

:dolphin: :horse: Extensions to Django for use with MySQL/MariaDB
https://django-mysql.readthedocs.io/
MIT License
568 stars 110 forks source link

Bad type annotation on GroupConcat `expression: Expression` #1054

Open anentropic opened 9 months ago

anentropic commented 9 months ago

Python Version

3.11

Django Version

4.2

MariaDB/MySQL Version

MySQL 8.0

Package Version

latest

Description

First arg to constructor is currently annotated as expression: Expression:

https://github.com/adamchainz/django-mysql/blob/main/src/django_mysql/models/aggregates.py#L27-L37

Example usage code in docs is:

author = Author.objects.annotate(book_ids=GroupConcat("books__id")).get(
    name="William Shakespeare"
)

"books__id" is a str and not an Expression, giving type errors when using as intended

also F("books_id") would not pass either, as F does not inherit from Expression (they are both instances of Combinable)

https://github.com/django/django/blob/4.2/django/db/models/expressions.py#L474

adamchainz commented 7 months ago

Correct. It looks like the annotation should probably be just Any, as per the superclass: https://github.com/typeddjango/django-stubs/blob/2cb9c832e97ea5a73d11596696effaab065dff94/django-stubs/db/models/aggregates.pyi#L11 . Do you agree? If so, could you open a PR?