The Fix
pip install pydantic==2.6.1
Based on closed pydantic/pydantic issue #8665 · PR/commit linked
@@ -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:
from pydantic import BaseModel
class MongoSettings(BaseModel):
MONGO_PASSWORD: str | None
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.
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
- GitHub issue: #8665
- Fix PR: https://github.com/pydantic/pydantic/pull/8666
- First fixed release: 2.6.1
- Reproduced locally: No (not executed)
- Last verified: 2026-02-09
- Confidence: 0.85
- Did this fix it?: Yes (upstream fix exists)
- Own content ratio: 0.39
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…”
“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…”
“@kikones34, Thanks for bringing this to our attention. We'll do a patch release tomorrow including @dmontagu's fix!”
Failure Signature (Search String)
- testo.py:4: error: INTERNAL ERROR -- Please try using mypy master on GitHub:
Error Message
Stack trace
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
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
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.
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
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
| Version | Status |
|---|---|
| 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.