Jump to solution
Verify

The Fix

pip install pydantic==2.6.1

Based on closed pydantic/pydantic issue #8665 · PR/commit linked

Jump to Verify Open PR/Commit
@@ -475,9 +475,9 @@ def transform(self) -> bool: is_settings = any(base.fullname == BASESETTINGS_FULLNAME for base in info.mro[:-1]) - self.add_initializer(fields, config, is_settings, is_root_model) - self.add_model_construct_method(fields, config, is_settings) try:
repro.py
from pydantic import BaseModel class MongoSettings(BaseModel): MONGO_PASSWORD: str | None
verify
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.
fix.md
Option A — Upgrade to fixed release\npip install pydantic==2.6.1\nWhen NOT to use: This fix should not be used if the mypy plugin is not required or if strict optional handling is needed.\n\n

Why This Fix Works in Production

  • Trigger: testo.py:4: error: INTERNAL ERROR -- Please try using mypy master on GitHub:
  • Mechanism: The mypy plugin was not handling all scenarios leading to a _DeferAnalysis error
  • Why the fix works: Fixes a bug with the mypy plugin when using no_strict_optional, preventing mypy from crashing. (first fixed release: 2.6.1).

Why This Breaks in Prod

  • Shows up under Python 3.12 in real deployments (not just unit tests).
  • The mypy plugin was not handling all scenarios leading to a _DeferAnalysis error
  • Surfaces as: testo.py:4: error: INTERNAL ERROR -- Please try using mypy master on GitHub:

Proof / Evidence

Discussion

High-signal excerpts from the issue thread (symptoms, repros, edge-cases).

“@kikones34 thanks for reporting, I've opened a PR that I believe fixes it — #8666. It would be great if you could confirm whether that…”
@dmontagu · 2024-01-29 · confirmation · source
“Yep, this was introduced in #7411 and it's obvious that the change I made there was not handling all possible scenarios under which the _DeferAnalysis…”
@dmontagu · 2024-01-29 · source
“@kikones34, Thanks for bringing this to our attention. We'll do a patch release tomorrow including @dmontagu's fix!”
@sydney-runkle · 2024-01-29 · source

Failure Signature (Search String)

  • testo.py:4: error: INTERNAL ERROR -- Please try using mypy master on GitHub:

Error Message

Stack trace
error.txt
Error Message ------------- testo.py:4: error: INTERNAL ERROR -- Please try using mypy master on GitHub: https://mypy.readthedocs.io/en/stable/common_issues.html#using-a-development-mypy-build Please report a bug at https://github.com/python/mypy/issues version: 1.8.0 Traceback (most recent call last): File "<frozen runpy>", line 198, in _run_module_as_main File "<frozen runpy>", line 88, in _run_code File "mypy\semanal.py", line 6539, in accept File "mypy\nodes.py", line 1142, in accept File "mypy\semanal.py", line 1612, in visit_class_def File "mypy\semanal.py", line 1697, in analyze_class File "mypy\semanal.py", line 1731, in analyze_class_body_common File "mypy\semanal.py", line 1816, in apply_class_plugin_hooks File "C:\Users\arico\venv2\Lib\site-packages\pydantic\mypy.py", line 187, in _pydantic_model_class_maker_callback return transformer.transform() ^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\arico\venv2\Lib\site-packages\pydantic\mypy.py", line 478, in transform self.add_initializer(fields, config, is_settings, is_root_model) File "C:\Users\arico\venv2\Lib\site-packages\pydantic\mypy.py", line 856, in add_initializer args = self.get_field_arguments( ^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\arico\venv2\Lib\site-packages\pydantic\mypy.py", line 1039, in get_field_arguments field.to_argument( File "C:\Users\arico\venv2\Lib\site-packages\pyd ... (truncated) ...

Minimal Reproduction

repro.py
from pydantic import BaseModel class MongoSettings(BaseModel): MONGO_PASSWORD: str | None

Environment

  • Python: 3.12
  • Pydantic: 2

What Broke

Mypy crashes when parsing models with optional fields, causing development interruptions.

Why It Broke

The mypy plugin was not handling all scenarios leading to a _DeferAnalysis error

Fix Options (Details)

Option A — Upgrade to fixed release Safe default (recommended)

pip install pydantic==2.6.1

When NOT to use: This fix should not be used if the mypy plugin is not required or if strict optional handling is needed.

Use when you can deploy the upstream fix. It is usually lower-risk than long-lived workarounds.

Fix reference: https://github.com/pydantic/pydantic/pull/8666

First fixed release: 2.6.1

Last verified: 2026-02-09. Validate in your environment.

Get updates

We publish verified fixes weekly. No spam.

Subscribe

When NOT to Use This Fix

  • This fix should not be used if the mypy plugin is not required or if strict optional handling is needed.

Verify Fix

verify
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.

Did This Fix Work in Your Case?

Quick signal helps us prioritize which fixes to verify and improve.

Prevention

  • Add a CI check that diffs key outputs after upgrades (OpenAPI schema snapshots, JSON payload shapes, CLI output).
  • Upgrade behind a canary and run integration tests against the canary before 100% rollout.

Version Compatibility Table

VersionStatus
2.6.1 Fixed

Related Issues

No related fixes found.

Sources

We don’t republish the full GitHub discussion text. Use the links above for context.