mypy returning module 'X' has no attribute 'MyClass' with django rest framework

594 views Asked by At

I have the following django rest framework setup

app/models.py:

from django.db import models
class A(models.Model):
    x = models.CharField(max_length=8)

app/serializer.py:

from .models import A
from rest_framework import serializers
class ASerializer(serializers.ModelSerializer):
    class Meta: 
        model = A
        fields = "__all__"

I have configured mypy to run as a pre-commit hook with the following settings:

-   repo: https://github.com/pre-commit/mirrors-mypy
    rev: v0.670  # Use the sha / tag you want to point at
    hooks:
    - id: mypy

however mypy (0.67) returns the following error: serializer.py: error: Module 'app.models' has no attribute 'A'

If I run mypy on the command line after install the stubs for django and django rest_framework mypy runs fine and no errors are reported:

mypy.ini

[mypy]
plugins = mypy_django_plugin.main, mypy_drf_plugin.main


pip install django-stubs
pip install djangorestframework-stubs
mypy app/serializers.py

how can I resolve this error which seems to be only related to running via the pre-commit hook?

0

There are 0 answers