Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

INTERNAL ERROR: maximum semantic analysis iteration count reached Found 1 error in 1 file (errors prevented further checking) #18237

Open
Ryang20718 opened this issue Dec 3, 2024 · 3 comments
Labels
crash semantic-analyzer Problems that happen during semantic analysis

Comments

@Ryang20718
Copy link

Ryang20718 commented Dec 3, 2024

Mypy is crashing in this example (currently works with 1.0.9)
Crash Report

mypy --show-error-codes --show-column-numbers  --show-traceback example/implementation.py
Deferral trace:
    example.implementation:-1
    example.implementation:-1
    example.implementation:-1
    example.implementation:-1
<snip>
    example.implementation:-1

To Reproduce

file to repro

from typing import NamedTuple

class CodecKey(NamedTuple):
    """A key object combines type and version."""

    @staticmethod
    def from_key_str(key: str) -> "CodecKey":
        """Return a CodecKey parsed from a key string.
        A codec key string has a format of f"{type_keytag}/{version}"
        """
        type_keytag, version = key.split("/")
        return CodecKey(type_keytag, version)

Your Environment

  • Mypy version used: 1.13
  • Mypy command-line flags: --show-traceback
  • Mypy configuration options from mypy.ini
[mypy]

[mypy-importlib.*]
follow_imports = skip
follow_imports_for_stubs = True

  • Python version used: 3.10.7
  • Operating system and version: ubuntu 20.04
@brianschubert
Copy link
Collaborator

Hmm, I can't seem reproduce a crash. Tested with mypy v1.13.0 (compiled) and master, Python 3.12.7.

Can you think of anything else that might help narrow this down? Also, can you try to reduce the reproducer to the minimum required code, config, and flags?

@Ryang20718
Copy link
Author

@brianschubert

apologies, I spun up a separate little repro in a virtual env with the most minimal repro

python version: 3.12.5
mypy version: 1.13.1
command line:

mypy.ini

[mypy]

[mypy-importlib.*]
follow_imports = skip
follow_imports_for_stubs = True

file that's causing errors

from typing import NamedTuple

class CodecKey(NamedTuple):
    """A key object combines type and version."""

    @staticmethod
    def from_key_str(key: str) -> "CodecKey":
        """Return a CodecKey parsed from a key string.
        A codec key string has a format of f"{type_keytag}/{version}"
        """
        type_keytag, version = key.split("/")
        return CodecKey(type_keytag, version)

@brianschubert
Copy link
Collaborator

brianschubert commented Dec 4, 2024

Thanks for the reduced repro! I can reproduce this on master. The necessary elements seems to be

  1. setting follow_imports = skip for importlib. This causes the semantic analyzer to defer on every (non-final) pass due to importlib.machinery.ModuleSpec being missing. There's already logic in place to fall back to builtins.object on the final pass; however the final pass is never reached (see below).
  2. a NamedTuple with a function definition. In this case, a new symbol table node is created for the function on every pass, causing the semantic analyzer to always "make progress". Since it makes progress on every pass, the final iteration is never reached.

The result is that the semantic analyzer always defers due to (1) and always makes progress due to (2). This leads to an infinite number of passes (until the max iteration logic kicks in).

@brianschubert brianschubert added the semantic-analyzer Problems that happen during semantic analysis label Dec 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crash semantic-analyzer Problems that happen during semantic analysis
Projects
None yet
Development

No branches or pull requests

2 participants