python / python/mypy

Inherit Pydantic BaseModel with optional generic attributes inside a function block

Open
#16,961 0 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
Mypy reports INTERNAL ERROR: maximum semantic analysis iteration count reached when defining a class that inherits a pydantic BaseModel with optional generic arguments inside a function block.

Traceback

Deferral trace:
    local_mypy_debug:16
    local_mypy_debug:16
    local_mypy_debug:16
    local_mypy_debug:16
    local_mypy_debug:16
    local_mypy_debug:16
    local_mypy_debug:16
    local_mypy_debug:16
    local_mypy_debug:16
    local_mypy_debug:16
    local_mypy_debug:16
    local_mypy_debug:16
    local_mypy_debug:16
    local_mypy_debug:16
    local_mypy_debug:16
    local_mypy_debug:16
    local_mypy_debug:16
    local_mypy_debug:16
    local_mypy_debug:16
Deferral trace:
    local_mypy_debug:21
    local_mypy_debug:21
    local_mypy_debug:21
    local_mypy_debug:21
    local_mypy_debug:21
    local_mypy_debug:21
    local_mypy_debug:21
    local_mypy_debug:21
    local_mypy_debug:21
    local_mypy_debug:21
    local_mypy_debug:21
    local_mypy_debug:21
    local_mypy_debug:21
    local_mypy_debug:21
    local_mypy_debug:21
    local_mypy_debug:21
    local_mypy_debug:21
    local_mypy_debug:21
    local_mypy_debug:21
local_mypy_debug.py: error: INTERNAL ERROR: maximum semantic analysis iteration count reached
Found 1 error in 1 file (errors prevented further checking)

To Reproduce

from pydantic import BaseModel
from typing import Optional, TypeVar, Generic

T = TypeVar("T")

class ClassWithOptionalGenericAttr(BaseModel, Generic[T]):
    attr: Optional[T]

# Case 1: This passes.
class B(ClassWithOptionalGenericAttr[T]):
    pass


# Case 2: Mypy: INTERNAL ERROR: maximum semantic analysis iteration count reached
def define_inside_a_func_with_generic() -> None:
    class B(ClassWithOptionalGenericAttr[T]):
        pass

# Case 3: Mypy: INTERNAL ERROR: maximum semantic analysis iteration count reached
def define_inside_a_func_with_type() -> None:
    class B(ClassWithOptionalGenericAttr[str]):
        pass


class ClassWithNonOptionalGenericAttr(BaseModel, Generic[T]):
    attr: T

# Case 4: This passes.
def define_inside_a_func_with_non_optional() -> None:
    class B(ClassWithNonOptionalGenericAttr[T]):
        pass

Your Environment

  • Mypy version used: mypy 1.8.0 (compiled: yes)
  • Mypy command-line flags: python -m mypy --show-traceback local_mypy_debug.py
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.9.15
  • Operating system and version: macOS Sonoma Version 14.3

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 local_mypy_debug.py reproducer and run the reported python -m mypy --show-traceback command, comparing the four class-definition cases and their semantic-analysis deferrals. Done means the reproducer no longer reaches the maximum semantic analysis iteration count, with coverage for the optional generic cases.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.