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

mypy INTERNAL ERROR: maximum semantic analysis iteration count reached #8760

Closed
1 task done
brianmedigate opened this issue Feb 8, 2024 · 7 comments · Fixed by #9008
Closed
1 task done

mypy INTERNAL ERROR: maximum semantic analysis iteration count reached #8760

brianmedigate opened this issue Feb 8, 2024 · 7 comments · Fixed by #9008
Labels
awaiting author response awaiting response from issue opener bug V2 Bug related to Pydantic V2 duplicate

Comments

@brianmedigate
Copy link
Contributor

brianmedigate commented Feb 8, 2024

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

The code below causes mypy to crash with INTERNAL ERROR: maximum semantic analysis iteration count reached.
It reproduces on pydantic 2.6.0 with the pydantic.mypy plugin enabled.
It does not reproduce on pydantic 2.5.3 or without the plugin.

The minimal reproducible example below is a bit strange and arbitrary. I have distilled it from a real codebase. It is not clear at all how the parts come together to cause the behavior, but almost any significant change to the code will solve the problem:

  • if the pydantic field isn't Optional
  • if the pydantic model isn't subclassed
  • if the NamedTuple doesn't have a method

Example Code

from typing import NamedTuple, Optional

from pydantic import BaseModel


class A(BaseModel):
    a: Optional[str]


class B(A):
    pass


class What(NamedTuple):
    foo: str

    def bar(self) -> None:
        pass

Python, Pydantic & OS Version

pydantic version: 2.6.0
        pydantic-core version: 2.16.1
          pydantic-core build: profile=release pgo=true
                 install path: /home/brianm/repos/medigator/venv/lib/python3.8/site-packages/pydantic
               python version: 3.8.18 (default, Aug 25 2023, 13:20:30)  [GCC 11.4.0]
                     platform: Linux-6.1.0-1029-oem-x86_64-with-glibc2.35
             related packages: email-validator-2.1.0.post1 typing_extensions-4.9.0 fastapi-0.109.0 pydantic-settings-2.1.0 mypy-1.8.0
                       commit: unknown
@brianmedigate brianmedigate added bug V2 Bug related to Pydantic V2 pending Awaiting a response / confirmation labels Feb 8, 2024
@sydney-runkle
Copy link
Member

I believe this is a duplicate of #8665 which was resolved with #8666. That fix is now live with 2.6.1.

@sydney-runkle sydney-runkle added duplicate and removed pending Awaiting a response / confirmation labels Feb 8, 2024
@sydney-runkle
Copy link
Member

@brianmedigate,

Could you please check if you're still having this issue with 2.6.1? Thanks!

@sydney-runkle sydney-runkle added the awaiting author response awaiting response from issue opener label Feb 8, 2024
@brianmedigate
Copy link
Contributor Author

brianmedigate commented Feb 8, 2024

Replicates in 2.6.1, unfortunately

$ python -c "import pydantic.version; print(pydantic.version.version_info())"
             pydantic version: 2.6.1
        pydantic-core version: 2.16.2
          pydantic-core build: profile=release pgo=true
                 install path: .../venv/lib/python3.8/site-packages/pydantic
               python version: 3.8.18 (default, Aug 25 2023, 13:20:30)  [GCC 11.4.0]
                     platform: Linux-6.1.0-1029-oem-x86_64-with-glibc2.35
             related packages: email-validator-2.1.0.post1 typing_extensions-4.9.0 fastapi-0.109.0 pydantic-settings-2.1.0 mypy-1.8.0
                       commit: unknown

nsoranzo added a commit to galaxybot/galaxy that referenced this issue Feb 12, 2024
xref: pydantic/pydantic#8760

Fix the following error when testing with mypy:

```
$ tox -e mypy
Deferral trace:
    cwltool.utils:62
    cwltool.utils:-1
    ...
    galaxy.model:666
    galaxy.model:666
    galaxy.model:666
    galaxy.model:666
lib/galaxy/model/__init__.py: error: INTERNAL ERROR: maximum semantic analysis
iteration count reached
Found 1 error in 1 file (errors prevented further checking)
```
nsoranzo added a commit to galaxybot/galaxy that referenced this issue Feb 14, 2024
xref: pydantic/pydantic#8760

