fix: incorrect returned type of access descriptors on unions of types by md384 · Pull Request #16604 · python/mypy

This PR fixed and revealed a new error with typing django models w/django-stubs.
Unsure whether to post here or on django-stubs so i'll start here on this PR:

Both Invoice and GroupInvoice here are django Models being pointed to from a third model, SalesDocument.

class SalesDocument(models.Model):
    invoice = models.OneToOneField(Invoice, on_delete=models.PROTECT, related_name='sales_document')
    group_invoice = models.OneToOneField(GroupInvoice, on_delete=models.PROTECT, related_name='sales_document')

But the following code errors out:

def foo(invoice: Invoice | GroupInvoice) -> bytes:
    invoice.sales_document  # type: ignore[call-overload]

After this patch, mypy is able to reveal the type of invoice.sales_document correctly as SalesDocument,
but errors out on the type of the overloaded sales_document method that django-stubs makes on both Invoice and GroupInvoice.

.../invoice.py:246: error: No overload variant of "__get__" of "ReverseOneToOneDescriptor" matches argument types "GroupInvoice", "type[GroupInvoice]"  [call-overload]
.../invoice.py:246: note: Possible overload variants:
.../invoice.py:246: note:     def __get__(self, instance: None, cls: Any = ...) -> ReverseOneToOneDescriptor[Invoice, SalesDocument]
.../invoice.py:246: note:     def __get__(self, instance: Invoice, cls: Any = ...) -> SalesDocument