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

Feature Request: Support django-polymorphic models in ManyToMany field #1836

Closed
khink opened this issue Nov 9, 2023 · 7 comments
Closed
Labels
3rd party Presence of a third party dependency has been mentioned bug Something isn't working

Comments

@khink
Copy link

khink commented Nov 9, 2023

Feature request

Sorry for filing this as a bug, i saw no other place to create a feature request.

And I thought posting here would make it easy for people with the same issue to find the reason.

What's wrong

When running mypy after upgrading to django-stubs[compatible-mypy]==4.2.6, i get error: Need type annotation for "articles".

Here, articles is defined as follows:

class ArticlePreset(models.Model):
    ...
    articles = models.ManyToManyField(Article, related_name="presets")

This is not due to the class being a string name as was the case in #1802.

We get four of these errors, and they are all for ManyToMany fields to the Article model.

The Article model is a polymorphic (https://github.com/django-polymorphic/django-polymorphic/) model:

class Article(PolymorphicModel):
    ...

This almost certainly is the reason for the error message. All other ManyToManyFields don't give this error.

How it should be

It would be nice if django-stubs could also support polymorphic models.

I realize that django-polymorphic has seen no activity in the last two years, and supporting it is not a priority. Also, I have no involvement with that project, we just use it (i wish we hadn't).

System information

  • OS: Ubuntu Linux
  • python version: 3.11.6
  • django version: 4.2.7
  • mypy version: 1.6.1
  • django-stubs version: 4.2.6
  • django-stubs-ext version: 4.2.5
@khink khink added the bug Something isn't working label Nov 9, 2023
@intgr
Copy link
Collaborator

intgr commented Nov 9, 2023

Looks unrelated, but we have another issue with django-polymorphic as well: #1158. There are 8 thumbs up reactions so it has a considerable number of users.

@intgr intgr changed the title Feature Request: Support polymorphic models in ManyToMany field Feature Request: Support django-polymorphic models in ManyToMany field Nov 9, 2023
@flaeppe flaeppe added the 3rd party Presence of a third party dependency has been mentioned label Nov 10, 2023
@flaeppe
Copy link
Member

flaeppe commented Nov 10, 2023

Since django-polymorphic doesn't export types. Could it be possible to conditionally declare the PolymorphicModel base?

e.g.

from django.db import models
from typing import TYPE_CHECKING

if TYPE_CHECKING:
    PolymorphicModel = models.Model
else:
    from xyz import PolymorphicModel

class Article(PolymorphicModel):
    ...

This wouldn't start showing any declarations from django-polymorphic but would set a default to interpret PolymorphicModel as a models.Model

@flaeppe
Copy link
Member

flaeppe commented Nov 10, 2023

This might be off topic, but I keep wondering if it's possible to use typing_extensions.Annotated to solve these kind of problems.

But I haven't gotten around to try it out myself. What I'm thinking about is doing:

from django.db import models
from typing_extensions import Annotated
from xyz import PolymorphicModel as _PolymorphicModel

PolymorphicModel = Annotated[models.Model, _PolymorphicModel]

class Article(PolymorphicModel):
    ...

@intgr
Copy link
Collaborator

intgr commented Nov 10, 2023

Anyway I think the "proper" solution would be to get type stubs for django-polymorphic? Either add them to upstream if they want it, or create a new django-polymorphic-stubs project. Would that be sufficient to fix these issues?

@flaeppe
Copy link
Member

flaeppe commented Nov 10, 2023

Yes, getting typing for django-polymorphic is what should resolve these issues

django-stubs can't really do anything here

@flaeppe
Copy link
Member

flaeppe commented Nov 16, 2023

I'm going to close this off with the reasoning that this is an issue for django-polymorphic and should be brought there

@flaeppe flaeppe closed this as completed Nov 16, 2023
@khink
Copy link
Author

khink commented Nov 16, 2023

Thanks for looking into this and suggesting workarounds!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3rd party Presence of a third party dependency has been mentioned bug Something isn't working
Development

No branches or pull requests

3 participants