Inherit Pydantic BaseModel with optional generic attributes inside a function block
Open
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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