# Buggy code
def get_additional_deps(self, file: MypyFile) -> List[Tuple[int, str, int]]:
    # for settings
    if file.fullname() == "django.conf" and self.django_context.django_settings_module:
        return [self._new_dependency(self.django_context.django_settings_module)]

    # for values / values_list
    if file.fullname() == "django.db.models":
        return [self._new_dependency("mypy_extensions"), self._new_dependency("typing")]

    # for `get_user_model()`
    if self.django_context.settings:
        if file.fullname() == "django.contrib.auth" or file.fullname() in {
            "django.http",
            "django.http.request",
        }:
            auth_user_model_name = self.django_context.settings.AUTH_USER_MODEL
            try:
                auth_user_module = self.django_context.apps_registry.get_model(
                    auth_user_model_name
                ).__module__
            except LookupError:
                # get_user_model() model app is not installed
                return []
            return [self._new_dependency(auth_user_module)]

    # ensure that all mentioned to='someapp.SomeModel' are loaded with corresponding related Fields
    defined_model_classes = self.django_context.model_modules.get(file.fullname())
    if not defined_model_classes:
        return []
    deps = set()
    for model_class in defined_model_classes:
        # forward relations
        for field in self.django_context.get_model_fields(model_class):
            if isinstance(field, RelatedField):
                related_model_cls = self.django_context.get_field_related_model_cls(
                    field
                )
                related_model_module = related_model_cls.__module__
                if related_model_module != file.fullname():
                    deps.add(self._new_dependency(related_model_module))
        # reverse relations
        for relation in model_class._meta.related_objects:
            related_model_cls = self.django_context.get_field_related_model_cls(
                relation
            )
            related_model_module = related_model_cls.__module__
            if related_model_module != file.fullname():
                deps.add(self._new_dependency(related_model_module))
    return list(deps)


# Traceback (AttributeError)
Traceback (most recent call last):
File "/usr/local/bin/mypy", line 10, in <module>
sys.exit(console_entry())
File "/usr/local/lib/python3.6/site-packages/mypy/__main__.py", line 8, in console_entry
main(None, sys.stdout, sys.stderr)
File "/usr/local/lib/python3.6/site-packages/mypy/main.py", line 83, in main
res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
File "/usr/local/lib/python3.6/site-packages/mypy/build.py", line 164, in build
result = _build(sources, options, alt_lib_path, flush_errors, fscache, stdout, stderr)
File "/usr/local/lib/python3.6/site-packages/mypy/build.py", line 224, in _build
graph = dispatch(sources, manager, stdout)
File "/usr/local/lib/python3.6/site-packages/mypy/build.py", line 2526, in dispatch
graph = load_graph(sources, manager)
File "/usr/local/lib/python3.6/site-packages/mypy/build.py", line 2748, in load_graph
caller_state=st, caller_line=st.dep_line_map.get(dep, 1))
File "/usr/local/lib/python3.6/site-packages/mypy/build.py", line 1772, in __init__
self.compute_dependencies()
File "/usr/local/lib/python3.6/site-packages/mypy/build.py", line 2059, in compute_dependencies
self.manager.plugin.get_additional_deps(self.tree))
File "/usr/local/lib/python3.6/site-packages/mypy/plugin.py", line 743, in get_additional_deps
deps.extend(plugin.get_additional_deps(file))
File "/usr/local/lib/python3.6/site-packages/mypy_django_plugin/main.py", line 152, in get_additional_deps
related_model_module = related_model_cls.__module__
AttributeError: 'NoneType' object has no attribute '__module__'

# Fixed code
def get_additional_deps(self, file: MypyFile) -> List[Tuple[int, str, int]]:
    # for settings
    if file.fullname() == "django.conf" and self.django_context.django_settings_module:
        return [self._new_dependency(self.django_context.django_settings_module)]

    # for values / values_list
    if file.fullname() == "django.db.models":
        return [self._new_dependency("mypy_extensions"), self._new_dependency("typing")]

    # for `get_user_model()`
    if self.django_context.settings:
        if file.fullname() == "django.contrib.auth" or file.fullname() in {
            "django.http",
            "django.http.request",
        }:
            auth_user_model_name = self.django_context.settings.AUTH_USER_MODEL
            try:
                auth_user_module = self.django_context.apps_registry.get_model(
                    auth_user_model_name
                ).__module__
            except LookupError:
                # get_user_model() model app is not installed
                return []
            return [self._new_dependency(auth_user_module)]

    # ensure that all mentioned to='someapp.SomeModel' are loaded with corresponding related Fields
    defined_model_classes = self.django_context.model_modules.get(file.fullname())
    if not defined_model_classes:
        return []
    deps = set()
    for model_class in defined_model_classes:
        # forward relations
        for field in self.django_context.get_model_fields(model_class):
            if isinstance(field, RelatedField):
                related_model_cls = self.django_context.get_field_related_model_cls(
                    field
                )
                if related_model_cls is None:
                    continue
                related_model_module = related_model_cls.__module__
                if related_model_module != file.fullname():
                    deps.add(self._new_dependency(related_model_module))
        # reverse relations
        for relation in model_class._meta.related_objects:
            related_model_cls = self.django_context.get_field_related_model_cls(
                relation
            )
            related_model_module = related_model_cls.__module__
            if related_model_module != file.fullname():
                deps.add(self._new_dependency(related_model_module))
    return list(deps)