python / python/mypy

INTERNAL ERROR: maximum semantic analysis iteration count reached when NamedTuple class with methods and MoneyField exist in the same models.py

Open
#15,342 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

crash
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Crash Report

Hello Mypy team,

I've encountered a peculiar issue when running mypy on my Django project. The issue seems to be related to the interaction between mypy, NamedTuple classes with methods, and the MoneyField from django-money library.

Specifically, I have a NamedTuple class that includes a method, and a Django model that includes a MoneyField. When both of these components are included in the same models.py file, mypy reports an error. However, if I comment out the method in the NamedTuple class, mypy does not report an error.

Here is what the trace looks like :

mypy installed: asgiref==3.6.0,Babel==2.11.0,certifi==2022.12.7,cffi==1.15.1,charset-normalizer==3.0.1,contextlib2==21.6.0,cryptography==39.0.1,defusedxml==0.7.1,dj-rest-auth==2.2.7,Django==4.1.7,django-allauth==0.52.0,django-cors-headers==3.13.0,django-dirtyfields==1.9.1,django-environ==0.9.0,django-formtools==2.4.1,django-money==3.1.0,django-ninja==0.20.0,django-ninja-apikey==0.1.0,django-ninja-extra==0.18.0,django-ninja-jwt==5.2.2,django-safedelete @ git+https://github.com/sebastian-philipp/django-safedelete.git@9475d77c6dcced73683a4548d208935fed6c96d8,django-salesforce==4.1,django-stubs==1.13.1,django-stubs-ext==0.7.0,django-waffle==3.0.0,djangorestframework==3.14.0,djangorestframework-simplejwt==5.2.2,djangorestframework-stubs==1.8.0,dnspython==2.3.0,email-validator==1.3.0,idna==3.4,injector==0.19.0,mypy==1.3.0,mypy-extensions==1.0.0,ninja-schema==0.13.0,oauthlib==3.2.2,-e git+https://gitlab.com/interdotlink/group-titan/titan.git@3fe6fcd97165b8d4b846d2470aeddfa0707482b7#egg=portal_common&subdirectory=portal-common,psycopg2-binary==2.9.5,py-moneyed==2.0,pycparser==2.21,pydantic==1.10.4,pydantic-to-typescript==1.0.10,PyJWT==2.6.0,python3-openid==3.2.0,pytz==2022.7.1,PyYAML==6.0,requests==2.28.2,requests-mock==1.10.0,requests-oauthlib==1.3.1,sentry-sdk==1.14.0,six==1.16.0,sqlparse==0.4.3,tomli==2.0.1,types-pytz==2022.7.1.0,types-PyYAML==6.0.12.3,types-requests==2.28.11.8,types-urllib3==1.26.25.4,typing_extensions==4.4.0,urllib3==1.26.14
mypy run-test-pre: PYTHONHASHSEED='3500358902'
mypy run-test: commands[0] | mypy --show-traceback --config-file ../mypy-django.ini .
Deferral trace:
    ninja_extra.controllers.registry:4
    ninja_extra.controllers.registry:-1
    ninja_extra.controllers.registry:8
    ninja_extra.controllers.registry:8
    django_stubs_ext.aliases:4
    django_stubs_ext.aliases:-1
    django_stubs_ext.aliases:4
    django_stubs_ext.aliases:-1
    django_stubs_ext.aliases:4
    django_stubs_ext.aliases:-1
    django_stubs_ext.aliases:5
    django_stubs_ext.aliases:-1
    django_stubs_ext.aliases:6
    django_stubs_ext.aliases:-1
    django_stubs_ext.aliases:8
    django_stubs_ext.aliases:8
    django_stubs_ext.aliases:8
    django_stubs_ext.aliases:8
    django_stubs_ext.aliases:-1
    ninja.params:56
    ninja.params:56
    ninja.params:-1
    ninja.params:60
    ninja.params:60
...
/home/zalshattle/Documents/.../products.py: error: INTERNAL ERROR: maximum semantic analysis iteration count reached
Found 1 error in 1 file (errors prevented further checking)
ERROR: InvocationError for command /home/zalshattle/... --show-traceback --config-file ../mypy-django.ini . (exited with code 2)

I have to remove some info but the trace basically lists every single file I have in my project (a few hundred lines)

To Reproduce

Here's a minimal example that reproduces the issue on my project:

from typing import NamedTuple
from djmoney.models.fields import MoneyField
from django.db import models

class PricingTuple(NamedTuple):
    mrc: 0
    nrc: 0

    def multiply(self):
        return None

class MyModel(models.Model):
    money = MoneyField(max_digits=10, decimal_places=2, default_currency='USD')

If I have this in my project, then mypy will crash. If I remove either the method (def multiply) or the MoneyField, then the issue doesn't happen. What is interesting is that the issue occurs in a compeletely unrelated file which has neither any mention of MoneyField, nor NamedTuples.

I've tried to isolate the issue by testing this code in a fresh Django project, but I was not able to reproduce the error in that context. This suggests that the issue might be related to some specific configuration or aspect of my original Django project, but I'm not sure what that could be.

I would appreciate any insight you can provide on this issue. Thank you for your time and help!

Your Environment

  • Mypy version used: 1.3.0
  • Mypy command-line flags: mypy --show-traceback --config-file ../mypy-django.ini .
  • Mypy configuration options from mypy.ini (and other config files):
[mypy]
strict_optional = True
no_implicit_optional = True
warn_incomplete_stub = True
check_untyped_defs = True
show_error_context = True
plugins = mypy_django_plugin.main, pydantic.mypy
disallow_untyped_defs = True

[mypy.plugins.django-stubs]
django_settings_module = "***"


[mypy-***.tests]
disallow_untyped_defs = False


[mypy-.....*]
ignore_missing_imports = True

[mypy-djmoney.*]
ignore_missing_imports = True
  • Python version used: 3.10.6
  • Operating system and version: Pop OS 22.04 LTS

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the minimal models.py example containing PricingTuple, its multiply method, and MoneyField, then run the reported mypy 1.3.0 command with the shown plugin configuration. Compare behavior when either component is removed and isolate the project configuration or dependency interaction that triggers the semantic-analysis iteration limit; done means the reproducer no longer reaches the internal error and the regression is covered.

Written by the indexing model from the issue text.

Assessment

Tech stack
django, python
Domain
compilers, devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.