Skip to content

fix(django-import-export): ForeignKeyWidget.__init__ argument types#15552

Merged
srittau merged 2 commits intopython:mainfrom
VictorColomb:fix/django-import-export/foreignkeywidget
Mar 24, 2026
Merged

fix(django-import-export): ForeignKeyWidget.__init__ argument types#15552
srittau merged 2 commits intopython:mainfrom
VictorColomb:fix/django-import-export/foreignkeywidget

Conversation

@VictorColomb
Copy link
Contributor

Issue statement

In the stubs for django-import-export, the model argument of the widgets.ForeignKeyWidget.__init__ method is incorrectly typed as _ModelT, which corresponds to a Django model instance, whereas the method actually expects the model itself, i.e. type[_ModelT].

MRE

from django.db.models import CASCADE, CharField, ForeignKey, Model
from import_export.resources import ModelResource
from import_export.widgets import ForeignKeyWidget


class RelatedModel(Model):
    label = CharField()


class MyModel(Model):
    related = ForeignKey(RelatedModel, on_delete=CASCADE)


class MyModelResource(ModelResource[MyModel]):
    related = ForeignKeyWidget(RelatedModel, field="label")

    class Meta:
        model = MyModel
        fields = ("related",)

The above code corresponds to the django-import-export documentation 1, and actually works. Yet:

$ mypy .
mre.py:15: error: Value of type variable "_ModelT" of "ForeignKeyWidget" cannot be "type[RelatedModel]"  [type-var]
Found 1 errors in 1 file (checked 1 source file)

Footnotes

  1. https://django-import-export.readthedocs.io/en/latest/api_widgets.html#import_export.widgets.ForeignKeyWidget

@github-actions

This comment has been minimized.

@github-actions
Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@srittau srittau merged commit b7ac9ee into python:main Mar 24, 2026
39 checks passed
@VictorColomb VictorColomb deleted the fix/django-import-export/foreignkeywidget branch March 25, 2026 08:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants