typeddjango / django-stubs

PEP-484 stubs for Django
MIT License
1.58k stars 436 forks source link

Passing iterable of integers to lookup model by `ManyToManyField` throws error in mypy #2243

Open tbrlpld opened 3 months ago

tbrlpld commented 3 months ago

Bug report

What's wrong

I have the following models

# matches/models.py

class MatchGroup(models.Model):
    members = models.ManyToManyField(
        to="users.User",
        related_name="match_groups",
    )

# users/models.py

class User(auth_models.AbstractBaseUser, auth_models.PermissionsMixin):
    # Details here should not be relevant.
    ...

When I look up a group by its users like so

user_ids = (1, 2)
group = MatchGroup.objects.get(members=user_ids)

I get the following error from mypy:

Incompatible type for lookup 'members': (got "tuple[int, int]", expected "User | int | None")  [misc]

How is that should be

Passing an iterable of integers to the lookup should be ok and not generate errors, because Django can deal with it.

System information