Fix the following error when testing with mypy:

```
$ tox -e mypy
Deferral trace:
    cwltool.utils:62
    cwltool.utils:-1
    ...
    galaxy.model:666
    galaxy.model:666
    galaxy.model:666
    galaxy.model:666
lib/galaxy/model/__init__.py: error: INTERNAL ERROR: maximum semantic analysis
iteration count reached
Found 1 error in 1 file (errors prevented further checking)
```
@alexgaspard
Copy link

alexgaspard commented Feb 27, 2024

I have this issue as well with this sample:

from typing import Annotated, Optional, Union

from pydantic import BaseModel, Field, field_validator, model_validator


class Origin(BaseModel):
    x: Union[str, None]


def hello() -> None:
    class Test(Origin):

        @field_validator("x")
        @classmethod
        def validate_x(cls, value: Union[str, None]) -> Union[str, None]:
            if value is not None and value == "":
                raise ValueError("Not allowed")
            return value

mypy returns error: INTERNAL ERROR: maximum semantic analysis iteration count reached

I'm using python3.9 and have these versions installed:

mypy                         1.8.0
mypy-extensions              1.0.0
pydantic                     2.6.1
pydantic_core                2.16.2
pydantic-settings            2.1.0

@Cielquan
Copy link

I have this issue as well with the same mypy error. The issue starts to occur with version 2.6.0b1.

Minimal repro

This repro is a stripped down version of a config file from a real application. The config file uses pydantic_settings but the issue also occurs if I replace BaseSettings with BaseModel, so I guess it's an issue with pydantic.

"""Config for Application."""
from __future__ import annotations

import typing as t

# from pydantic_settings import BaseSettings
from pydantic import BaseModel as BaseSettings


class CustomBaseSettings(BaseSettings):
    """Custom base settings class with added functionality and set base config."""


class MySQLSettings(CustomBaseSettings):
    """MySQL settings with partial defaults."""

    DB_MYSQL_PASSWORD: str | None


class Settings(t.Protocol):
    """Application Settings protocol."""

    DB_MYSQL_SETTINGS: MySQLSettings | None


class _SettingsBase(CustomBaseSettings):
    """Shareable base settings."""

    DB_MYSQL_SETTINGS: MySQLSettings | None = None


def get_prod_settings() -> Settings:
    """Return cached Production settings."""

    class ProdMySQLSettings(MySQLSettings):
        """Production MySQL settings."""

    class ProdSettings(_SettingsBase):
        """Production application settings."""

        DB_MYSQL_SETTINGS: MySQLSettings | None = ProdMySQLSettings()

    return ProdSettings()

Versions

Python:

$ python3 -VV
Python 3.11.7 (main, Dec  8 2023, 18:56:58) [GCC 11.4.0]

Dependencies:

pydantic==2.6.4
pydantic-settings==2.2.1
pydantic_core==2.16.3
mypy==1.9.0
mypy-extensions==1.0.0

Mypy Config

Mypy config in pyproject.toml:

[tool.mypy]
python_version = "3.11"
mypy_path = "mypy_stubs"
follow_imports = "silent"
disallow_any_generics = true
disallow_subclassing_any = true
disallow_untyped_defs = true
check_untyped_defs = true
strict_optional = true
warn_redundant_casts = true
warn_unused_ignores = true
warn_return_any = true
warn_unreachable = true
implicit_reexport = false
show_error_context = true
show_column_numbers = true
plugins = ["pydantic.mypy"]

[tool.pydantic-mypy]
init_forbid_extra = true
init_typed = false
warn_required_dynamic_aliases = true

@dmontagu
Copy link
Contributor

dmontagu commented Mar 17, 2024

I believe this is all fixed by #9008, which should be included in the next release.

@Cielquan
Copy link

I just tested against main at commit a3b7214 and can confirm the issue is gone for my example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting author response awaiting response from issue opener bug V2 Bug related to Pydantic V2 duplicate
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants