Skip to content

Incorrect Manager type for child model's objects when parent has circular imports #32

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

Closed
syastrov opened this issue Feb 27, 2019 · 1 comment
Labels
bug Something isn't working

Comments

@syastrov
Copy link
Contributor

After running on an internal codebase, where we have a few places using inherited models, I encountered some issues in figuring out the correct Manager type for an inherited Model (which otherwise works great!), when there is an import cycle.

I created a relatively small testcase that demonstrates the problem:

[CASE managers_multiple_inheritance_multiple_files_import_cycle]
from django.db import models
from myapp.models import Child
reveal_type(Child.objects) # E: Revealed type is 'django.db.models.manager.Manager[myapp.models.child.Child]'
Child.objects.create(c=True, p="abc", other_id=123)

[file myapp/__init__.py]
[file myapp/models/__init__.py]
from .other import Other
from .parent import Parent
from .child import Child

[file myapp/models/other.py]
from django.db import models
class Other(models.Model):
    o = models.CharField(max_length=255)

[file myapp/models/parent.py]
from django.db import models
from myapp.models import Other
# Changing the above line to the following fixes around the problem:
#from myapp.models.other import Other
class Parent(models.Model):
    p = models.CharField(max_length=255)
    other = models.ForeignKey(Other, on_delete=models.CASCADE)

[file myapp/models/child.py]
from django.db import models
from myapp.models import Parent
class Child(Parent):
    c = models.BooleanField(default=True)

reveal_type(Child.objects) gives: Revealed type is 'django.db.models.manager.Manager[myapp.models.parent.Parent]'
But it should be django.db.models.manager.Manager[myapp.models.child.Child].

If I change the line from myapp.models import Other to from myapp.models.other import Other in myapp/models/parent.py, then it behaves as expected, so there is a fairly easy workaround.

I don't know if this is something to do with mypy itself or how this plugin works.

@TonyRippy TonyRippy mentioned this issue Dec 20, 2021
@intgr intgr added the bug Something isn't working label Nov 11, 2022
@flaeppe
Copy link
Member

flaeppe commented Sep 24, 2023

I can't reproduce this, feel free to reopen with a breaking case

@flaeppe flaeppe closed this as completed Sep 24, 2023
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

3 participants