The Fix
pip install pydantic==2.7
Based on closed pydantic/pydantic issue #9068 · PR/commit linked
Production note: Most teams hit this during upgrades or environment changes. Roll out with a canary and smoke critical endpoints (health, OpenAPI/docs) before 100%.
@@ -126,10 +126,6 @@ def plugin(version: str) -> type[Plugin]:
-class _DeferAnalysis(Exception):
- pass
-
import pydantic
class ModelNested(pydantic.BaseModel):
my_var: str | None = pydantic.Field(default=None)
class Model(ModelNested):
pass
m = Model()
if m.my_var is None:
print("hello there") # test.py:14: error: Statement is unreachable [unreachable]
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.7\nWhen NOT to use: Do not apply this fix if using a version of Pydantic prior to 2.7.\n\n
Why This Fix Works in Production
- Trigger: print("hello there") # test.py:14: error: Statement is unreachable [unreachable]
- Mechanism: Mypy incorrectly infers that a config variable can never be None in inherited BaseModel classes
- Why the fix works: Fix handling of optionals in mypy plugin to resolve mypy issues with optional types. (first fixed release: 2.7).
- If left unfixed, the same config can fail only in production (env differences), causing startup failures or partial feature outages.
Why This Breaks in Prod
- Mypy incorrectly infers that a config variable can never be None in inherited BaseModel classes
- Production symptom (often without a traceback): print("hello there") # test.py:14: error: Statement is unreachable [unreachable]
Proof / Evidence
- GitHub issue: #9068
- Fix PR: https://github.com/pydantic/pydantic/pull/9008
- First fixed release: 2.7
- 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.65
Discussion
High-signal excerpts from the issue thread (symptoms, repros, edge-cases).
“Should be fixed in 2.7, coming out soon! Fixed by @dmontagu here: https://github.com/pydantic/pydantic/pull/9008”
Failure Signature (Search String)
- print("hello there") # test.py:14: error: Statement is unreachable [unreachable]
Copy-friendly signature
Failure Signature
-----------------
print("hello there") # test.py:14: error: Statement is unreachable [unreachable]
python version: 3.12.2 (main, Feb 14 2024, 11:13:34) [Clang 15.0.0 (clang-1500.1.0.2.5)]
Error Message
Signature-only (no traceback captured)
Error Message
-------------
print("hello there") # test.py:14: error: Statement is unreachable [unreachable]
python version: 3.12.2 (main, Feb 14 2024, 11:13:34) [Clang 15.0.0 (clang-1500.1.0.2.5)]
Minimal Reproduction
import pydantic
class ModelNested(pydantic.BaseModel):
my_var: str | None = pydantic.Field(default=None)
class Model(ModelNested):
pass
m = Model()
if m.my_var is None:
print("hello there") # test.py:14: error: Statement is unreachable [unreachable]
Environment
- Pydantic: 2
What Broke
Mypy reports unreachable code errors when checking optional variables in models.
Why It Broke
Mypy incorrectly infers that a config variable can never be None in inherited BaseModel classes
Fix Options (Details)
Option A — Upgrade to fixed release Safe default (recommended)
pip install pydantic==2.7
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/9008
First fixed release: 2.7
Last verified: 2026-02-09. Validate in your environment.
When NOT to Use This Fix
- Do not apply this fix if using a version of Pydantic prior to 2.7.
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.7 | Fixed |
Related Issues
No related fixes found.
Sources
We don’t republish the full GitHub discussion text. Use the links above for context.