Skip to content

bulk_create "Self" Typing Error: Argument 1 to "bulk_create" of "Manager" has incompatible type "list[MyModel]"; expected "Iterable[Self]" #3401

@Myselfito

Description

@Myselfito

Bug report

The objects manager seems to be weirdly mistyping the required iterable for bulk_create.
A Self type is required (which is OK), but this Self type is conflicting with the class type.
See minimal reproduction below.

What's wrong

# models.py
from django.db import models

class MyModel(models.Model):
    pass
    
    @classmethod
    def test_bulk_create(cls) -> None:
        created = [cls()]
        cls.objects.bulk_create(created)  # error: Argument 1 to "bulk_create" of "Manager" has incompatible type "list[MyModel]"; expected "Iterable[Self]"  [arg-type]

How is that should be

The above code should not generate an error.

Using the "abstract model" mitigator _default_manager is working, but here the model is concrete (I tried with strict_model_abstract_attrs=false, which does not solve the issue).

The issue might be in the bounding of the objects attribute on subclasses (as it's deleted from the db/models/base.pyi Model stub).

System information

  • OS: Windows 11
  • python version: 3.11.9
  • django version: 5.2.14
  • mypy version: 2.0.0
  • django-stubs version: 6.0.4
  • django-stubs-ext version: 6.0.4
[tool.mypy]
plugins = ["mypy_django_plugin.main"]

[tool.django-stubs]
django_settings_module = "test_mypy_django.settings"

(I tried with django-stubs/subs-ext: 5.2.9 and mypy 1.19, same outcome)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions